using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InventoryManagement.Components { internal class InventoryManager { public void AddProduct(Product product) { using (var context = new InventoryContext()) { if (context.Products.Any(p => p.Name == product.Name)) { Console.WriteLine("Product with the same name already exists."); return; } context.Products.Add(product); context.SaveChanges(); Console.WriteLine($"{product.Name} added."); } } public void BuyProduct(string name, int quantity) { using (var context = new InventoryContext()) { var product = context.Products.FirstOrDefault(p => p.Name == name); if (product == null) { Console.WriteLine($"Cannot find the product named {name}"); return; } product.Quantity += quantity; context.SaveChanges(); Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); } } public void BuyProduct(int id, int quantity) { using (var context = new InventoryContext()) { var product = context.Products.Find(id); if (product == null) { Console.WriteLine("Product not found."); return; } product.Quantity += quantity; context.SaveChanges(); Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); } } public void SellProduct(string name, int quantity) { using (var context = new InventoryContext()) { var product = context.Products.FirstOrDefault(p => p.Name == name); if (product == null) { Console.WriteLine($"Cannot find the product named {name}"); return; } if (product.Quantity >= quantity) { product.Quantity -= quantity; context.SaveChanges(); Console.WriteLine($"{quantity} units of {product.Name} sold."); } else { Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); } } } public void SellProduct(int id, int quantity) { using (var context = new InventoryContext()) { var product = context.Products.Find(id); if (product == null) { Console.WriteLine("Product not found."); return; } if (product.Quantity >= quantity) { product.Quantity -= quantity; context.SaveChanges(); Console.WriteLine($"{quantity} units of {product.Name} sold."); } else { Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); } } } public void AddAccessary(int productId, string name, string description) { using (var context = new InventoryContext()) { var product = context.Products.Find(productId); if (product == null) { Console.WriteLine("Product not found."); return; } context.Entry(product).Collection(e => e.CompatibleAccessaries).Load(); Accessary acc = new Accessary() { ProductId = productId, Name = name, Description = description, }; product.CompatibleAccessaries.Add(acc); context.SaveChanges(); Console.WriteLine($"{acc.Name} added."); } } public void ShowInventory() { using (var context = new InventoryContext()) { Console.WriteLine("********** Current Inventory **********"); Console.WriteLine("***** Products *****"); foreach (var product in context.Products) { Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}"); } Console.WriteLine("********************"); Console.WriteLine(); Console.WriteLine("***** Accesarry *****"); foreach (var accesarry in context.Accessaries.Include(e => e.CompatibleProduct)) { Console.WriteLine($"ID: {accesarry.Id}, Name: {accesarry.Name}, Compatible product: {accesarry.CompatibleProduct.Name}"); } Console.WriteLine("*********************"); Console.WriteLine("****************************************"); } } } }