{}
run-icon
main.py
class Product: def __init__(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity def __str__(self): return f"{self.name} - ${self.price:.2f} (Quantity: {self.quantity})" class ShoppingCart: def __init__(self): self.items = [] def add_item(self, product, quantity): if product.quantity >= quantity: self.items.append((product, quantity)) product.quantity -= quantity print(f"Added {quantity} {product.name} to your cart.") else: print(f"Not enough {product.name} in stock.") def remove_item(self, product_name): for i, (product, quantity) in enumerate(self.items): if product.name == product_name: del self.items[i] product.quantity += quantity print(f"Removed {product_name} from your cart.") return print(f"Product '{product_name}' not found in your cart.") def view_cart(self): if not self.items: print("Your cart is empty.") return print("Your cart contains:") for product, quantity in self.items: print(f"- {quantity} {product}") def calculate_total(self): total = 0 for product, quantity in self.items: total += product.price * quantity return total def main(): products = [ Product("Apple", 0.50, 10), Product("Banana", 0.25, 20), Product("Milk", 2.99, 15), Product("Bread", 2.49, 12), ] cart = ShoppingCart() while True: print("\nShopping App:") print("1. View Products") print("2. Add to Cart") print("3. Remove from Cart") print("4. View Cart") print("5. Calculate Total") print("6. Exit") while True: choice = input("Enter your choice: ") if choice in ["1", "2", "3", "4", "5", "6"]: if choice == '1': print("\nAvailable Products:") for i, product in enumerate(products): print(f"{i + 1}. {product}") input("\nEnter to conntinue...") elif choice == '2': try: product_index = int(input("Enter product number: ")) if 0 <= product_index < len(products): product = products[product_index] quantity = int(input("Enter quantity: ")) cart.add_item(product, quantity) else: print("Invalid product number.") except ValueError: print("Please enter a valid number.") elif choice == '3': product_name = input("Enter product name to remove: ") cart.remove_item(product_name) elif choice == '4': cart.view_cart() elif choice == '5': total = cart.calculate_total() print(f"Total cost: ${total:.2f}") elif choice == '6': print("Exiting...") break # Exit the loop else: print("Invalid choice. Please enter a number from 1 to 6.") if __name__ == "__main__": cart = ShoppingCart() main() #code made by me. i don't know what to write here #i just wanted to write sonmething here as i didn't want to end in a prime number
Output