Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. ''' Lab 2 Q 3 '''
  2. class Product:
  3. def __init__(self, code, desc, price):
  4. self._code = code
  5. self._desc = desc
  6. self._price = price
  7. @property
  8. def code(self):
  9. return self._code
  10. @property
  11. def price(self):
  12. return self._price
  13. def __str__(self):
  14. return "{} ${}".format(self._code, self._price)
  15.  
  16. ######################
  17. class CartItem:
  18. def __init__(self, prod, qty):
  19. self._prod = prod
  20. self._qty = qty
  21.  
  22. @property
  23. def pCode(self):
  24. return self._prod.code
  25. @property
  26. def qty(self):
  27. return self._qty
  28. @qty.setter
  29. def qty(self, value):
  30. if value >= 0:
  31. self._qty = value
  32. @property
  33. def cost(self):
  34. return self._prod.price * self._qty
  35.  
  36. def __str__(self):
  37. return "{} {} at ${}".format(self._prod.code, self._qty, self.cost)
  38. '''
  39. Sorry, please add one property/method to CartItem class.
  40. OK everyone??
  41. '''
  42. ######################
  43. class ShoppingCart:
  44. def __init__(self, shopperName):
  45. self._shopper = shopperName # As stated in the questioni
  46. self._items = {} # No CartItem initially
  47.  
  48. def add(self, cartItem):
  49. if cartItem.pCode in self._items.keys(): # Already in the shopping cart
  50. print("Item already exist")
  51. else:
  52. self._items[cartItem.pCode] = cartItem # Add to the dictionary
  53.  
  54. def showAll(self):
  55. print("Item(s) in the shopping cart:")
  56. for cItem in self._items.values():
  57. print(cItem)
  58. def remove(self, pCode):
  59. if pCode in self._items.keys():
  60. del self._items[pCode] # Remove from collection
  61.  
  62. def search(self, pCode):
  63. if pCode in self._items.keys():
  64. return self._items[pCode]
  65. else:
  66. return None # As stated in the question
  67. def getTotal(self):
  68. totalAmt = 0
  69. for eachItem in self._items.values():
  70. totalAmt += eachItem.cost # Add up each cartItem
  71. return totalAmt
  72.  
  73. ######################
  74. def main():
  75. # Quick test
  76. p1 = Product('p1', 'desc 1', 10)
  77. p2 = Product('p2', 'desc 2', 20)
  78. c1 = CartItem(p1, 10) # 10 units of Product: p1
  79. c2 = CartItem(p2, 20) # 20 units of Product: p2
  80. sCart = ShoppingCart("John") # One instance of ShoppingCart
  81. sCart.add(c1) # c1 go into the shopping cart
  82. sCart.add(c2) # Add another item into the shopping cart
  83. sCart.showAll() # Print items in the shopping cart
  84. # Next, remove cartItem from the shopping cart
  85. sCart.remove('p1') # Remove product 'p1' from the shopping cart
  86. sCart.showAll() # Check again to see if the item is removed
  87. # Next, search method
  88. cItem = sCart.search('p2') # Look for cartItem containing Product: 'p2'
  89. if cItem is None:
  90. print("Not in the shopping cart")
  91. else:
  92. print(cItem)
  93. cItem = sCart.search('p3') # Look for cartItem containing Product: 'p3'
  94. if cItem is None:
  95. print("Not in the shopping cart")
  96. else:
  97. print(cItem)
  98. # Last method, get the total cost
  99. total = sCart.getTotal()
  100. print(f"Total is ${total}")
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement