-
Notifications
You must be signed in to change notification settings - Fork 0
Inventory Management System #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
Open
PradeepITT
wants to merge
5
commits into
main
Choose a base branch
from
Inventory-Management-System
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.
Open
Changes from all commits
Commits
Show all changes
5 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
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,35 @@ | ||
| #ifndef CUSTOM_EXCEPTIONS_H | ||
| #define CUSTOM_EXCEPTIONS_H | ||
| using namespace std; | ||
| #include <exception> | ||
| #include <string> | ||
|
|
||
| class InvalidInputException : public exception { | ||
| public: | ||
| const char* what() const noexcept override { | ||
| return "Invalid input data"; | ||
| } | ||
| }; | ||
|
|
||
| class ProductNotFoundException : public exception { | ||
| public: | ||
| const char* what() const noexcept override { | ||
| return "Product not found"; | ||
| } | ||
| }; | ||
|
|
||
| class InsufficientQuantityException : public exception { | ||
| public: | ||
| const char* what() const noexcept override { | ||
| return "Insufficient quantity"; | ||
| } | ||
| }; | ||
|
|
||
| class DuplicateProductException : public exception { | ||
| public: | ||
| const char* what() const noexcept override { | ||
| return "Duplicate product"; | ||
| } | ||
| }; | ||
|
|
||
| #endif // CUSTOM_EXCEPTIONS_H |
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,18 @@ | ||
| #include "InventoryManager.h" | ||
| #include <iostream> | ||
|
|
||
| using namespace std; | ||
|
|
||
| void InventoryManager::displayInventory(const ProductCatalog& catalog) const { | ||
| const vector<Product>& products = catalog.getProducts(); | ||
|
|
||
| if (products.empty()) { | ||
| cout << "Inventory is empty." << endl; | ||
| } else { | ||
| cout << "Inventory:\n"; | ||
| cout << "ID\tName\tPrice \tQuantity\n"; | ||
| for (const Product& product : products) { | ||
| cout << product.getId() << "\t" << product.getName() << "\tRs " << product.getPrice() << "\t" << product.getQuantity() << endl; | ||
| } | ||
| } | ||
| } |
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,11 @@ | ||
| #ifndef INVENTORYMANAGER_H | ||
| #define INVENTORYMANAGER_H | ||
|
|
||
| #include "ProductCatalog.h" | ||
|
|
||
| class InventoryManager { | ||
| public: | ||
| void displayInventory(const ProductCatalog& catalog) const; | ||
| }; | ||
|
|
||
| #endif // INVENTORYMANAGER_H |
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,26 @@ | ||
| #include "Product.h" | ||
| #include <stdexcept> | ||
| using namespace std; | ||
|
|
||
| Product::Product(int _id, string _name, float _price, int _quantity) | ||
| : id(_id), name(_name), price(_price), quantity(_quantity) {} | ||
|
|
||
| int Product::getId() const { return id; } | ||
|
|
||
| string Product::getName() const { return name; } | ||
|
|
||
| float Product::getPrice() const { return price; } | ||
|
|
||
| int Product::getQuantity() const { return quantity; } | ||
|
|
||
| void Product::setPrice(float _price) { | ||
| if (_price < 0) | ||
| throw invalid_argument("Price cannot be negative"); | ||
| price = _price; | ||
| } | ||
|
|
||
| void Product::setQuantity(int _quantity) { | ||
| if (_quantity < 0) | ||
| throw invalid_argument("Quantity cannot be negative"); | ||
| quantity = _quantity; | ||
| } |
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,24 @@ | ||
| #ifndef PRODUCT_H | ||
| #define PRODUCT_H | ||
|
|
||
| #include <string> | ||
| using namespace std; | ||
|
|
||
| class Product { | ||
| private: | ||
| int id; | ||
| string name; | ||
| float price; | ||
| int quantity; | ||
|
|
||
| public: | ||
| Product(int _id, string _name, float _price, int _quantity); | ||
| int getId() const; | ||
| string getName() const; | ||
| float getPrice() const; | ||
| int getQuantity() const; | ||
| void setPrice(float _price); | ||
| void setQuantity(int _quantity); | ||
| }; | ||
|
|
||
| #endif // PRODUCT_H |
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,50 @@ | ||
| #include "ProductCatalog.h" | ||
| #include "Product.h" | ||
| #include "CustomExceptions.h" | ||
| #include <algorithm> | ||
| #include <stdexcept> | ||
| using namespace std; | ||
|
|
||
| void ProductCatalog::addProduct(const Product& product) { | ||
| for (const Product& existingProduct : products) { | ||
| if (existingProduct.getId() == product.getId()) { | ||
| throw DuplicateProductException(); | ||
| } | ||
| } | ||
| products.push_back(product); | ||
| } | ||
|
|
||
| void ProductCatalog::updateProduct(int id, float price, int quantity) { | ||
| auto productToUpdate = find_if(products.begin(), products.end(), [id](const Product& p) { return p.getId() == id; }); | ||
| if (productToUpdate == products.end()) { | ||
| throw ProductNotFoundException(); | ||
| } | ||
|
|
||
| productToUpdate->setPrice(price); | ||
| productToUpdate->setQuantity(quantity); | ||
| } | ||
|
|
||
| void ProductCatalog::deleteProduct(int id) { | ||
| auto productToDelete = remove_if(products.begin(), products.end(), [id](const Product& p) { return p.getId() == id; }); | ||
| if (productToDelete == products.end()) { | ||
| throw ProductNotFoundException(); | ||
| } | ||
| products.erase(productToDelete, products.end()); | ||
| } | ||
|
|
||
| void ProductCatalog::sellProduct(int id, int quantity) { | ||
| auto productToSell = find_if(products.begin(), products.end(), [id](const Product& p) { return p.getId() == id; }); | ||
| if (productToSell == products.end()) { | ||
| throw ProductNotFoundException(); | ||
| } | ||
|
|
||
| if (productToSell->getQuantity() < quantity) { | ||
| throw InsufficientQuantityException(); | ||
| } | ||
|
|
||
| productToSell->setQuantity(productToSell->getQuantity() - quantity); | ||
| } | ||
|
|
||
| const vector<Product>& ProductCatalog::getProducts() const { | ||
| return products; | ||
| } |
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,20 @@ | ||
| #ifndef PRODUCTCATALOG_H | ||
| #define PRODUCTCATALOG_H | ||
|
|
||
| #include "Product.h" | ||
| #include <vector> | ||
| using namespace std; | ||
|
|
||
| class ProductCatalog { | ||
| private: | ||
| vector<Product> products; | ||
|
|
||
| public: | ||
| void addProduct(const Product& product); | ||
| void updateProduct(int id, float price, int quantity); | ||
| void deleteProduct(int id); | ||
| void sellProduct(int id, int quantity); | ||
| const vector<Product>& getProducts() const; | ||
| }; | ||
|
|
||
| #endif // PRODUCTCATALOG_H |
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,161 @@ | ||
| #include "ProductCatalog.h" | ||
| #include "InventoryManager.h" | ||
| #include "CustomExceptions.h" | ||
| #include <iostream> | ||
| #include <limits> // for clearing input buffer | ||
| using namespace std; | ||
|
|
||
| void clearInputBuffer() { | ||
| cin.clear(); | ||
| cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
| } | ||
|
|
||
| void addProduct(ProductCatalog& catalog) { | ||
| int id; | ||
| string name; | ||
| float price; | ||
| int quantity; | ||
|
|
||
| cout << "Enter product ID: "; | ||
| if (!(cin >> id)) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| cout << "Enter product name: "; | ||
| getline(cin, name); | ||
|
|
||
| cout << "Enter product price: "; | ||
| if (!(cin >> price) || price < 0) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| cout << "Enter product quantity: "; | ||
| if (!(cin >> quantity) || quantity < 0) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| catalog.addProduct(Product(id, name, price, quantity)); | ||
| cout << "Product added successfully.\n"; | ||
| } | ||
|
|
||
| void updateProduct(ProductCatalog& catalog) { | ||
| int id; | ||
| float price; | ||
| int quantity; | ||
|
|
||
| cout << "Enter product ID to update: "; | ||
| if (!(cin >> id)) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| cout << "Enter new price: "; | ||
| if (!(cin >> price) || price < 0) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| cout << "Enter new quantity: "; | ||
| if (!(cin >> quantity) || quantity < 0) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| catalog.updateProduct(id, price, quantity); | ||
| cout << "Product updated successfully.\n"; | ||
| } | ||
|
|
||
| void deleteProduct(ProductCatalog& catalog) { | ||
| int id; | ||
| cout << "Enter product ID to delete: "; | ||
| if (!(cin >> id)) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| catalog.deleteProduct(id); | ||
| cout << "Product deleted successfully.\n"; | ||
| } | ||
|
|
||
| void sellProduct(ProductCatalog& catalog) { | ||
| int id; | ||
| int quantity; | ||
|
|
||
| cout << "Enter product ID to sell: "; | ||
| if (!(cin >> id)) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| cout << "Enter quantity to sell: "; | ||
| if (!(cin >> quantity) || quantity <= 0) { | ||
| throw InvalidInputException(); | ||
| } | ||
| clearInputBuffer(); | ||
|
|
||
| catalog.sellProduct(id, quantity); | ||
| cout << "Product sold successfully.\n"; | ||
| } | ||
|
|
||
| int main() { | ||
| ProductCatalog catalog; | ||
| InventoryManager manager; | ||
|
|
||
| int choice; | ||
| bool exitProgram = false; | ||
|
|
||
| do { | ||
| try { | ||
| cout << "Main Menu:\n"; | ||
| cout << "1. Add Product\n"; | ||
| cout << "2. Update Product\n"; | ||
| cout << "3. Delete Product\n"; | ||
| cout << "4. Sell Product\n"; | ||
| cout << "5. Display Inventory\n"; | ||
| cout << "6. Exit\n"; | ||
| cout << "Enter your choice: "; | ||
| cin >> choice; | ||
|
|
||
| switch (choice) { | ||
| case 1: | ||
| addProduct(catalog); | ||
| break; | ||
| case 2: | ||
| updateProduct(catalog); | ||
| break; | ||
| case 3: | ||
| deleteProduct(catalog); | ||
| break; | ||
| case 4: | ||
| sellProduct(catalog); | ||
| break; | ||
| case 5: | ||
| manager.displayInventory(catalog); | ||
| break; | ||
| case 6: | ||
| cout << "Exiting...\n"; | ||
| exitProgram = true; | ||
| break; | ||
| default: | ||
| cout << "Invalid choice. Please try again.\n"; | ||
| } | ||
| } catch (const InvalidInputException& e) { | ||
| cerr << "Invalid input data: " << e.what() << endl; | ||
| clearInputBuffer(); | ||
| } catch (const ProductNotFoundException& e) { | ||
| cerr << "Product not found: " << e.what() << endl; | ||
| } catch (const InsufficientQuantityException& e) { | ||
| cerr << "Insufficient quantity: " << e.what() << endl; | ||
| } catch (const DuplicateProductException& e) { | ||
| cerr << "Duplicate product: " << e.what() << endl; | ||
| } catch (const exception& e) { | ||
| cerr << "Exception: " << e.what() << endl; | ||
| } | ||
|
|
||
| } while (!exitProgram); | ||
|
|
||
| return 0; | ||
| } | ||
Binary file not shown.
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.