-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 5 (Inventory Management System with Exception Handling) #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mukulpalol16
wants to merge
3
commits into
main
Choose a base branch
from
Assignment5.2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
InventoryManagementSystem/InventoryManagementSystem/IProductManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
228 changes: 228 additions & 0 deletions
228
InventoryManagementSystem/InventoryManagementSystem/InventoryManagementController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
InventoryManagementSystem/InventoryManagementSystem/InventoryManagementExceptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
25 changes: 25 additions & 0 deletions
25
InventoryManagementSystem/InventoryManagementSystem/InventoryManagementSystem.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
10
InventoryManagementSystem/InventoryManagementSystem/Product.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
InventoryManagementSystem/InventoryManagementSystem/ProductManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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."); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.