Skip to content

Commit

Permalink
pg.181
Browse files Browse the repository at this point in the history
  • Loading branch information
CavalcanteLucas committed Nov 8, 2021
1 parent caf9b58 commit b1b0950
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from collections import namedtuple

Customer = namedtuple('Customer', 'name fidelity')


class LineItem:

def __init__(self, product, quantity, price):
self.product = product
self.quantity = quantity
self.price = price

def total (self):
return self.price * self.quantity


class Order: # the Context

def __init__(self, customer, cart, promotion = None):
self.customer = customer
self.cart = list(cart)
self.promotion = promotion

def total(self):
if not hasattr(self, '__total'):
self.__total = sum(item.total() for item in self.cart)
return self.__total

def due(self):
if self.promotion is None:
discount = 0
else:
discount = self.promotion(self)
return self.total() - discount

def __repr__(self):
fmt = '<Order total: {:.2f} due: {:.2f}>'
return fmt.format(self.total(), self.due())

def fidelity_promo(order):
"""5% discount for customers with 1000 or more fidelity points"""
return order.total() * .05 if order.customer.fidelity >- 1000 else 0

def bulk_item_promo(order):
"""10% discount for each LineItem with 20 or more units"""
discount = 0
for item in order.cart:
if item.quantity >= 20:
discount += item.total() * .1
return discount

def large_order_promo(order):
"""7% discount for orders with 10 or more items"""
distinct_items = {item.product for item in order.cart}
if len(distinct_items) >= 10:
return order.total() * .07
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Order total: 42.00 due: 39.90>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ex_6_3__functional_strategy import (\n",
" Customer,\n",
" LineItem,\n",
" Order,\n",
" fidelity_promo,\n",
" bulk_item_promo,\n",
" large_order_promo,\n",
")\n",
"joe = Customer('John Doe', 0)\n",
"ann = Customer('Ann Smith', 1100)\n",
"cart = [\n",
" LineItem('banana', 4, .5),\n",
" LineItem('apple', 10, 1.5),\n",
" LineItem('watermellon', 5, 5.0),\n",
"]\n",
"Order(joe, cart, fidelity_promo)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Order total: 42.00 due: 39.90>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Order(ann, cart, fidelity_promo)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Order total: 30.00 due: 28.50>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"banana_cart = [\n",
" LineItem('banana', 30, .5),\n",
" LineItem('apple', 10, 1.5),\n",
"]\n",
"Order(joe, banana_cart, bulk_item_promo)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'large_order_promo' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\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",
"\u001b[0;31mNameError\u001b[0m: name 'large_order_promo' is not defined"
]
}
],
"source": [
"long_order = [\n",
" LineItem(str(item_code), 1, 1.0)\n",
" for item_code in range(10)\n",
"]\n",
"Order(joe, long_order, large_order_promo)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
},
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions default-54e50c86-af21-45a9-8ae4-b8d7ed2804d9.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit b1b0950

Please sign in to comment.