|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | - |
5 | 4 | classModel(object): |
| 5 | +def__iter__(self): |
| 6 | +raiseNotImplementedError |
| 7 | + |
| 8 | +defget(self, item): |
| 9 | +"""Returns an object with a .items() call method |
| 10 | + that iterates over key,value pairs of its information.""" |
| 11 | +raiseNotImplementedError |
| 12 | + |
| 13 | +@property |
| 14 | +defitem_type(self): |
| 15 | +raiseNotImplementedError |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +classProductModel(Model): |
| 20 | + |
| 21 | +classPrice(float): |
| 22 | +"""A polymorphic way to pass a float with a particular __str__ functionality.""" |
| 23 | +def__str__(self): |
| 24 | +first_digits_str=str(round(self,2)) |
| 25 | +try: |
| 26 | +dot_location=first_digits_str.index('.') |
| 27 | +exceptValueError: |
| 28 | +return (first_digits_str+'.00') |
| 29 | +else: |
| 30 | +return (first_digits_str+ |
| 31 | +'0'*(3+dot_location-len(first_digits_str))) |
6 | 32 |
|
7 | 33 | products={ |
8 | | -'milk':{'price': 1.50, 'quantity': 10}, |
9 | | -'eggs':{'price': 0.20, 'quantity': 100}, |
10 | | -'cheese':{'price': 2.00, 'quantity': 10} |
| 34 | +'milk':{'price': Price(1.50), 'quantity': 10}, |
| 35 | +'eggs':{'price': Price(0.20), 'quantity': 100}, |
| 36 | +'cheese':{'price': Price(2.00), 'quantity': 10} |
11 | 37 | } |
12 | 38 |
|
| 39 | +item_type='product' |
| 40 | + |
| 41 | +def__iter__(self): |
| 42 | +foriteminself.products: |
| 43 | +yielditem |
| 44 | + |
| 45 | +defget(self, product): |
| 46 | +try: |
| 47 | +returnself.products[product] |
| 48 | +exceptKeyErrorase: |
| 49 | +raiseKeyError((str(e) +" not in the model's item list.")) |
13 | 50 |
|
14 | 51 | classView(object): |
| 52 | +defshow_item_list(self, item_type, item_list): |
| 53 | +raiseNotImplementedError |
15 | 54 |
|
16 | | -defproduct_list(self, product_list): |
17 | | -print('PRODUCT LIST:') |
18 | | -forproductinproduct_list: |
19 | | -print(product) |
20 | | -print('') |
| 55 | +defshow_item_information(self, item_type, item_name, item_info): |
| 56 | +"""Will look for item information by iterating over key,value pairs |
| 57 | + yielded by item_info.items()""" |
| 58 | +raiseNotImplementedError |
21 | 59 |
|
22 | | -defproduct_information(self, product, product_info): |
23 | | -print('PRODUCT INFORMATION:') |
24 | | -print('Name: %s, Price: %.2f, Quantity: %d\n'% |
25 | | - (product.title(), product_info.get('price', 0), |
26 | | -product_info.get('quantity', 0))) |
| 60 | +defitem_not_found(self, item_type, item_name): |
| 61 | +raiseNotImplementedError |
27 | 62 |
|
28 | | -defproduct_not_found(self, product): |
29 | | -print('That product "%s" does not exist in the records'%product) |
| 63 | +classConsoleView(View): |
30 | 64 |
|
| 65 | +defshow_item_list(self, item_type, item_list): |
| 66 | +print(item_type.upper() +' LIST:') |
| 67 | +foriteminitem_list: |
| 68 | +print(item) |
| 69 | +print('') |
31 | 70 |
|
32 | | -classController(object): |
| 71 | +@staticmethod |
| 72 | +defcapitalizer(string): |
| 73 | +returnstring[0].upper() +string[1:].lower() |
33 | 74 |
|
34 | | -def__init__(self): |
35 | | -self.model=Model() |
36 | | -self.view=View() |
| 75 | +defshow_item_information(self, item_type, item_name, item_info): |
| 76 | +print(item_type.upper() +' INFORMATION:') |
| 77 | +printout='Name: %s'%item_name |
| 78 | +forkey, valueinitem_info.items(): |
| 79 | +printout+= (', '+self.capitalizer(str(key)) +': '+str(value)) |
| 80 | +printout+='\n' |
| 81 | +print(printout) |
37 | 82 |
|
38 | | -defget_product_list(self): |
39 | | -product_list=self.model.products.keys() |
40 | | -self.view.product_list(product_list) |
| 83 | +defitem_not_found(self, item_type, item_name): |
| 84 | +print('That %s "%s" does not exist in the records'% (item_type, item_name)) |
| 85 | + |
| 86 | + |
| 87 | +classController(object): |
41 | 88 |
|
42 | | -defget_product_information(self, product): |
43 | | -product_info=self.model.products.get(product, None) |
44 | | -ifproduct_infoisnotNone: |
45 | | -self.view.product_information(product, product_info) |
| 89 | +def__init__(self, model, view): |
| 90 | +self.model=model |
| 91 | +self.view=view |
| 92 | + |
| 93 | +defshow_items(self): |
| 94 | +items=list(self.model) |
| 95 | +item_type=self.model.item_type |
| 96 | +self.view.show_item_list(item_type, items) |
| 97 | + |
| 98 | +defshow_item_information(self, item_name): |
| 99 | +try: |
| 100 | +item_info=self.model.get(item_name) |
| 101 | +except: |
| 102 | +item_type=self.model.item_type |
| 103 | +self.view.item_not_found(item_type, item_name) |
46 | 104 | else: |
47 | | -self.view.product_not_found(product) |
| 105 | +item_type=self.model.item_type |
| 106 | +self.view.show_item_information(item_type, item_name, item_info) |
48 | 107 |
|
49 | 108 |
|
50 | 109 | if__name__=='__main__': |
51 | 110 |
|
52 | | -controller=Controller() |
53 | | -controller.get_product_list() |
54 | | -controller.get_product_information('cheese') |
55 | | -controller.get_product_information('eggs') |
56 | | -controller.get_product_information('milk') |
57 | | -controller.get_product_information('arepas') |
| 111 | +model=ProductModel() |
| 112 | +view=ConsoleView() |
| 113 | +controller=Controller(model, view) |
| 114 | +controller.show_items() |
| 115 | +controller.show_item_information('cheese') |
| 116 | +controller.show_item_information('eggs') |
| 117 | +controller.show_item_information('milk') |
| 118 | +controller.show_item_information('arepas') |
58 | 119 |
|
59 | 120 | ### OUTPUT ### |
60 | 121 | # PRODUCT LIST: |
|
0 commit comments