From e141d16a150d67a4f8cadee1242a506357baca97 Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Wed, 24 Apr 2024 09:10:46 +0530 Subject: [PATCH 1/3] added InventoryManagementSystem assignment --- .../IProductManager.cs | 12 + .../InventoryManagementController.cs | 232 ++++++++++++++++++ .../InventoryManagementExceptions.cs | 22 ++ .../InventoryManagementSystem.csproj | 10 + .../InventoryManagementSystem.sln | 25 ++ .../InventoryManagementSystem/Product.cs | 10 + .../ProductManager.cs | 71 ++++++ .../InventoryManagementSystem/Program.cs | 19 ++ InventoryManagementSystem/README.md | 15 ++ 9 files changed, 416 insertions(+) create mode 100644 InventoryManagementSystem/InventoryManagementSystem/IProductManager.cs create mode 100644 InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs create mode 100644 InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs create mode 100644 InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.csproj create mode 100644 InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.sln create mode 100644 InventoryManagementSystem/InventoryManagementSystem/Product.cs create mode 100644 InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs create mode 100644 InventoryManagementSystem/InventoryManagementSystem/Program.cs create mode 100644 InventoryManagementSystem/README.md 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..e4d44db --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs @@ -0,0 +1,232 @@ +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); + Console.WriteLine("Name: " + product.Name); + Console.WriteLine("Price: " + product.Price); + Console.WriteLine("Quantity: " + product.Quantity); + Console.WriteLine(); + } + } + } + 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..1acfaf8 --- /dev/null +++ b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs @@ -0,0 +1,71 @@ +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."); + } + + if (product.Price < 0) + { + throw new InvalidDataException("Price of product cannot be negative."); + } + + if (product.Quantity < 0) + { + throw new InvalidDataException("Quantity cannot be negative."); + } + + productInventory.Add(product); + } + + public void UpdateProduct(Product product) + { + var existingProduct = GetProductById(product.Id); + if (product.Price < 0) + { + throw new InvalidDataException("Price of product cannot be negative."); + } + + if (product.Quantity < 0) + { + throw new InvalidDataException("Quantity cannot be negative."); + } + + existingProduct.Name = product.Name; + existingProduct.Price = product.Price; + existingProduct.Quantity = product.Quantity; + } + + public void DeleteProduct(Product product) + { + productInventory.Remove(product); + } + + public void SellProduct(Product product, int quantity) + { + if (quantity > product.Quantity) + { + throw new InsufficientQuantityException("Insufficient quantity for sale."); + } + + product.Quantity -= quantity; + } + } +} 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 From 701fcb9f50c7391e0a0f318c7f1b1980869a18a7 Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Tue, 30 Apr 2024 09:46:40 +0530 Subject: [PATCH 2/3] addressed review comments --- .../InventoryManagementController.cs | 6 +-- .../InventoryManagementExceptions.cs | 5 ++ .../ProductManager.cs | 49 +++++++++++-------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs index e4d44db..0fbcaed 100644 --- a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs @@ -75,11 +75,7 @@ private void ViewInventory() Console.WriteLine("\nList of all Products:"); foreach (var product in products) { - Console.WriteLine("ID: " + product.Id); - Console.WriteLine("Name: " + product.Name); - Console.WriteLine("Price: " + product.Price); - Console.WriteLine("Quantity: " + product.Quantity); - Console.WriteLine(); + Console.WriteLine($"ID: {product.Id}\nName: {product.Name}\nPrice: {product.Price}\nQuantity: {product.Quantity}\n"); } } } diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs index 913f585..12d4d0a 100644 --- a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs @@ -19,4 +19,9 @@ public class InvalidDataException : Exception { public InvalidDataException(string message) : base(message) { } } + + public class InvalidQuantityException : Exception + { + public InvalidQuantityException(string message) : base(message) { } + } } diff --git a/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs index 1acfaf8..2553acf 100644 --- a/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs +++ b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs @@ -22,15 +22,7 @@ public void AddProduct(Product product) throw new DuplicateProductException("Product with the same ID already exists."); } - if (product.Price < 0) - { - throw new InvalidDataException("Price of product cannot be negative."); - } - - if (product.Quantity < 0) - { - throw new InvalidDataException("Quantity cannot be negative."); - } + ValidateProductData(product); productInventory.Add(product); } @@ -38,19 +30,11 @@ public void AddProduct(Product product) public void UpdateProduct(Product product) { var existingProduct = GetProductById(product.Id); - if (product.Price < 0) - { - throw new InvalidDataException("Price of product cannot be negative."); - } - - if (product.Quantity < 0) - { - throw new InvalidDataException("Quantity cannot be negative."); - } + ValidateProductData(product); - existingProduct.Name = product.Name; - existingProduct.Price = product.Price; - existingProduct.Quantity = product.Quantity; + 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) @@ -60,12 +44,35 @@ public void DeleteProduct(Product product) public void SellProduct(Product product, int quantity) { + if (quantity <= 0) + { + throw new InvalidQuantityException("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."); + } + } } } From 2b25db985f3dca92dd3ae1411a3e3e50d6dc671c Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Tue, 30 Apr 2024 11:57:55 +0530 Subject: [PATCH 3/3] refactored code --- .../InventoryManagementExceptions.cs | 5 ----- .../InventoryManagementSystem/ProductManager.cs | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs index 12d4d0a..913f585 100644 --- a/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs +++ b/InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs @@ -19,9 +19,4 @@ public class InvalidDataException : Exception { public InvalidDataException(string message) : base(message) { } } - - public class InvalidQuantityException : Exception - { - public InvalidQuantityException(string message) : base(message) { } - } } diff --git a/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs index 2553acf..bc2c2b1 100644 --- a/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs +++ b/InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs @@ -46,7 +46,7 @@ public void SellProduct(Product product, int quantity) { if (quantity <= 0) { - throw new InvalidQuantityException("Invalid quantity. Quantity must be greater than zero."); + throw new InvalidDataException("Invalid quantity. Quantity must be greater than zero."); } if (quantity > product.Quantity)