Untitled
unknown
plain_text
a year ago
6.6 kB
11
Indexable
#include <iostream> #include <fstream> #include <vector> #include <string> #include <iomanip> using namespace std; // This struct represents a product in the inventory struct Product { int skuCode; string prodName; double price; int quantity; string prodDescription; }; // Function prototypes void addProduct(vector<Product>& products); void removeProduct(vector<Product>& products); void updateProduct(vector<Product>& products); void searchProduct(const vector<Product>& products); void displayAllProducts(const vector<Product>& products); void loadProducts(vector<Product>& products); void saveProducts(const vector<Product>& products); void displayMenu(); // File where inventory data is stored const string FILENAME = "inventory.txt"; int main() { vector<Product> products; // Store all products loadProducts(products); // Load existing products from the file int choice; do { displayMenu(); cin >> choice; if (choice == 1) { addProduct(products); } else if (choice == 2) { removeProduct(products); } else if (choice == 3) { updateProduct(products); } else if (choice == 4) { searchProduct(products); } else if (choice == 5) { displayAllProducts(products); } else if (choice == 6) { cout << "Exiting the program..." << endl; } else { cout << "Invalid choice. Please try again." << endl; } } while (choice != 6); saveProducts(products); // Save all products back to the file before exiting return 0; } // Function to display the main menu void displayMenu() { cout << "\n--- Inventory Management System ---" << endl; cout << "1. Add Product" << endl; cout << "2. Remove Product" << endl; cout << "3. Update Product" << endl; cout << "4. Search Product" << endl; cout << "5. Display All Products" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; } // Function to load products from file void loadProducts(vector<Product>& products) { ifstream inFile(FILENAME); if (inFile.is_open()) { Product prod; while (inFile >> prod.skuCode) { inFile.ignore(); // Ignore the newline after skuCode getline(inFile, prod.prodName); inFile >> prod.price >> prod.quantity; inFile.ignore(); // Ignore the newline after quantity getline(inFile, prod.prodDescription); products.push_back(prod); } inFile.close(); } else { cout << "Could not open file for loading. Starting with an empty inventory." << endl; } } // Function to save products to file void saveProducts(const vector<Product>& products) { ofstream outFile(FILENAME); if (outFile.is_open()) { for (const auto& prod : products) { outFile << prod.skuCode << '\n' << prod.prodName << '\n' << prod.price << '\n' << prod.quantity << '\n' << prod.prodDescription << '\n'; } outFile.close(); } else { cout << "Could not open file for saving." << endl; } } // Function to add a product void addProduct(vector<Product>& products) { Product newProd; cout << "Enter SKU Code: "; cin >> newProd.skuCode; cin.ignore(); // Ignore the leftover newline character cout << "Enter Product Name: "; getline(cin, newProd.prodName); cout << "Enter Price: "; cin >> newProd.price; cout << "Enter Quantity: "; cin >> newProd.quantity; cin.ignore(); cout << "Enter Product Description: "; getline(cin, newProd.prodDescription); products.push_back(newProd); cout << "Product added successfully!" << endl; } // Function to remove a product void removeProduct(vector<Product>& products) { int sku; cout << "Enter SKU Code of the product to remove: "; cin >> sku; bool found = false; for (auto it = products.begin(); it != products.end(); ++it) { if (it->skuCode == sku) { products.erase(it); cout << "Product removed successfully!" << endl; found = true; break; } } if (!found) { cout << "Product not found!" << endl; } } // Function to update a product void updateProduct(vector<Product>& products) { int sku; cout << "Enter SKU Code of the product to update: "; cin >> sku; cin.ignore(); bool found = false; for (auto& prod : products) { if (prod.skuCode == sku) { cout << "Enter new Product Name: "; getline(cin, prod.prodName); cout << "Enter new Price: "; cin >> prod.price; cout << "Enter new Quantity: "; cin >> prod.quantity; cin.ignore(); cout << "Enter new Product Description: "; getline(cin, prod.prodDescription); found = true; cout << "Product updated successfully!" << endl; break; } } if (!found) { cout << "Product not found!" << endl; } } // Function to search for a product void searchProduct(const vector<Product>& products) { int sku; cout << "Enter SKU Code to search: "; cin >> sku; bool found = false; for (const auto& prod : products) { if (prod.skuCode == sku) { cout << "Product Found:" << endl; cout << "SKU Code: " << prod.skuCode << endl; cout << "Name: " << prod.prodName << endl; cout << "Price: $" << fixed << setprecision(2) << prod.price << endl; cout << "Quantity: " << prod.quantity << endl; cout << "Description: " << prod.prodDescription << endl; found = true; break; } } if (!found) { cout << "Product not found!" << endl; } } // Function to display all products void displayAllProducts(const vector<Product>& products) { if (products.empty()) { cout << "No products in the inventory." << endl; return; } cout << left << setw(10) << "SKU" << setw(20) << "Name" << setw(10) << "Price" << setw(10) << "Quantity" << "Description" << endl; cout << "----------------------------------------------------------" << endl; for (const auto& prod : products) { cout << left << setw(10) << prod.skuCode << setw(20) << prod.prodName << setw(10) << prod.price << setw(10) << prod.quantity << prod.prodDescription << endl; } }
Editor is loading...
Leave a Comment