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