Question: # Global data products = [ { id : 1 , name: Laptop, price: 1 4 9 9 } , { id

# Global data products =[{"id": 1, "name": "Laptop", "price": 1499},{"id": 2, "name": "Smartphone", "price": 899},{"id": 3, "name": "Headphones", "price": 249},] cart =[] # Functions def display_products(): print("Available Products:") for product in products: print(f"{product['id']}.{product['name']}- ${product['price']}") def add_to_cart(product_id): product = next((p for p in products if p["id"]== product_id), None) if product: cart.append(product) print(f"{product['name']} added to cart.") else: print("Product not found.") def display_cart(): print("Shopping Cart:") for item in cart: print(f"{item['name']}- ${item['price']}") def calculate_total(): total = sum(item["price"] for item in cart) print(f"Total: ${total}") # Main program while True: print("
1. Display Products
2. Add to Cart
3. Display Cart
4. Calculate Total
5. Exit") choice = input("Enter your choice: ") if choice =="1": display_products() elif choice =="2": product_id = int(input("Enter the product ID to add to cart: ")) add_to_cart(product_id) elif choice =="3": display_cart() elif choice =="4": calculate_total() elif choice =="5": print("Thank you for shopping. Goodbye!") break else: print("Invalid choice. Please try again.") Change this code into object oriented approach

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!