Skip to content

Commit dd5f200

Browse files
committed
committed from zkp
1 parent ede836e commit dd5f200

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

FluentPython/best_promos.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# from abc import ABC, abstractmethod
2+
from collections import namedtuple
3+
4+
Customer = namedtuple('Customer', 'name fidelity')
5+
6+
7+
class LineItem:
8+
def __init__(self, product, quantity, price):
9+
self.product = product
10+
self.quantity = quantity
11+
self.price = price
12+
13+
def total(self):
14+
return self.price * self.quantity
15+
16+
17+
class Order:
18+
19+
def __init__(self, customer, cart, promotion=None):
20+
self.customer = customer
21+
self.cart = list(cart)
22+
self.promotion = promotion
23+
24+
def total(self):
25+
if not hasattr(self, '__total'):
26+
self.__total = sum(item.total() for item in self.cart)
27+
return self.__total
28+
29+
def due(self):
30+
if self.promotion is None:
31+
discount = 0
32+
else:
33+
discount = self.promotion(self)
34+
return self.total() - discount
35+
36+
def __repr__(self):
37+
fmt = '<Order total: {:.2f} due: {:.2f}>'
38+
return fmt.format(self.total(), self.due())
39+
40+
41+
def fidelity_promo(order):
42+
'''为积分为1000或以上的顾客提供5%折扣'''
43+
return order.total() * .05 if order.customer.fidelity >= 1000 else 0
44+
45+
46+
def bulkItem_promo(order):
47+
'''单个商品为20个或以上时提供10%折扣'''
48+
discount = 0
49+
for item in order.cart:
50+
if item.quantity >= 20:
51+
discount += item.total() * .1
52+
return discount
53+
54+
55+
def large_order_promo(order):
56+
'''订单中的不同商品达到10个或以上时提供7%折扣'''
57+
distinct_items = {item.product for item in order.cart}
58+
if len(distinct_items) >= 10:
59+
return order.total() * .07
60+
return 0
61+
62+
63+
promos = [fidelity_promo, bulkItem_promo, large_order_promo]
64+
65+
66+
def best_promo(order):
67+
'''选择可用的最佳折扣'''
68+
return max(promo(order) for promo in promos)

0 commit comments

Comments
 (0)