Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace InventoryManagementSystem
{
public interface IProductManager
{
Product GetProductById(int productId);
List<Product> GetProducts();
void AddProduct(Product product);
void UpdateProduct(Product product);
void DeleteProduct(Product product);
void SellProduct(Product product, int quantity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
namespace InventoryManagementSystem
{
public class InventoryManagementController
{
private IProductManager manager;

public InventoryManagementController(IProductManager manager)
{
this.manager = manager;
}

public void Start()
{
Console.WriteLine("Welcome to Inventory Management System");
while (true)
{
DisplayMenu();
string choice = Console.ReadLine();
ProcessChoice(choice);
}
}

private void DisplayMenu()
{
Console.WriteLine("\nEnter operation to perform:");
Console.WriteLine("1. View Inventory");
Console.WriteLine("2. Add Product");
Console.WriteLine("3. Update Product");
Console.WriteLine("4. Delete Product");
Console.WriteLine("5. Sell Product");
Console.WriteLine("6. Exit");
Console.Write("Enter your choice: ");
}

private void ProcessChoice(string choice)
{
switch (choice)
{
case "1":
ViewInventory();
break;
case "2":
AddProduct();
break;
case "3":
UpdateProduct();
break;
case "4":
DeleteProduct();
break;
case "5":
SellProduct();
break;
case "6":
Console.WriteLine("Exiting program...");
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
break;
}
}

private void ViewInventory()
{
try
{
var products = manager.GetProducts();
if (products.Count == 0)
{
Console.WriteLine("\nInventory is empty!");
}
else
{
Console.WriteLine("\nList of all Products:");
foreach (var product in products)
{
Console.WriteLine($"ID: {product.Id}\nName: {product.Name}\nPrice: {product.Price}\nQuantity: {product.Quantity}\n");
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

private void AddProduct()
{
try
{
Console.WriteLine("\nEnter details of the product:");
Console.Write("ID: ");
int id = ReadIntFromConsole("ID");
Console.Write("Name: ");
string name = ReadStringFromConsole("Name");
Console.Write("Price: ");
double price = ReadDoubleFromConsole("Price");
Console.Write("Quantity: ");
int quantity = ReadIntFromConsole("Quantity");

manager.AddProduct(new Product { Id = id, Name = name, Price = price, Quantity = quantity });
Console.WriteLine("Product added successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

private void UpdateProduct()
{
try
{
Console.Write("\nEnter ID of the product to update: ");
int id = ReadIntFromConsole("ID");

var productToUpdate = manager.GetProductById(id);

Console.Write("New Name (press Enter to keep current): ");
string name = Console.ReadLine();
if (name == null)
{
name = productToUpdate.Name;
}

Console.Write("New Price (press Enter to keep current): ");
string priceInput = Console.ReadLine();
double? price = null;
if (!string.IsNullOrWhiteSpace(priceInput))
{
price = double.Parse(priceInput);
}
else
{
price = productToUpdate.Price;
}

Console.Write("New Quantity (press Enter to keep current): ");
string quantityInput = Console.ReadLine();
int? quantity = null;
if (!string.IsNullOrWhiteSpace(quantityInput))
{
quantity = int.Parse(quantityInput);
}
else
{
quantity = productToUpdate.Quantity;
}

manager.UpdateProduct(new Product { Id = id, Name = name, Price = (double)price, Quantity = (int)quantity });
Console.WriteLine("Product updated successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

private void DeleteProduct()
{
try
{
Console.Write("\nEnter ID of the product to delete: ");
int id = ReadIntFromConsole("ID");

var product = manager.GetProductById(id);

manager.DeleteProduct(product);
Console.WriteLine("Product deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

private void SellProduct()
{
try
{
Console.Write("\nEnter ID of the product to sell: ");
int id = ReadIntFromConsole("ID");
Console.Write("Enter quantity to sell: ");
int quantity = ReadIntFromConsole("Quantity");

var product = manager.GetProductById(id);

manager.SellProduct(product, quantity);
Console.WriteLine("Product sold successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

private int ReadIntFromConsole(string fieldName)
{
string input = Console.ReadLine();
if (!int.TryParse(input, out int result))
{
throw new ArgumentException($"{fieldName} must be an integer.");
}
return result;
}

private double ReadDoubleFromConsole(string fieldName)
{
string input = Console.ReadLine();
if (!double.TryParse(input, out double result))
{
throw new ArgumentException($"{fieldName} must be a valid number.");
}
return result;
}

private string ReadStringFromConsole(string fieldName)
{
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
throw new ArgumentException($"{fieldName} is required.");
}
return input;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace InventoryManagementSystem
{
public class DuplicateProductException : Exception
{
public DuplicateProductException(string message) : base(message) { }
}

public class ProductNotFoundException : Exception
{
public ProductNotFoundException(string message) : base(message) { }
}

public class InsufficientQuantityException : Exception
{
public InsufficientQuantityException(string message) : base(message) { }
}

public class InvalidDataException : Exception
{
public InvalidDataException(string message) : base(message) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InventoryManagementSystem", "InventoryManagementSystem.csproj", "{1B05856A-36E1-4658-8124-1209E781ECD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B05856A-36E1-4658-8124-1209E781ECD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B05856A-36E1-4658-8124-1209E781ECD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B05856A-36E1-4658-8124-1209E781ECD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B05856A-36E1-4658-8124-1209E781ECD8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {105F8084-48FF-445A-A58B-982EB594CF11}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions InventoryManagementSystem/InventoryManagementSystem/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace InventoryManagementSystem
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace InventoryManagementSystem
{
public class ProductManager : IProductManager
{
private readonly List<Product> productInventory = new List<Product>();

public Product GetProductById(int productId)
{
var product = productInventory.Find(p => p.Id == productId) ?? throw new ProductNotFoundException("Product with this ID not found.");
return product;
}

public List<Product> GetProducts()
{
return productInventory;
}

public void AddProduct(Product product)
{
if (productInventory.Exists(p => p.Id == product.Id))
{
throw new DuplicateProductException("Product with the same ID already exists.");
}

ValidateProductData(product);

productInventory.Add(product);
}

public void UpdateProduct(Product product)
{
var existingProduct = GetProductById(product.Id);
ValidateProductData(product);

existingProduct.Name = product.Name ?? existingProduct.Name;
existingProduct.Price = product.Price >= 0 ? product.Price : existingProduct.Price;
existingProduct.Quantity = product.Quantity >= 0 ? product.Quantity : existingProduct.Quantity;
}

public void DeleteProduct(Product product)
{
productInventory.Remove(product);
}

public void SellProduct(Product product, int quantity)
{
if (quantity <= 0)
{
throw new InvalidDataException("Invalid quantity. Quantity must be greater than zero.");
}

if (quantity > product.Quantity)
{
throw new InsufficientQuantityException("Insufficient quantity for sale.");
}

if (product.Quantity == 0)
{
throw new InsufficientQuantityException("Cannot sell. Product is out of stock.");
}

product.Quantity -= quantity;
}

private void ValidateProductData(Product product)
{
if (product.Price < 0)
{
throw new InvalidDataException("Price of product cannot be negative.");
}

if (product.Quantity < 0)
{
throw new InvalidDataException("Quantity cannot be negative.");
}
}
}
}
Loading