Skip to content

Commit b1b0950

Browse files
pg.181
1 parent caf9b58 commit b1b0950

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from collections import namedtuple
2+
3+
Customer = namedtuple('Customer', 'name fidelity')
4+
5+
6+
class LineItem:
7+
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: # the Context
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+
def fidelity_promo(order):
41+
"""5% discount for customers with 1000 or more fidelity points"""
42+
return order.total() * .05 if order.customer.fidelity >- 1000 else 0
43+
44+
def bulk_item_promo(order):
45+
"""10% discount for each LineItem with 20 or more units"""
46+
discount = 0
47+
for item in order.cart:
48+
if item.quantity >= 20:
49+
discount += item.total() * .1
50+
return discount
51+
52+
def large_order_promo(order):
53+
"""7% discount for orders with 10 or more items"""
54+
distinct_items = {item.product for item in order.cart}
55+
if len(distinct_items) >= 10:
56+
return order.total() * .07
57+
return 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"<Order total: 42.00 due: 39.90>"
12+
]
13+
},
14+
"execution_count": 1,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"from ex_6_3__functional_strategy import (\n",
21+
" Customer,\n",
22+
" LineItem,\n",
23+
" Order,\n",
24+
" fidelity_promo,\n",
25+
" bulk_item_promo,\n",
26+
" large_order_promo,\n",
27+
")\n",
28+
"joe = Customer('John Doe', 0)\n",
29+
"ann = Customer('Ann Smith', 1100)\n",
30+
"cart = [\n",
31+
" LineItem('banana', 4, .5),\n",
32+
" LineItem('apple', 10, 1.5),\n",
33+
" LineItem('watermellon', 5, 5.0),\n",
34+
"]\n",
35+
"Order(joe, cart, fidelity_promo)\n"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 2,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"data": {
45+
"text/plain": [
46+
"<Order total: 42.00 due: 39.90>"
47+
]
48+
},
49+
"execution_count": 2,
50+
"metadata": {},
51+
"output_type": "execute_result"
52+
}
53+
],
54+
"source": [
55+
"Order(ann, cart, fidelity_promo)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 3,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/plain": [
66+
"<Order total: 30.00 due: 28.50>"
67+
]
68+
},
69+
"execution_count": 3,
70+
"metadata": {},
71+
"output_type": "execute_result"
72+
}
73+
],
74+
"source": [
75+
"banana_cart = [\n",
76+
" LineItem('banana', 30, .5),\n",
77+
" LineItem('apple', 10, 1.5),\n",
78+
"]\n",
79+
"Order(joe, banana_cart, bulk_item_promo)"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": 4,
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"ename": "NameError",
89+
"evalue": "name 'large_order_promo' is not defined",
90+
"output_type": "error",
91+
"traceback": [
92+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
93+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
94+
"\u001b[0;32m<ipython-input-4-80e58dad44d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mitem_code\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m ]\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mOrder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjoe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlong_order\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlarge_order_promo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
95+
"\u001b[0;31mNameError\u001b[0m: name 'large_order_promo' is not defined"
96+
]
97+
}
98+
],
99+
"source": [
100+
"long_order = [\n",
101+
" LineItem(str(item_code), 1, 1.0)\n",
102+
" for item_code in range(10)\n",
103+
"]\n",
104+
"Order(joe, long_order, large_order_promo)"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": []
113+
}
114+
],
115+
"metadata": {
116+
"interpreter": {
117+
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
118+
},
119+
"kernelspec": {
120+
"display_name": "Python 3.8.10 64-bit",
121+
"language": "python",
122+
"name": "python3"
123+
},
124+
"language_info": {
125+
"codemirror_mode": {
126+
"name": "ipython",
127+
"version": 3
128+
},
129+
"file_extension": ".py",
130+
"mimetype": "text/x-python",
131+
"name": "python",
132+
"nbconvert_exporter": "python",
133+
"pygments_lexer": "ipython3",
134+
"version": "3.8.10"
135+
},
136+
"orig_nbformat": 4
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 2
140+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

0 commit comments

Comments
 (0)