3-tier.py 1.8 KB

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