3-tier.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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:
  27. print('PRODUCT INFORMATION:')
  28. print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
  29. product.title(), product_info.get('price', 0),
  30. product_info.get('quantity', 0)))
  31. else:
  32. print('That product "{0}" does not exist in the records'.format(
  33. product))
  34. def main():
  35. ui = Ui()
  36. ui.get_product_list()
  37. ui.get_product_information('cheese')
  38. ui.get_product_information('eggs')
  39. ui.get_product_information('milk')
  40. ui.get_product_information('arepas')
  41. if __name__ == '__main__':
  42. main()
  43. ### OUTPUT ###
  44. # PRODUCT LIST:
  45. # eggs
  46. # milk
  47. # cheese
  48. #
  49. # PRODUCT INFORMATION:
  50. # Name: Cheese, Price: 2.00, Quantity: 10
  51. # PRODUCT INFORMATION:
  52. # Name: Eggs, Price: 0.20, Quantity: 100
  53. # PRODUCT INFORMATION:
  54. # Name: Milk, Price: 1.50, Quantity: 10
  55. # That product "arepas" does not exist in the records