forked from faif/python-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvc.py
More file actions
Latest commit
74 lines (57 loc) · 1.87 KB
/
Copy pathmvc.py
File metadata and controls
74 lines (57 loc) · 1.87 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
classModel(object):
products= {
'milk': {'price': 1.50, 'quantity': 10},
'eggs': {'price': 0.20, 'quantity': 100},
'cheese': {'price': 2.00, 'quantity': 10}
}
classView(object):
defproduct_list(self, product_list):
print('PRODUCT LIST:')
forproductinproduct_list:
print(product)
print('')
defproduct_information(self, product, product_info):
print('PRODUCT INFORMATION:')
print('Name: %s, Price: %.2f, Quantity: %d\n'%
(product.title(), product_info.get('price', 0),
product_info.get('quantity', 0)))
defproduct_not_found(self, product):
print('That product "%s" does not exist in the records'%product)
classController(object):
def__init__(self):
self.model=Model()
self.view=View()
defget_product_list(self):
product_list=self.model.products.keys()
self.view.product_list(product_list)
defget_product_information(self, product):
product_info=self.model.products.get(product, None)
ifproduct_infoisnotNone:
self.view.product_information(product, product_info)
else:
self.view.product_not_found(product)
if__name__=='__main__':
controller=Controller()
controller.get_product_list()
controller.get_product_information('cheese')
controller.get_product_information('eggs')
controller.get_product_information('milk')
controller.get_product_information('arepas')
### OUTPUT ###
# PRODUCT LIST:
# cheese
# eggs
# milk
#
# PRODUCT INFORMATION:
# Name: Cheese, Price: 2.00, Quantity: 10
#
# PRODUCT INFORMATION:
# Name: Eggs, Price: 0.20, Quantity: 100
#
# PRODUCT INFORMATION:
# Name: Milk, Price: 1.50, Quantity: 10
#
# That product "arepas" does not exist in the records