|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 |
| - |
| 3 | + |
| 4 | + |
4 | 5 | class Model(object):
|
5 |
| - |
| 6 | + |
6 | 7 | products = {
|
7 |
| - 'milk': { 'price': 1.50, 'quantity': 10 }, |
8 |
| - 'eggs': { 'price': 0.20, 'quantity': 100 }, |
9 |
| - 'cheese': { 'price': 2.00, 'quantity': 10 } |
| 8 | + 'milk': {'price': 1.50, 'quantity': 10}, |
| 9 | + 'eggs': {'price': 0.20, 'quantity': 100}, |
| 10 | + 'cheese': {'price': 2.00, 'quantity': 10} |
10 | 11 | }
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class View(object):
|
14 |
| - |
| 15 | + |
15 | 16 | def product_list(self, product_list):
|
16 | 17 | print('PRODUCT LIST:')
|
17 | 18 | for product in product_list:
|
18 | 19 | print(product)
|
19 | 20 | print('')
|
20 |
| - |
| 21 | + |
21 | 22 | def product_information(self, product, product_info):
|
22 | 23 | print('PRODUCT INFORMATION:')
|
23 |
| - print('Name: %s, Price: %.2f, Quantity: %d\n' % \ |
24 |
| - (product.title(), product_info.get('price', 0), \ |
| 24 | + print('Name: %s, Price: %.2f, Quantity: %d\n' % |
| 25 | + (product.title(), product_info.get('price', 0), |
25 | 26 | product_info.get('quantity', 0)))
|
26 | 27 |
|
27 | 28 | def product_not_found(self, product):
|
28 | 29 | print('That product "%s" does not exist in the records' % product)
|
29 | 30 |
|
30 | 31 |
|
31 | 32 | class Controller(object):
|
32 |
| - |
| 33 | + |
33 | 34 | def __init__(self):
|
34 | 35 | self.model = Model()
|
35 | 36 | self.view = View()
|
36 |
| - |
| 37 | + |
37 | 38 | def get_product_list(self):
|
38 | 39 | product_list = self.model.products.keys()
|
39 | 40 | self.view.product_list(product_list)
|
40 |
| - |
| 41 | + |
41 | 42 | def get_product_information(self, product):
|
42 | 43 | product_info = self.model.products.get(product, None)
|
43 | 44 | if product_info is not None:
|
44 | 45 | self.view.product_information(product, product_info)
|
45 | 46 | else:
|
46 | 47 | self.view.product_not_found(product)
|
47 |
| - |
48 |
| - |
| 48 | + |
| 49 | + |
49 | 50 | if __name__ == '__main__':
|
50 |
| - |
| 51 | + |
51 | 52 | controller = Controller()
|
52 | 53 | controller.get_product_list()
|
53 | 54 | controller.get_product_information('cheese')
|
|
0 commit comments