Advertisement
Guest User

Untitled

a guest
May 26th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #! python3
  2.  
  3. """
  4. Create an application which manages an inventory of products.
  5. Create a product class which has a price, id, and quantity on hand.
  6. Then create an inventory class which keeps track of various products
  7. and can sum up the inventory value.
  8. """
  9.  
  10. class Products():
  11. def __init__(self, name, price):
  12. self.name = name
  13. self.price = price
  14. self.products = {}
  15. self.products.setdefault(self.name, self.price)
  16.  
  17. def str(self):
  18. print(f"{self.name} have a price of ${str(self.price)} each")
  19.  
  20. class Inventory():
  21. def __init__(self):
  22. self.inventory = {}
  23.  
  24. def addToInventory(self, product, quantity):
  25. self.inventory.setdefault(product, quantity)
  26. print(f'{product} added to inventory')
  27.  
  28. def printInventory(self):
  29. for product, quantity in self.inventory.items():
  30. print(f'\nITEMS IN INVENTORY \n{product}: {quantity}')
  31.  
  32. class Cart():
  33. def __init__(self):
  34. self.cart = {}
  35.  
  36. def addToCart(self, product, quantity):
  37. self.cart.setdefault(product, quantity)
  38. print(f'{product} added to cart')
  39.  
  40. # @property ??
  41. def valueCart(self):
  42. for product, quantity in self.cart.items():
  43. return sum(self.products[product]*quantity)
  44.  
  45. def main():
  46. bananas = Products("bananas", 3)
  47. bananas.str()
  48. apples = Products("apples", 2)
  49. inventory = Inventory()
  50. inventory.addToInventory("apples", 4)
  51. inventory.printInventory()
  52.  
  53. cart = Cart()
  54. cart.addToCart("bananas", 3)
  55. cart.addToCart("apples", 2)
  56. print(cart.valueCart())
  57.  
  58.  
  59. if __name__ == '__main__':
  60. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement