Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- """
- Create an application which manages an inventory of products.
- Create a product class which has a price, id, and quantity on hand.
- Then create an inventory class which keeps track of various products
- and can sum up the inventory value.
- """
- class Products():
- def __init__(self, name, price):
- self.name = name
- self.price = price
- self.products = {}
- self.products.setdefault(self.name, self.price)
- def str(self):
- print(f"{self.name} have a price of ${str(self.price)} each")
- class Inventory():
- def __init__(self):
- self.inventory = {}
- def addToInventory(self, product, quantity):
- self.inventory.setdefault(product, quantity)
- print(f'{product} added to inventory')
- def printInventory(self):
- for product, quantity in self.inventory.items():
- print(f'\nITEMS IN INVENTORY \n{product}: {quantity}')
- class Cart():
- def __init__(self):
- self.cart = {}
- def addToCart(self, product, quantity):
- self.cart.setdefault(product, quantity)
- print(f'{product} added to cart')
- # @property ??
- def valueCart(self):
- for product, quantity in self.cart.items():
- return sum(self.products[product]*quantity)
- def main():
- bananas = Products("bananas", 3)
- bananas.str()
- apples = Products("apples", 2)
- inventory = Inventory()
- inventory.addToInventory("apples", 4)
- inventory.printInventory()
- cart = Cart()
- cart.addToCart("bananas", 3)
- cart.addToCart("apples", 2)
- print(cart.valueCart())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement