diff --git a/InventoryManagementSystem/InventoryManagementSystem/IProductManager.cs b/InventoryManagementSystem/InventoryManagementSystem/IProductManager.cs new file mode 100644 index 0000000..a56d2d1 --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/IProductManager.cs @@ -0,0 +1,12 @@ +namespace InventoryManagementSystem +{ + public interface IProductManager + { + Product GetProductById(int productId); + List GetProducts(); + void AddProduct(Product product); + void UpdateProduct(Product product); + void DeleteProduct(Product product); + void SellProduct(Product product, int quantity); + } +} diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs new file mode 100644 index 0000000..0fbcaed --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs @@ -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; + } + } +} diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs new file mode 100644 index 0000000..913f585 --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs @@ -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) { } + } +} diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.csproj b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.sln b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.sln new file mode 100644 index 0000000..d66963a --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.sln @@ -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 diff --git a/InventoryManagementSystem/InventoryManagementSystem/Product.cs b/InventoryManagementSystem/InventoryManagementSystem/Product.cs new file mode 100644 index 0000000..e114f93 --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/Product.cs @@ -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; } + } +} diff --git a/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs new file mode 100644 index 0000000..bc2c2b1 --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs @@ -0,0 +1,78 @@ +namespace InventoryManagementSystem +{ + public class ProductManager : IProductManager + { + private readonly List productInventory = new List(); + + 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 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."); + } + } + } +} diff --git a/InventoryManagementSystem/InventoryManagementSystem/Program.cs b/InventoryManagementSystem/InventoryManagementSystem/Program.cs new file mode 100644 index 0000000..3c2196c --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/Program.cs @@ -0,0 +1,19 @@ +namespace InventoryManagementSystem +{ + internal class Program + { + static void Main(string[] args) + { + try + { + IProductManager manager = new ProductManager(); + InventoryManagementController controller = new InventoryManagementController(manager); + controller.Start(); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } + } +} diff --git a/InventoryManagementSystem/README.md b/InventoryManagementSystem/README.md new file mode 100644 index 0000000..c9e727c --- /dev/null +++ b/InventoryManagementSystem/README.md @@ -0,0 +1,15 @@ +# Assignemnt Title: Inventory Management System with Exception Handling + +### Objective: +Develop an inventory management system for a small business that handles various operations such as adding, updating, and deleting products from the inventory. The system should implement proper exception handling. + +### Requirements: +1. Create a Product class with attributes such as id, name, price, and quantity. +2. Implement a ProductManager class responsible for managing the inventory operations, including adding, updating, and deleting products.\ +Handle exceptions such as: + - Invalid input data (e.g., negative price or quantity) + - Product not found during update or deletion + - Insufficient quantity during product sale + - DuplicateProductException +3. Implement a user interface (console-based or GUI) to interact with the inventory management system. +4. Use Custom Exception as much as you can do. \ No newline at end of file