Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions CustomExceptions.h
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
18 changes: 18 additions & 0 deletions InventoryManager.cpp
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;
}
}
}
11 changes: 11 additions & 0 deletions InventoryManager.h
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
26 changes: 26 additions & 0 deletions Product.cpp
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;
}
24 changes: 24 additions & 0 deletions Product.h
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
50 changes: 50 additions & 0 deletions ProductCatalog.cpp
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;
}
20 changes: 20 additions & 0 deletions ProductCatalog.h
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
161 changes: 161 additions & 0 deletions main.cpp
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 added main.exe
Binary file not shown.