-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_hw.py
105 lines (76 loc) · 2.92 KB
/
bank_hw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Customer:
last_id = 0
def _init_(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name
self.email = email
Customer.last_id += 1
self.id = Customer.last_id
def _repr_(self):
return 'Customer[{},{},{},{}]'.format(self.id, self.first_name, self.last_name, self.email)
class Account:
last_id = 0
def _init_(self, customer):
Account.last_id += 1
self.id = Account.last_id
self.customer = customer
self._balance = 0
def charge(self, amount):
if self._balance >= amount:
self._balance -= amount
else:
print('Insuffecient Balance')
def deposit(self, amount):
if amount > 0:
self._balance += amount
def get_balance(self):
return self._balance
def _repr_(self):
return '{}[{},{},{}]'.format(self._class.name_, self.id, self.customer.last_name, self._balance)
class SavingsAccount(Account):
interest_rate = 0.02
def calc_interest(self):
self._balance += self.interest_rate * self._balance
class CheckingAccount(Account):
pass
class Bank:
def _init_(self):
self.cust_list = []
self.acc_list = []
def new_customer(self, first_name, last_name, email):
c = Customer(first_name, last_name, email)
self.cust_list.append(c)
return c
def new_account(self, customer, is_savings=True):
a = SavingsAccount(customer) if is_savings else CheckingAccount(customer)
self.acc_list.append(a)
return a
def get_account(self, account_id):
return [account for account in self.acc_list if account.id == account_id]
def is_valid_transfer(self, from_account, to_account, amount):
is_valid_transfer = False
if from_account and to_account:
if from_account[0].get_balance() >= amount:
is_valid_transfer = True
return is_valid_transfer
def transfer(self, from_account_id, to_account_id, amount):
from_account, to_account = self.get_account(from_account_id), self.get_account(to_account_id)
if self.is_valid_transfer(from_account, to_account, amount):
to_account[0].deposit(amount)
from_account[0].charge(amount)
else:
print('Invalid Transfer request')
def _repr_(self):
return 'Bank\n{}\n{}'.format(self.cust_list, self.acc_list)
b = Bank()
c1 = b.new_customer('John', 'Brown', 'john@brown.com')
c2 = b.new_customer('Anna', 'Smith', 'anne@smith.com')
a1 = b.new_account(c1, is_savings=True)
a2 = b.new_account(c1, is_savings=False)
print(b)
a1.deposit(10)
print('------------------------')
print('Accounts before transfer: \n from_account: {} \n to_account: {}'.format(a1, a2))
b.transfer(a1.id, a2.id, 5)
print('-----------------------')
print('Accounts after transfer: \n from_account: {} \n to_account: {}'.format(a1, a2))