Group Members
Group Members
Group Members
class Product
{
private:
int id;
string name;
string category;
double price;
int quantity;
public:
Product(int id, string name, string category, double price, int quantity)
{
this->id = id;
this->name = name;
this->category = category;
this->price = price;
this->quantity = quantity;
}
class Inventory
{
private:
vector<Product> products;
public:
void addProduct(Product product)
{
bool found = false;
for (auto &p : products)
{
if (p.getId() == product.getId())
{
cout << "Id already Exist." << endl;
found = true;
break;
}
}
if (!found)
{
products.push_back(product);
add_toCSV(product);
cout << "Product added successfully." << endl;
cout << "-----------------------------------------------------------" << endl;
}
}
void updateProduct(int id, string name, string category, double price, int quantity)
{
bool found = false;
for (auto i = products.begin(); i != products.end(); i++)
{
if (i->getId() == id)
{
i->setName(name);
i->setCategory(category);
i->setPrice(price);
i->setQuantity(quantity);
found = true;
break;
}
}
if (!found)
{
cout << "ID does not exist." << endl;
}
}
if (file.is_open())
{
string line;
while (getline(file, line))
{
stringstream ss(line);
string idStr, name, category, priceStr, quantityStr;
getline(ss, idStr, ',');
getline(ss, name, ',');
getline(ss, category, ',');
getline(ss, priceStr, ',');
getline(ss, quantityStr, ',');
int id = stoi(idStr);
double price = stod(priceStr);
int quantity = stoi(quantityStr);
file.close();
}
else
{
cout << "Error: Could not open file " << filename << endl;
}
}
};
int main()
{
Inventory inventory;
cout << "-----------------------------------------------------------" << endl;
cout << "---------------Inventory Management System ----------------" << endl;
cout << "-----------------------------------------------------------" << endl;
cout << "------------------------- Welcome! ------------------------" << endl;
cout << "-----------------------------------------------------------" << endl;
char choice;
do
{
cout << "Please choose an option:" << endl;
cout << "1. Add a product" << endl;
cout << "2. Remove a product" << endl;
cout << "3. Find a product" << endl;
cout << "4. Update a product" << endl;
cout << "5. View all products" << endl;
cout << "6. Save inventory to file" << endl;
cout << "7. Load Inventory from file" << endl;
cout << "Q. Quit" << endl;
cin >> choice;
switch (choice)
{
case '1':
{
int id;
string name, category;
double price;
int quantity;
cout << "Enter ID: ";
cin >> id;
cout << "Enter product name: ";
cin >> name;
cout << "Enter product category: ";
cin >> category;
cout << "Enter product price: $ ";
cin >> price;
cout << "Enter product quantity: ";
cin >> quantity;
Product product(id, name, category, price, quantity);
inventory.addProduct(product);
break;
}
case '2':
{
int id;
cout << "Enter product id: ";
cin >> id;
inventory.removeProduct(id);
break;
}
case '3':
{
int id;
cout << "Enter product id: ";
cin >> id;
Product *product = inventory.findProduct(id);
if (product)
{
cout << "Name: " << product->getName() << endl;
cout << "Category: " << product->getCategory() << endl;
cout << "Price: $ " << product->getPrice() << endl;
cout << "Quantity: " << product->getQuantity() << endl;
cout << "-----------------------------------------------------------" << endl;
}
else
{
cout << "Product not found." << endl;
cout << "-----------------------------------------------------------" << endl;
}
break;
}
case '4':
{
int id;
string name, category;
double price;
int quantity;
cout << "Enter the product id: ";
cin >> id;
cout << "Enter new product name: ";
cin >> name;
cout << "Enter new product category: ";
cin >> category;
cout << "Enter new product price: $ ";
cin >> price;
cout << "Enter new product quantity: ";
cin >> quantity;
inventory.updateProduct(id, name, category, price, quantity);
cout << "Product updated successfully." << endl;
cout << "-----------------------------------------------------------" << endl;
break;
}
case '5':
{
inventory.printProduct();
break;
}
case '6':
{
inventory.saveInventoryToFile("inventory.csv");
cout << "Inventory saved to file." << endl;
cout << "-----------------------------------------------------------" << endl;
break;
}
case '7':
{
inventory.loadInventoryFromFile("inventory.csv");
cout << "Inventory loaded from file." << endl;
cout << "-----------------------------------------------------------" << endl;
break;
}
case 'q':
case 'Q':
cout << "Goodbye!" << endl;
cout << "-----------------------------------------------------------" << endl;
return 0;
default:
cout << "Invalid Choice. Please Try again" << endl;
cout << "-----------------------------------------------------------" << endl;
break;
}
} while (true);
return 0;
}
OUTPUT: