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.

159 lines
5.4 KiB

2 years ago
using Microsoft.EntityFrameworkCore;
using System;
2 years ago
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})");
}
}
}
2 years ago
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.");
}
}
2 years ago
public void ShowInventory()
{
using (var context = new InventoryContext())
{
Console.WriteLine("********** Current Inventory **********");
2 years ago
Console.WriteLine("***** Products *****");
2 years ago
foreach (var product in context.Products)
{
Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}");
}
2 years ago
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("*********************");
2 years ago
Console.WriteLine("****************************************");
}
}
}
}