-
Notifications
You must be signed in to change notification settings - Fork 56
/
strategy.py
57 lines (39 loc) · 1.24 KB
/
strategy.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
import abc
class CashSuper:
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def accept_cash(self, c):
"""caculate cash"""
class CashNormal(CashSuper):
def accept_cash(self, c):
return c
class CashRebate(CashSuper):
def __init__(self, r):
self.rebate = r
def accept_cash(self, c):
return c * self.rebate
class CashReturn(CashSuper):
def __init__(self, c, r):
self.condition = c
self.money = r
def accept_cash(self, c):
return c - int(c / self.condition) * self.money
class CashContext:
def __init__(self, t, s):
self.cash = None
if t == "normal":
self.cash = CashNormal()
elif t == "rebate":
self.cash = CashRebate(float(s))
elif t == "return":
args = s.split(' ')
self.cash = CashReturn(float(args[0]), float(args[1]))
def get_result(self, c):
return self.cash.accept_cash(c)
if __name__ == "__main__":
cash_context = CashContext("normal", "")
print cash_context.get_result(1000)
cash_context = CashContext("rebate", "0.8")
print cash_context.get_result(1000)
cash_context = CashContext("return", "300 100")
print cash_context.get_result(1000)