mvc.py 1.6 KB

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