diff --git a/InventoryApp.cs b/InventoryApp.cs new file mode 100644 index 0000000..a6e8aa1 --- /dev/null +++ b/InventoryApp.cs @@ -0,0 +1,152 @@ +namespace InventoryManagementSystem +{ + public class InventoryApp + { + public static void Main(string[] args) + { + ProductManager manager = new ProductManager(); + + while (true) + { + Console.WriteLine("Inventory Management System"); + Console.WriteLine("1. Add Product"); + Console.WriteLine("2. Update Product"); + Console.WriteLine("3. Delete Product"); + Console.WriteLine("4. Display Inventory"); + Console.WriteLine("5. Sale Product"); + Console.WriteLine("6. Exit"); + Console.WriteLine("Enter your choice:"); + + int choice; + if (!int.TryParse(Console.ReadLine(), out choice)) + { + Console.WriteLine("Invalid input. Please enter a number."); + continue; + } + + switch (choice) + { + case 1: + AddProduct(manager); + break; + case 2: + UpdateProduct(manager); + break; + case 3: + DeleteProduct(manager); + break; + case 4: + manager.DisplayInventory(); + break; + case 5: + SellProduct(manager); + break; + case 6: + Environment.Exit(0); + break; + default: + Console.WriteLine("Invalid choice. Please enter a number between 1 and 5."); + break; + } + } + } + + private static void AddProduct(ProductManager manager) + { + try + { + Console.WriteLine("Enter product details:"); + Console.Write("ID: "); + int id = int.Parse(Console.ReadLine()); + Console.Write("Name: "); + string name = Console.ReadLine(); + Console.Write("Price: "); + double price = double.Parse(Console.ReadLine()); + Console.Write("Quantity: "); + int quantity = int.Parse(Console.ReadLine()); + + manager.AddProduct(new Product { Id = id, Name = name, Price = price, Quantity = quantity }); + } + catch (FormatException) + { + Console.WriteLine("Invalid input format. Please enter valid data."); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + + private static void UpdateProduct(ProductManager manager) + { + try + { + Console.WriteLine("Enter product ID to update:"); + int id = int.Parse(Console.ReadLine()); + Console.WriteLine("Enter new product details:"); + Console.Write("Name: "); + string name = Console.ReadLine(); + Console.Write("Price: "); + double price = double.Parse(Console.ReadLine()); + Console.Write("Quantity: "); + int quantity = int.Parse(Console.ReadLine()); + + manager.UpdateProduct(new Product { Id = id, Name = name, Price = price, Quantity = quantity }); + } + catch (FormatException) + { + Console.WriteLine("Invalid input format. Please enter valid data."); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + + private static void DeleteProduct(ProductManager manager) + { + try + { + Console.WriteLine("Enter product ID to delete:"); + int id = int.Parse(Console.ReadLine()); + manager.DeleteProduct(id); + } + catch (FormatException) + { + Console.WriteLine("Invalid input format. Please enter valid data."); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + + private static void SellProduct(ProductManager manager) + { + try + { + Console.WriteLine("Enter Product ID:"); + int id; + if (!int.TryParse(Console.ReadLine(), out id)) + { + Console.WriteLine("Invalid input for ID. Please enter a number."); + return; + } + + Console.WriteLine("Enter Quantity to Sell:"); + int quantity; + if (!int.TryParse(Console.ReadLine(), out quantity) || quantity <= 0) + { + Console.WriteLine("Invalid input for Quantity. Please enter a positive integer."); + return; + } + + manager.SellProduct(id, quantity); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} diff --git a/InventoryExceptions.cs b/InventoryExceptions.cs new file mode 100644 index 0000000..fff2b4d --- /dev/null +++ b/InventoryExceptions.cs @@ -0,0 +1,44 @@ +namespace InventoryManagementSystem +{ + public class DuplicateProductException : Exception + { + public DuplicateProductException(string message) : base(message) + { + } + } + + public class InvalidPriceException : Exception + { + public InvalidPriceException(string message) : base(message) + { + } + } + + public class InvalidInputDataException : Exception + { + public InvalidInputDataException(string message) : base(message) + { + } + } + + public class OutOfStockException : Exception + { + public OutOfStockException(string message) : base(message) + { + } + } + + public class ProductNotFoundException : Exception + { + public ProductNotFoundException(string message) : base(message) + { + } + } + + public class InsufficientQuantityException : Exception + { + public InsufficientQuantityException(string message) : base(message) + { + } + } +} \ No newline at end of file diff --git a/Product.cs b/Product.cs new file mode 100644 index 0000000..05af972 --- /dev/null +++ b/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; } + } +} \ No newline at end of file diff --git a/ProductManager.cs b/ProductManager.cs new file mode 100644 index 0000000..1d6d0cc --- /dev/null +++ b/ProductManager.cs @@ -0,0 +1,83 @@ +namespace InventoryManagementSystem +{ + public class ProductManager + { + private List inventory = new List(); + + public void AddProduct(Product product) + { + if (inventory.Exists(p => p.Id == product.Id)) + { + throw new DuplicateProductException($"Product with ID {product.Id} already exists."); + } + if (product.Id < 0 || product.Price < 0 || product.Quantity < 0) + { + throw new InvalidInputDataException("Id, Price and quantity cannot be negative."); + } + if (product.Price == 0) + { + throw new InvalidPriceException("Product price cannot be zero."); + } + + inventory.Add(product); + Console.WriteLine($"Product '{product.Name}' added successfully."); + } + + public void UpdateProduct(Product product) + { + int index = inventory.FindIndex(p => p.Id == product.Id); + if (index == -1) + { + throw new ProductNotFoundException($"Product with ID {product.Id} not found."); + } + if (product.Id < 0 || product.Price < 0 || product.Quantity < 0) + { + throw new InvalidInputDataException("Id, Price and quantity cannot be negative."); + } + + inventory[index] = product; + Console.WriteLine($"Product '{product.Name}' updated successfully."); + } + + public void DeleteProduct(int productId) + { + int index = inventory.FindIndex(p => p.Id == productId); + if (index == -1) + { + throw new ProductNotFoundException($"Product with ID {productId} not found."); + } + + inventory.RemoveAt(index); + Console.WriteLine($"Product with ID {productId} deleted successfully."); + } + + public void SellProduct(int productId, int quantityToSell) + { + Product product = inventory.Find(p => p.Id == productId); + if (product == null) + { + throw new ProductNotFoundException($"Product with ID {productId} not found."); + } + if (product.Quantity < quantityToSell) + { + if (product.Quantity == 0) + { + throw new OutOfStockException($"Product '{product.Name}' is currently out of stock."); + } + throw new InsufficientQuantityException($"Insufficient quantity of product '{product.Name}'. Available: {product.Quantity}"); + } + + product.Quantity -= quantityToSell; + Console.WriteLine($"Sold {quantityToSell} units of product '{product.Name}'."); + } + + public void DisplayInventory() + { + Console.WriteLine("Inventory:"); + foreach (var product in inventory) + { + Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Price: {product.Price}, Quantity: {product.Quantity}"); + } + } + } +} \ No newline at end of file