using System.Collections.Generic;
public static void Main()
List<Product> products = new List<Product>
new Product { Id = 1, Name = "Laptop", Price = 999.99M },
new Product { Id = 2, Name = "Smartphone", Price = 699.99M },
new Product { Id = 3, Name = "Tablet", Price = 399.99M }
List<CartItem> cart = new List<CartItem>();
Console.WriteLine("Available Products:");
foreach (var product in products)
Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Price: ${product.Price}");
Console.WriteLine("\nEnter Product ID to add to cart (or 'done' to finish, 'remove' to remove an item):");
input = Console.ReadLine();
if (input.ToLower() == "done")
if (input.ToLower() == "remove")
Console.WriteLine("Enter Product ID to remove from cart:");
string removeInput = Console.ReadLine();
if (int.TryParse(removeInput, out int removeProductId))
var itemToRemove = cart.Find(item => item.Product.Id == removeProductId);
if (itemToRemove != null)
if (itemToRemove.Quantity > 1)
Console.WriteLine($"{itemToRemove.Product.Name} quantity decreased by 1.");
cart.Remove(itemToRemove);
Console.WriteLine($"{itemToRemove.Product.Name} removed from cart.");
Console.WriteLine("Product not found in cart.");
Console.WriteLine("Invalid input. Please enter a valid product ID.");
if (int.TryParse(input, out int productId))
var product = products.Find(p => p.Id == productId);
AddToCart(cart, product);
Console.WriteLine($"{product.Name} added to cart.");
Console.WriteLine("Product not found.");
Console.WriteLine("Invalid input. Please enter a valid product ID.");
Console.WriteLine("\nYour Cart:");
foreach (var item in cart)
Console.WriteLine($"{item.Product.Name} - Quantity: {item.Quantity} - Total: ${item.Quantity * item.Product.Price}");
decimal total = CalculateTotal(cart);
Console.WriteLine($"\nTotal Amount: ${total}");
public static void AddToCart(List<CartItem> cart, Product product)
var existingItem = cart.Find(item => item.Product.Id == product.Id);
if (existingItem != null)
cart.Add(new CartItem { Product = product, Quantity = 1 });
public static decimal CalculateTotal(List<CartItem> cart)
foreach (var item in cart)
total += item.Product.Price * item.Quantity;
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public Product Product { get; set; }
public int Quantity { get; set; }