forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts0.py
49 lines (40 loc) · 1.34 KB
/
accounts0.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
#!/usr/bin/env python3
class Accounts(object):
accounts = 0
types = ('A','L','I','E','OE')
def __init__(self, balance = 0, atype = 'A'):
self._balance = balance
self._type = atype
def debit(self, amount):
#self._balance += (amount if self._type in ('A','E') else -amount)
if self._type == 'A' or self._type == 'E':
self._balance += amount
else:
self._balance -= amount
def credit(self,amount):
self.debit(-amount)
def get_balance(self):
return self._balance
def test_accounts(self):
print(self)
class CreditAccounts(Accounts):
def __init__(self, balance=0, atype='A', credit_limit=0 ):
self.credit_limit = credit_limit
Accounts.__init__(self,balance,atype)
def debit(self, amount):
if self._type in ('A','E'): value = -amount
else: value = amount
print(value,self._balance)
if self._balance + self.credit_limit > value:
Accounts.debit(self, amount)
else:
raise ValueError('insufficient funds')
if __name__ == "__main__":
o = CreditAccounts(100, credit_limit=100);
print(o.get_balance())
print(o.credit_limit)
o.debit(50)
o.credit(300)
print(o.get_balance())
print(isinstance(o,Accounts))
print(isinstance(o,CreditAccounts))