-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathChainOfResponsibility.py
More file actions
49 lines (37 loc) · 1.12 KB
/
ChainOfResponsibility.py
File metadata and controls
49 lines (37 loc) · 1.12 KB
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
import inspect
class Account:
_successor = None
_balance = None
def setNext(self, account):
self._successor = account
def pay(self, amountToPay):
import inspect
myCaller = inspect.stack()[1][3]
if self.canPay(amountToPay):
print "Paid " + str(amountToPay) + " using " + myCaller
elif (self._successor):
print "Cannot pay using " + myCaller + ". Proceeding .."
self._successor.pay(amountToPay)
else:
raise ValueError('None of the accounts have enough balance')
def canPay(self, amount):
return self.balance >= amount
class Bank(Account):
_balance = None
def __init__(self, balance):
self.balance = balance
class Paypal(Account):
_balance = None
def __init__(self, balance):
self.balance = balance
class Bitcoin(Account):
_balance = None
def __init__(self, balance):
self.balance = balance
if __name__ == '__main__':
bank = Bank(100)
paypal = Paypal(200)
bitcoin = Bitcoin(300)
bank.setNext(paypal)
paypal.setNext(bitcoin)
bank.pay(259)