diff --git a/CustomExceptions.h b/CustomExceptions.h new file mode 100644 index 0000000..409f530 --- /dev/null +++ b/CustomExceptions.h @@ -0,0 +1,35 @@ +#ifndef CUSTOM_EXCEPTIONS_H +#define CUSTOM_EXCEPTIONS_H +using namespace std; +#include +#include + +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 diff --git a/InventoryManager.cpp b/InventoryManager.cpp new file mode 100644 index 0000000..e3caf59 --- /dev/null +++ b/InventoryManager.cpp @@ -0,0 +1,18 @@ +#include "InventoryManager.h" +#include + +using namespace std; + +void InventoryManager::displayInventory(const ProductCatalog& catalog) const { + const vector& 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; + } + } +} diff --git a/InventoryManager.h b/InventoryManager.h new file mode 100644 index 0000000..a40f8e2 --- /dev/null +++ b/InventoryManager.h @@ -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 diff --git a/Product.cpp b/Product.cpp new file mode 100644 index 0000000..0e3a5b2 --- /dev/null +++ b/Product.cpp @@ -0,0 +1,26 @@ +#include "Product.h" +#include +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; +} diff --git a/Product.h b/Product.h new file mode 100644 index 0000000..316da05 --- /dev/null +++ b/Product.h @@ -0,0 +1,24 @@ +#ifndef PRODUCT_H +#define PRODUCT_H + +#include +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 diff --git a/ProductCatalog.cpp b/ProductCatalog.cpp new file mode 100644 index 0000000..38289fa --- /dev/null +++ b/ProductCatalog.cpp @@ -0,0 +1,50 @@ +#include "ProductCatalog.h" +#include "Product.h" +#include "CustomExceptions.h" +#include +#include +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& ProductCatalog::getProducts() const { + return products; +} diff --git a/ProductCatalog.h b/ProductCatalog.h new file mode 100644 index 0000000..3fcde8f --- /dev/null +++ b/ProductCatalog.h @@ -0,0 +1,20 @@ +#ifndef PRODUCTCATALOG_H +#define PRODUCTCATALOG_H + +#include "Product.h" +#include +using namespace std; + +class ProductCatalog { +private: + vector 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& getProducts() const; +}; + +#endif // PRODUCTCATALOG_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..895e9a7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,161 @@ +#include "ProductCatalog.h" +#include "InventoryManager.h" +#include "CustomExceptions.h" +#include +#include // for clearing input buffer +using namespace std; + +void clearInputBuffer() { + cin.clear(); + cin.ignore(numeric_limits::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; +} diff --git a/main.exe b/main.exe new file mode 100644 index 0000000..627a66c Binary files /dev/null and b/main.exe differ