google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: simple C++ program to manage a kirana store

Sunday, March 2, 2025

simple C++ program to manage a kirana store

 Here's a simple C++ program to manage a kirana store:


#include <iostream>

#include <string>

#include <map>


using namespace std;


class KiranaStore {

private:

    map<string, int> inventory;

    map<string, int> prices;


public:

    void addInventory(const string& item, int quantity) {

        if (inventory.find(item) != inventory.end()) {

            inventory[item] += quantity;

        } else {

            inventory[item] = quantity;

        }

    }


    void removeInventory(const string& item, int quantity) {

        if (inventory.find(item) != inventory.end()) {

            if (inventory[item] >= quantity) {

                inventory[item] -= quantity;

            } else {

                cout << "Insufficient quantity in stock." << endl;

            }

        } else {

            cout << "Item not found in inventory." << endl;

        }

    }


    void addPrice(const string& item, int price) {

        prices[item] = price;

    }


    void displayInventory() {

        cout << "Current Inventory:" << endl;

        for (const auto& pair : inventory) {

            cout << pair.first << ": " << pair.second << endl;

        }

    }


    void displayPrices() {

        cout << "Current Prices:" << endl;

        for (const auto& pair : prices) {

            cout << pair.first << ": " << pair.second << endl;

        }

    }


    void calculateBill(const string& item, int quantity) {

        if (prices.find(item) != prices.end()) {

            int total = prices[item] * quantity;

            cout << "Total bill for " << quantity << " " << item << "(s): " << total << endl;

        } else {

            cout << "Price not found for the item." << endl;

        }

    }

};


int main() {

    KiranaStore store;


    while (true) {

        cout << "Kirana Store Management System" << endl;

        cout << "1. Add inventory" << endl;

        cout << "2. Remove inventory" << endl;

        cout << "3. Add price" << endl;

        cout << "4. Display inventory" << endl;

        cout << "5. Display prices" << endl;

        cout << "6. Calculate bill" << endl;

        cout << "7. Exit" << endl;


        int choice;

        cin >> choice;


        switch (choice) {

            case 1: {

                string item;

                int quantity;

                cout << "Enter item name: ";

                cin >> item;

                cout << "Enter quantity: ";

                cin >> quantity;

                store.addInventory(item, quantity);

                break;

            }

            case 2: {

                string item;

                int quantity;

                cout << "Enter item name: ";

                cin >> item;

                cout << "Enter quantity: ";

                cin >> quantity;

                store.removeInventory(item, quantity);

                break;

            }

            case 3: {

                string item;

                int price;

                cout << "Enter item name: ";

                cin >> item;

                cout << "Enter price: ";

                cin >> price;

                store.addPrice(item, price);

                break;

            }

            case 4:

                store.displayInventory();

                break;

            case 5:

                store.displayPrices();

                break;

            case 6: {

                string item;

                int quantity;

                cout << "Enter item name: ";

                cin >> item;

                cout << "Enter quantity: ";

                cin >> quantity;

                store.calculateBill(item, quantity);

                break;

            }

            case 7:

                return 0;

            default:

                cout << "Invalid choice. Please choose a valid option." << endl;

        }

    }


    return 0;

}

```


*Example Use Cases:*

1. Run the program and select option 1 (Add inventory).

2. Enter an item name and quantity, e.g., "Rice" and 50.

3. Select option 4 (Display inventory) to view the updated inventory.

4. Select option 3 (Add price) and enter a price for the item, e.g., 50 for "Rice".

5. Select option 6 (Calculate bill) and enter the item name and quantity, e.g., "Rice" and 10.

6. The program will display the total bill for the item.


*How to Compile and Run:*

1. Save the code in a file named `kirana_store.cpp`.

2. Open a terminal or command prompt.

3. Navigate to the directory where you saved the file.

4. Compile the program using `g++ kirana_store.cpp -o kirana_store`.

5. Run the program using `./kirana_store` (on Linux/macOS) or `kirana_store.exe` (on Windows).

No comments:

Post a Comment

simple C++ program to manage a kirana store

 Here's a simple C++ program to manage a kirana store: #include <iostream> #include <string> #include <map> using name...