3-tier.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class Data(object):
  4. """ Data Store Class """
  5. products = {
  6. 'milk': {'price': 1.50, 'quantity': 10},
  7. 'eggs': {'price': 0.20, 'quantity': 100},
  8. 'cheese': {'price': 2.00, 'quantity': 10}
  9. }
  10. def __get__(self, obj, klas):
  11. print ("(Fetching from Data Store)")
  12. return {'products': self.products}
  13. class BusinessLogic(object):
  14. """ Business logic holding data store instances """
  15. data = Data()
  16. def product_list(self):
  17. return self.data['products'].keys()
  18. def product_information(self, product):
  19. return self.data['products'].get(product, None)
  20. class Ui(object):
  21. """ UI interaction class """
  22. def __init__(self):
  23. self.business_logic = BusinessLogic()
  24. def get_product_list(self):
  25. print('PRODUCT LIST:')
  26. for product in self.business_logic.product_list():
  27. #print(product)
  28. yield product
  29. print('')
  30. def get_product_information(self, product):
  31. product_info = self.business_logic.product_information(product)
  32. if product_info:
  33. print('PRODUCT INFORMATION:')
  34. print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
  35. product.title(), product_info.get('price', 0),
  36. product_info.get('quantity', 0)))
  37. else:
  38. print('That product "{0}" does not exist in the records'.format(
  39. product))
  40. def main():
  41. ui = Ui()
  42. ui.get_product_list()
  43. ui.get_product_information('cheese')
  44. ui.get_product_information('eggs')
  45. ui.get_product_information('milk')
  46. ui.get_product_information('arepas')
  47. if __name__ == '__main__':
  48. main()
  49. ### OUTPUT ###
  50. # (Fetching from Data Store)
  51. # PRODUCT INFORMATION:
  52. # Name: Cheese, Price: 2.00, Quantity: 10
  53. # (Fetching from Data Store)
  54. # PRODUCT INFORMATION:
  55. # Name: Eggs, Price: 0.20, Quantity: 100
  56. # (Fetching from Data Store)
  57. # PRODUCT INFORMATION:
  58. # Name: Milk, Price: 1.50, Quantity: 10
  59. # (Fetching from Data Store)
  60. # That product "arepas" does not exist in the records