Untitled
unknown
plain_text
a month ago
5.2 kB
8
Indexable
Never
#include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> using namespace std; struct Product { int skuCode; string prodName; double price; int quantity; string prodDescription; }; void addProduct(vector<Product>& inventory); void removeProduct(vector<Product>& inventory); void updateProduct(vector<Product>& inventory); void searchProduct(const vector<Product>& inventory); void displayProducts(const vector<Product>& inventory); void saveToFile(const vector<Product>& inventory); void loadFromFile(vector<Product>& inventory); int main() { vector<Product> inventory; loadFromFile(inventory); int choice; do { cout << "\nInventory Management System\n"; cout << "1. Add Product\n"; cout << "2. Remove Product\n"; cout << "3. Update Product\n"; cout << "4. Search Product\n"; cout << "5. Display All Products\n"; cout << "6. Save and Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: addProduct(inventory); break; case 2: removeProduct(inventory); break; case 3: updateProduct(inventory); break; case 4: searchProduct(inventory); break; case 5: displayProducts(inventory); break; case 6: saveToFile(inventory); break; default: cout << "Invalid choice. Please try again.\n"; } } while (choice != 6); return 0; } void addProduct(vector<Product>& inventory) { Product p; cout << "Enter SKU Code: "; cin >> p.skuCode; cin.ignore(); // Clear the buffer cout << "Enter Product Name: "; getline(cin, p.prodName); cout << "Enter Price: "; cin >> p.price; cout << "Enter Quantity: "; cin >> p.quantity; cin.ignore(); // Clear the buffer cout << "Enter Product Description: "; getline(cin, p.prodDescription); inventory.push_back(p); cout << "Product added successfully.\n"; } void removeProduct(vector<Product>& inventory) { int sku; cout << "Enter SKU Code of the product to remove: "; cin >> sku; auto it = remove_if(inventory.begin(), inventory.end(), [sku](Product& p) { return p.skuCode == sku; }); if (it != inventory.end()) { inventory.erase(it, inventory.end()); cout << "Product removed successfully.\n"; } else { cout << "Product not found.\n"; } } void updateProduct(vector<Product>& inventory) { int sku; cout << "Enter SKU Code of the product to update: "; cin >> sku; for (auto& p : inventory) { if (p.skuCode == sku) { cout << "Enter new Product Name: "; cin.ignore(); // Clear the buffer getline(cin, p.prodName); cout << "Enter new Price: "; cin >> p.price; cout << "Enter new Quantity: "; cin >> p.quantity; cin.ignore(); // Clear the buffer cout << "Enter new Product Description: "; getline(cin, p.prodDescription); cout << "Product updated successfully.\n"; return; } } cout << "Product not found.\n"; } void searchProduct(const vector<Product>& inventory) { int sku; cout << "Enter SKU Code of the product to search: "; cin >> sku; for (const auto& p : inventory) { if (p.skuCode == sku) { cout << "Product found:\n"; cout << "SKU Code: " << p.skuCode << "\n"; cout << "Product Name: " << p.prodName << "\n"; cout << "Price: " << p.price << "\n"; cout << "Quantity: " << p.quantity << "\n"; cout << "Description: " << p.prodDescription << "\n"; return; } } cout << "Product not found.\n"; } void displayProducts(const vector<Product>& inventory) { cout << "\nAll Products:\n"; for (const auto& p : inventory) { cout << "SKU Code: " << p.skuCode << "\n"; cout << "Product Name: " << p.prodName << "\n"; cout << "Price: " << p.price << "\n"; cout << "Quantity: " << p.quantity << "\n"; cout << "Description: " << p.prodDescription << "\n\n"; } } void saveToFile(const vector<Product>& inventory) { ofstream file("inventory.txt"); for (const auto& p : inventory) { file << p.skuCode << "\n"; file << p.prodName << "\n"; file << p.price << "\n"; file << p.quantity << "\n"; file << p.prodDescription << "\n"; } file.close(); cout << "Inventory saved to file successfully.\n"; } void loadFromFile(vector<Product>& inventory) { ifstream file("inventory.txt"); if (!file.is_open()) { cout << "No existing inventory found.\n"; return; } Product p; while (file >> p.skuCode) { file.ignore(); // Clear the buffer getline(file, p.prodName); file >> p.price; file >> p.quantity; file.ignore(); // Clear the buffer getline(file, p.prodDescription); inventory.push_back(p); } file.close(); cout << "Inventory loaded from file successfully.\n"; }
Leave a Comment