3-tier.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class Data(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 BusinessLogic(object):
  10. def __init__(self):
  11. self.data = Data()
  12. def product_list(self):
  13. return self.data.products.keys()
  14. def product_information(self, product):
  15. return self.data.products.get(product, None)
  16. class Ui(object):
  17. def __init__(self):
  18. self.business_logic = BusinessLogic()
  19. def get_product_list(self):
  20. print('PRODUCT LIST:')
  21. for product in self.business_logic.product_list():
  22. print(product)
  23. print('')
  24. def get_product_information(self, product):
  25. product_info = self.business_logic.product_information(product)
  26. if product_info is not None:
  27. print('PRODUCT INFORMATION:')
  28. print('Name: %s, Price: %.2f, Quantity: %d\n' %
  29. (product.title(), product_info.get('price', 0),
  30. product_info.get('quantity', 0)))
  31. else:
  32. print('That product "%s" does not exist in the records' % product)
  33. if __name__ == '__main__':
  34. ui = Ui()
  35. ui.get_product_list()
  36. ui.get_product_information('cheese')
  37. ui.get_product_information('eggs')
  38. ui.get_product_information('milk')
  39. ui.get_product_information('arepas')