|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | - |
5 | 4 | class Model(object): |
| 5 | + def __iter__(self): |
| 6 | + raise NotImplementedError |
| 7 | + |
| 8 | + def get(self, item): |
| 9 | + """Returns an object with a .items() call method |
| 10 | + that iterates over key,value pairs of its information.""" |
| 11 | + raise NotImplementedError |
| 12 | + |
| 13 | + @property |
| 14 | + def item_type(self): |
| 15 | + raise NotImplementedError |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +class ProductModel(Model): |
| 20 | + |
| 21 | + class Price(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 | + except ValueError: |
| 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 | + for item in self.products: |
| 43 | + yield item |
| 44 | + |
| 45 | + def get(self, product): |
| 46 | + try: |
| 47 | + return self.products[product] |
| 48 | + except KeyError as e: |
| 49 | + raise KeyError((str(e) + " not in the model's item list.")) |
13 | 50 |
|
14 | 51 | class View(object): |
| 52 | + def show_item_list(self, item_type, item_list): |
| 53 | + raise NotImplementedError |
15 | 54 |
|
16 | | - def product_list(self, product_list): |
17 | | - print('PRODUCT LIST:') |
18 | | - for product in product_list: |
19 | | - print(product) |
20 | | - print('') |
| 55 | + def show_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 | + raise NotImplementedError |
21 | 59 |
|
22 | | - def product_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 | + def item_not_found(self, item_type, item_name): |
| 61 | + raise NotImplementedError |
27 | 62 |
|
28 | | - def product_not_found(self, product): |
29 | | - print('That product "%s" does not exist in the records' % product) |
| 63 | +class ConsoleView(View): |
30 | 64 |
|
| 65 | + def show_item_list(self, item_type, item_list): |
| 66 | + print(item_type.upper() + ' LIST:') |
| 67 | + for item in item_list: |
| 68 | + print(item) |
| 69 | + print('') |
31 | 70 |
|
32 | | -class Controller(object): |
| 71 | + @staticmethod |
| 72 | + def capitalizer(string): |
| 73 | + return string[0].upper() + string[1:].lower() |
33 | 74 |
|
34 | | - def __init__(self): |
35 | | - self.model = Model() |
36 | | - self.view = View() |
| 75 | + def show_item_information(self, item_type, item_name, item_info): |
| 76 | + print(item_type.upper() + ' INFORMATION:') |
| 77 | + printout = 'Name: %s' % item_name |
| 78 | + for key, value in item_info.items(): |
| 79 | + printout += (', ' + self.capitalizer(str(key)) + ': ' + str(value)) |
| 80 | + printout += '\n' |
| 81 | + print(printout) |
37 | 82 |
|
38 | | - def get_product_list(self): |
39 | | - product_list = self.model.products.keys() |
40 | | - self.view.product_list(product_list) |
| 83 | + def item_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 | +class Controller(object): |
41 | 88 |
|
42 | | - def get_product_information(self, product): |
43 | | - product_info = self.model.products.get(product, None) |
44 | | - if product_info is not None: |
45 | | - self.view.product_information(product, product_info) |
| 89 | + def __init__(self, model, view): |
| 90 | + self.model = model |
| 91 | + self.view = view |
| 92 | + |
| 93 | + def show_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 | + def show_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