Advertisement
furas

Python - ShoppingCart & Shop

Feb 28th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. class ShoppingCart:
  2.    
  3.     def __init__ (self):    
  4.         self.total = 0
  5.         self.items = {}
  6.  
  7.     def add_item(self, item_name, quantity, price):
  8.         self.total += quantity * price
  9.         self.items[item_name] = quantity
  10.  
  11.     def remove_item(self, item_name, quantity, price):
  12.         if quantity > self.items[item_name]:
  13.             quantity = self.items[item_name]
  14.            
  15.         self.total -= quantity * price
  16.         self.items[item_name] -= quantity
  17.  
  18.     def checkout(self, cash_paid):
  19.         if cash_paid < self.total:
  20.             return "Cash paid not enough"
  21.  
  22.         return cash_paid - self.total
  23.  
  24. class Shop(ShoppingCart):
  25.    
  26.     def __init__(self):
  27.         # ShoppingCart "constructor"
  28.         super().__init__()
  29.  
  30.         self.quantity = 100
  31.  
  32.     def remove_item(self, item_name=None, quantity=None, price=None):
  33.         if item_name is None or quantity is None or price is None:
  34.             self.quantity -= 1
  35.         else:
  36.             # ShoppingCart "remove_item"
  37.             super().remove_item(item_name, quantity, price)
  38.        
  39. # --- test ---
  40.  
  41. a = Shop()
  42.  
  43. print('\n--- adding items ---\n')
  44.  
  45. a.add_item('banana', 5, 4)
  46. a.add_item('orange', 3, 5)
  47.  
  48. print('total:', a.total)
  49. print('items:', a.items)
  50. print('quantity:', a.quantity)
  51.  
  52. print('\n--- removing without arguments ---\n')
  53.  
  54. a.remove_item()
  55.  
  56. print('total:', a.total)
  57. print('items:', a.items)
  58. print('quantity:', a.quantity)
  59.  
  60. print('\n--- removing 6 bananas ---\n')
  61.  
  62. a.remove_item('banana', 6, 4)
  63.  
  64. print('total:', a.total)
  65. print('items:', a.items)
  66. print('quantity:', a.quantity)
  67.  
  68. print('\n--- removing 3 bananas ---\n')
  69.  
  70. a.remove_item('banana', 3, 4)
  71.  
  72. print('total:', a.total)
  73. print('items:', a.items)
  74. print('quantity:', a.quantity)
  75.  
  76. print('\n--- checkout ---\n')
  77.  
  78. print('result:', a.checkout(10))
  79. print('result:', a.checkout(50))
  80.  
  81. '''
  82. --- adding items ---
  83.  
  84. total: 35
  85. items: {'banana': 5, 'orange': 3}
  86. quantity: 100
  87.  
  88. --- removing without arguments ---
  89.  
  90. total: 35
  91. items: {'banana': 5, 'orange': 3}
  92. quantity: 99
  93.  
  94. --- removing 6 bananas ---
  95.  
  96. total: 15
  97. items: {'banana': 0, 'orange': 3}
  98. quantity: 99
  99.  
  100. --- removing 3 bananas ---
  101.  
  102. total: 15
  103. items: {'banana': 0, 'orange': 3}
  104. quantity: 99
  105.  
  106. --- checkout ---
  107.  
  108. result: Cash paid not enough
  109. result: 35
  110. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement