You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
4.0 KiB
122 lines
4.0 KiB
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 ShowInventory()
|
|
{
|
|
using (var context = new InventoryContext())
|
|
{
|
|
Console.WriteLine("********** Current Inventory **********");
|
|
foreach (var product in context.Products)
|
|
{
|
|
Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}");
|
|
}
|
|
Console.WriteLine("****************************************");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|