Untitled
unknown
plain_text
a year ago
5.6 kB
13
Indexable
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Product {
int skuCode;
string prodName;
double price;
int quantity;
string prodDescription;
};
// Function declarations
void addProduct();
void removeProduct();
void updateProduct();
void searchProduct();
void displayAllProducts();
void displayMenu();
void loadProducts(vector<Product>& products);
void saveProducts(const vector<Product>& products);
const string FILENAME = "inventory.txt";
// Main function
int main() {
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1: addProduct(); break;
case 2: removeProduct(); break;
case 3: updateProduct(); break;
case 4: searchProduct(); break;
case 5: displayAllProducts(); break;
case 6: cout << "Exiting...\n"; break;
default: cout << "Invalid choice, please try again.\n";
}
} while (choice != 6);
return 0;
}
// Function to display the main menu
void displayMenu() {
cout << "\n--- Inventory 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. Exit\n";
cout << "Enter your choice: ";
}
// Function to load products from file
void loadProducts(vector<Product>& products) {
products.clear();
ifstream inFile(FILENAME);
Product prod;
while (inFile >> prod.skuCode) {
inFile.ignore(); // Ignore newline character
getline(inFile, prod.prodName);
inFile >> prod.price >> prod.quantity;
inFile.ignore();
getline(inFile, prod.prodDescription);
products.push_back(prod);
}
inFile.close();
}
// Function to save products to file
void saveProducts(const vector<Product>& products) {
ofstream outFile(FILENAME);
for (const auto& prod : products) {
outFile << prod.skuCode << '\n'
<< prod.prodName << '\n'
<< prod.price << '\n'
<< prod.quantity << '\n'
<< prod.prodDescription << '\n';
}
outFile.close();
}
// Function to add a product
void addProduct() {
vector<Product> products;
loadProducts(products);
Product newProd;
cout << "Enter SKU Code: ";
cin >> newProd.skuCode;
cin.ignore(); // Ignore leftover newline
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);
saveProducts(products);
cout << "Product added successfully!\n";
}
// Function to remove a product
void removeProduct() {
vector<Product> products;
loadProducts(products);
int sku;
cout << "Enter SKU Code of the product to remove: ";
cin >> sku;
auto it = remove_if(products.begin(), products.end(), [sku](const Product& prod) {
return prod.skuCode == sku;
});
if (it != products.end()) {
products.erase(it, products.end());
saveProducts(products);
cout << "Product removed successfully!\n";
} else {
cout << "Product not found!\n";
}
}
// Function to update a product
void updateProduct() {
vector<Product> products;
loadProducts(products);
int sku;
cout << "Enter SKU Code of the product to update: ";
cin >> sku;
cin.ignore();
auto it = find_if(products.begin(), products.end(), [sku](const Product& prod) {
return prod.skuCode == sku;
});
if (it != products.end()) {
cout << "Enter new Product Name: ";
getline(cin, it->prodName);
cout << "Enter new Price: ";
cin >> it->price;
cout << "Enter new Quantity: ";
cin >> it->quantity;
cin.ignore();
cout << "Enter new Product Description: ";
getline(cin, it->prodDescription);
saveProducts(products);
cout << "Product updated successfully!\n";
} else {
cout << "Product not found!\n";
}
}
// Function to search for a product
void searchProduct() {
vector<Product> products;
loadProducts(products);
int sku;
cout << "Enter SKU Code to search: ";
cin >> sku;
auto it = find_if(products.begin(), products.end(), [sku](const Product& prod) {
return prod.skuCode == sku;
});
if (it != products.end()) {
cout << "Product Found:\n";
cout << "SKU Code: " << it->skuCode << "\n";
cout << "Name: " << it->prodName << "\n";
cout << "Price: $" << fixed << setprecision(2) << it->price << "\n";
cout << "Quantity: " << it->quantity << "\n";
cout << "Description: " << it->prodDescription << "\n";
} else {
cout << "Product not found!\n";
}
}
// Function to display all products
void displayAllProducts() {
vector<Product> products;
loadProducts(products);
cout << left << setw(10) << "SKU" << setw(20) << "Name" << setw(10) << "Price" << setw(10) << "Quantity" << "Description" << '\n';
cout << "----------------------------------------------------------\n";
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 << '\n';
}
}
Editor is loading...
Leave a Comment