Untitled
unknown
plain_text
2 years ago
1.7 kB
4
Indexable
class Product: def __init__(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity class OnlineStore: def __init__(self): self.products = [] def add_product(self, product): self.products.append(product) def remove_product(self, product): self.products.remove(product) def display_products(self): for product in self.products: print(f"Name: {product.name}, Price: ${product.price}, Quantity: {product.quantity}") def search_product(self, name): for product in self.products: if product.name == name: return product return None def sell_product(self, name, quantity): product = self.search_product(name) if product: if product.quantity >= quantity: product.quantity -= quantity print(f"Sold {quantity} {product.name}(s) for ${product.price * quantity}") else: print(f"Insufficient quantity of {product.name}") else: print(f"Product '{name}' not found") # Create an instance of the OnlineStore store = OnlineStore() # Add some products to the store store.add_product(Product("iPhone", 999, 10)) store.add_product(Product("MacBook", 1999, 5)) store.add_product(Product("iPad", 499, 20)) # Display all products in the store store.display_products() # Sell some products store.sell_product("iPhone", 3) store.sell_product("MacBook", 1) store.sell_product("iMac", 2) # Product not found # Display updated product quantities store.display_products()
Editor is loading...