- Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy path3-tier.py
98 lines (73 loc) · 2.38 KB
/
3-tier.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
*TL;DR
Separates presentation, application processing, and data management functions.
"""
fromtypingimportDict, KeysView, Optional, Union
classData:
"""Data Store Class"""
products= {
"milk": {"price": 1.50, "quantity": 10},
"eggs": {"price": 0.20, "quantity": 100},
"cheese": {"price": 2.00, "quantity": 10},
}
def__get__(self, obj, klas):
print("(Fetching from Data Store)")
return {"products": self.products}
classBusinessLogic:
"""Business logic holding data store instances"""
data=Data()
defproduct_list(self) ->KeysView[str]:
returnself.data["products"].keys()
defproduct_information(
self, product: str
) ->Optional[Dict[str, Union[int, float]]]:
returnself.data["products"].get(product, None)
classUi:
"""UI interaction class"""
def__init__(self) ->None:
self.business_logic=BusinessLogic()
defget_product_list(self) ->None:
print("PRODUCT LIST:")
forproductinself.business_logic.product_list():
print(product)
print("")
defget_product_information(self, product: str) ->None:
product_info=self.business_logic.product_information(product)
ifproduct_info:
print("PRODUCT INFORMATION:")
print(
f"Name: {product.title()}, "
+f"Price: {product_info.get('price', 0):.2f}, "
+f"Quantity: {product_info.get('quantity', 0):}"
)
else:
print(f"That product '{product}' does not exist in the records")
defmain():
"""
>>> ui = Ui()
>>> ui.get_product_list()
PRODUCT LIST:
(Fetching from Data Store)
milk
eggs
cheese
<BLANKLINE>
>>> ui.get_product_information("cheese")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Cheese, Price: 2.00, Quantity: 10
>>> ui.get_product_information("eggs")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Eggs, Price: 0.20, Quantity: 100
>>> ui.get_product_information("milk")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Milk, Price: 1.50, Quantity: 10
>>> ui.get_product_information("arepas")
(Fetching from Data Store)
That product 'arepas' does not exist in the records
"""
if__name__=="__main__":
importdoctest
doctest.testmod()