Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
8.4 kB
5
Indexable
Never
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include <limits>
#include <algorithm>

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 searchProductBySKU(const vector<Product>& products);
void searchProductByName(const vector<Product>& products);
void displayAllProducts(const vector<Product>& products);
void loadProducts(vector<Product>& products);
void saveProducts(const vector<Product>& products);
void displayMenu();
void clearInputBuffer();
bool isValidDouble(const string& s);

// 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();                   // Display the main menu
        cin >> choice;                   // Get user's choice
        clearInputBuffer();              // Clear any remaining input

        switch(choice) {
            case 1: addProduct(products); break;
            case 2: removeProduct(products); break;
            case 3: updateProduct(products); break;
            case 4: searchProductBySKU(products); break;
            case 5: searchProductByName(products); break;
            case 6: displayAllProducts(products); break;
            case 7: cout << "Exiting the program..." << endl; break;
            default: cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 7);

    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 by SKU" << endl;
    cout << "5. Search Product by Name" << endl;
    cout << "6. Display All Products" << endl;
    cout << "7. 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;
    string input;

    cout << "Enter SKU Code: ";
    cin >> newProd.skuCode;
    clearInputBuffer();

    cout << "Enter Product Name: ";
    getline(cin, newProd.prodName);

    do {
        cout << "Enter Price: ";
        cin >> input;
    } while (!isValidDouble(input));
    newProd.price = stod(input);

    cout << "Enter Quantity: ";
    cin >> newProd.quantity;
    clearInputBuffer();

    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;

    auto it = remove_if(products.begin(), products.end(),
                        [sku](const Product& p) { return p.skuCode == sku; });
    
    if (it != products.end()) {
        products.erase(it, products.end());
        cout << "Product removed successfully!" << endl;
    } else {
        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;
    clearInputBuffer();

    auto it = find_if(products.begin(), products.end(),
                      [sku](const Product& p) { return p.skuCode == sku; });
    
    if (it != products.end()) {
        cout << "Enter new Product Name: ";
        getline(cin, it->prodName);
        
        string input;
        do {
            cout << "Enter new Price: ";
            cin >> input;
        } while (!isValidDouble(input));
        it->price = stod(input);

        cout << "Enter new Quantity: ";
        cin >> it->quantity;
        clearInputBuffer();

        cout << "Enter new Product Description: ";
        getline(cin, it->prodDescription);

        cout << "Product updated successfully!" << endl;
    } else {
        cout << "Product not found!" << endl;
    }
}

// Function to search for a product by SKU
void searchProductBySKU(const vector<Product>& products) {
    int sku;
    cout << "Enter SKU Code to search: ";
    cin >> sku;

    auto it = find_if(products.begin(), products.end(),
                      [sku](const Product& p) { return p.skuCode == sku; });
    
    if (it != products.end()) {
        cout << "Product Found:" << endl;
        cout << "SKU Code: " << it->skuCode << endl;
        cout << "Name: " << it->prodName << endl;
        cout << "Price: $" << fixed << setprecision(2) << it->price << endl;
        cout << "Quantity: " << it->quantity << endl;
        cout << "Description: " << it->prodDescription << endl;
    } else {
        cout << "Product not found!" << endl;
    }
}

// Function to search for a product by name
void searchProductByName(const vector<Product>& products) {
    string name;
    cout << "Enter Product Name to search: ";
    getline(cin, name);

    bool found = false;
    for (const auto& prod : products) {
        if (prod.prodName.find(name) != string::npos) {
            if (!found) {
                cout << "Products Found:" << endl;
                found = true;
            }
            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;
            cout << "------------------------" << endl;
        }
    }
    if (!found) {
        cout << "No products found matching that name." << 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 << string(70, '-') << endl;
    for (const auto& prod : products) {
        cout << left << setw(10) << prod.skuCode
             << setw(20) << prod.prodName
             << setw(10) << fixed << setprecision(2) << prod.price
             << setw(10) << prod.quantity
             << prod.prodDescription << endl;
    }
}

// Function to clear input buffer
void clearInputBuffer() {
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

// Function to validate if a string is a valid double
bool isValidDouble(const string& s) {
    try {
        size_t pos = 0;
        stod(s, &pos);
        return pos == s.size();
    } catch (...) {
        return false;
    }
}
Leave a Comment