Open In App

Implementing Interactive Online Shopping in C++

Improve
Improve
Like Article
Like
Save
Share
Report

Online shopping is all about calculating the total amount for the items selected by the customer. In this article, we will discuss a menu-driven C++ program for Online Shopping System.

Features of Online Shopping Management System

The online shopping management system will be able to provide the following functions to the users:

  1. Users will be able to purchase laptops, Mobile, and Programming Courses.
  2. Users will be able to add items.
  3. Users will be able to purchase an Item multiple times.
  4. Users will be able to print the bill.

Approach

  1. Firstly a menu will be displayed to the customer showing all the available product categories and checkout options.
  2. After the selection, all the products with their prices will be displayed.
  3. Then the customer will select the products they want to buy.
  4. This process continues until the shopping is completed and the customer clicks on the check-out button.
  5. Whenever the customer completes his shopping, the items, quantity, cost, and finally the total amount to be paid are displayed.

Prerequisites: C++ Control Statements, Functions, OOPs Concept, Standard Template Library.

C++ Program to Implement Online Shopping Management System

C++




// C++ Program to implement Online shopping management
// system
#include <bits/stdc++.h>
 
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
 
// map data structure to store category-model as key and
// their price as value
map<string, map<string, double> > items;
 
// map data structure to store category-model in integer
// form as key and their model selected by the customer as
// value
map<pair<int, int>, string> names;
 
// a function to fill the product data into the data
// structure
void fillItems()
{
    items["Mobile"]["Samsung"] = 15000;
    items["Mobile"]["Redmi"] = 12000;
    items["Mobile"]["Apple"] = 100000;
    items["Laptop"]["HP"] = 40000;
    items["Laptop"]["Lenovo"] = 35000;
    items["Laptop"]["Macbook"] = 250000;
    items["Course"]["C"] = 1000;
    items["Course"]["C++"] = 3000;
    items["Course"]["Java"] = 4000;
    items["Course"]["Python"] = 3500;
 
    // by default, category 1 is Mobile, category 2 is
    // Laptop and category 3 is Course
    /// similarly, 1,2,3 in the second part of the key
    /// represent their model
    names[{ 1, 1 }] = "Apple";
    names[{ 1, 2 }] = "Redmi";
    names[{ 1, 3 }] = "Samsung";
 
    names[{ 2, 1 }] = "HP";
    names[{ 2, 2 }] = "Lenovo";
    names[{ 2, 3 }] = "Macbook";
 
    names[{ 3, 1 }] = "C";
    names[{ 3, 2 }] = "C++";
    names[{ 3, 3 }] = "Java";
    names[{ 3, 4 }] = "Python";
}
 
// class customer to represent a real world entity, in our
// case - a customer
class customer {
    // map data strucute to store category-model as key and
    // their quantity selected by the customer as value
    map<string, map<string, int> > selected_items;
 
    // name of the customer. Here more data can be collected
    // from customer like phone number etc, but here we have
    // only collected name as an example
    string name;
 
public:
    // constructor
    customer()
    {
        cout << "Enter your name: ";
        string customer_name;
        getline(cin, customer_name);
        name = customer_name;
        cout << "HI ";
        for (int i = 0; i < customer_name.length(); i++) {
            cout << char(toupper(customer_name[i]));
        }
        cout << "\n\n\n";
    }
 
    // function to insert item in the data structure alloted
    // to customer
    void addToCart(string a, string b)
    {
        selected_items[a][b]++;
    }
 
    // function to print the bill during check-out
    void printBill()
    {
        int total_amount = 0;
        cout << "Category\t"
             << "Item\t"
             << "Quantity\t"
             << "Cost\n";
 
        // iterators to traverse over respective maps
        map<string, map<string, int> >::iterator itr;
        map<string, int>::iterator ptr;
        for (itr = selected_items.begin();
             itr != selected_items.end(); itr++) {
            for (ptr = itr->second.begin();
                 ptr != itr->second.end(); ptr++) {
                string category = itr->first;
                string model = ptr->first;
                int quantity = ptr->second;
                int temp
                    = quantity * items[category][model];
                cout << category << "\t\t" << model << "\t"
                     << quantity << "\t\t" << temp << endl;
                total_amount += temp;
            }
        }
 
        cout << "-------------------------------"
             << "-------------\n";
        cout << "Total amount:             \t\t"
             << total_amount << endl;
        cout << "-------------------------------"
             << "-------------\n";
        cout << "*****THANK YOU && HAPPY"
             << " ONLINE SHOPPING*****";
    }
};
 
// class shop to represent a real world entity - in our case
// a shop
class shop {
 
public:
    shop() {}
 
    virtual void show()
    {
        // this will be overridden
    }
    virtual void select(customer& obj)
    {
        // this will be overridden
    }
 
    void showMenu()
    {
        cout << "                 Menu\n";
        cout << " -----------------------------------------"
                "\n";
        cout << "1.Mobile\n2.Laptop\n3"
             << ".Programming Courses\n4.Checkout\n\n";
        cout << " -----------------------------------------"
                "\n";
    }
};
 
// class mobile inheriting properties from class shop
class mobile : public shop {
public:
    void show()
    {
        cout << "- - - - - - - - - - -"
             << " - -\nItem    Cost\n";
        int cnt = 1;
        for (auto& it : items["Mobile"]) {
            cout << cnt++ << ". " << it.first << " --- Rs."
                 << it.second << "/-\n";
        }
 
        cout << "- - - - - - - - - - - - -\n\n";
    }
 
    void select(customer& obj)
    {
        cout << "These are the smartphone categories we "
                "have....\n";
        int num;
        cout << "Enter your selection\n";
        cin >> num;
        if (num < 1 || num > 3) {
            cout << "Wrong Input\n";
            return;
        }
        cout << "\n\nPURCHASE SUCCESSFULL!! ";
        cout << names[{ 1, num }]
             << " IS ADDED TO THE CART\n\n";
        obj.addToCart("Mobile", names[{ 1, num }]);
    }
};
 
// class laptop inheriting properties from class shop
class laptop : public shop {
public:
    void show()
    {
        cout << "- - - - - - - - - - -"
             << " - -\nItem    Cost\n";
        int cnt = 1;
        for (auto& it : items["Laptop"]) {
            cout << cnt++ << ". " << it.first << " --- Rs."
                 << it.second << "/-\n";
        }
        cout << "- - - - - - - - - - - - -\n";
    }
    void select(customer& obj)
    {
        cout << "These are the laptop categories we "
                "have....\n";
        int num;
        cout << "Enter your selection\n";
        cin >> num;
        if (num < 1 || num > 3) {
            cout << "Wrong Input\n";
            return;
        }
        cout << "\n\nPURCHASE SUCCESSFULL!! ";
        cout << names[{ 2, num }]
             << " IS ADDED TO THE CART\n\n";
        obj.addToCart("Laptop", names[{ 2, num }]);
    }
};
 
// class courses inheriting properties from class shop
class courses : public shop {
public:
    void show()
    {
        cout << "- - - - - - - - - - "
             << " - -\nItem       Cost\n";
        int cnt = 1;
        for (auto& it : items["Course"]) {
            cout << cnt++ << ". " << it.first << " --- Rs."
                 << it.second << "/-\n";
        }
        cout << "- - - - - - - - - - - - -\n";
    }
    void select(customer& obj)
    {
        cout << "These are the courses categories we "
                "have....\n";
        int num;
        cout << "Enter your selection\n";
        cin >> num;
        if (num < 1 || num > 4) {
            cout << "Wrong Input\n";
            return;
        }
        cout << "\n\nPURCHASE SUCCESSFULL!! ";
        cout << names[{ 3, num }]
             << " IS ADDED TO THE CART\n\n";
        obj.addToCart("Course", names[{ 3, num }]);
    }
};
 
int main()
{
    // filling the hardcoded shop items in the data
    // structure
    fillItems();
    cout << "WELCOME TO OUR SHOP. WE ARE DELIGHTED THAT "
            "YOU ARE HERE. HOW CAN WE HELP YOU? WE DEAL IN "
            "ELECTRONIC GADGETS AND COURSES.\n";
    customer c;
    shop* s;
 
    while (1) {
 
        s->showMenu();
        cout
            << "Please select an option to proceed "
               "further. If you choose to checkout, "
               "shopping will end and bill will be "
               "generated for the items bought so far...\n";
        int val;
        cin >> val;
 
        if (val == 1) {
            mobile mb;
            s = &mb;
            s->show();
            s->select(c);
        }
        else if (val == 2) {
            laptop lp;
            s = &lp;
            s->show();
            s->select(c);
        }
        else if (val == 3) {
            courses cs;
            s = &cs;
            s->show();
            s->select(c);
        }
        else if (val == 4) {
            c.printBill();
            break;
        }
        else {
            cout << "Wrong Input. If you want to checkout, "
                    "you can press 4\n\n";
        }
        cout << "REDIRECTING TO THE MAIN MENU.....\n\n";
 
        // a function call to add a deliberate delay
        sleep_until(system_clock::now() + seconds(3));
 
        // clear screen
        system("cls");
    }
 
    return 0;
}


Output

Let us suppose, someone need to buy 2 Redmi mobiles, 1 HP Laptop and a Java course.

output of the program

Video Output

Working of the Program

STEP 1: Firstly, a map (map <string, map <string, double> >) data structure is declared to store category-model as key and their price as value. Similarly, a map (map <pair <int, int> , string>) data structure is declare to store category-model in integer form as key and their model selected by the customer as value.

image1

STEP 2: The program first ask for details of the customer i.e. the name of the customer. In our code, constructor in the customer class is used for this purpose. toupper() is used for converting all the characters of a string into uppercase.
 

image2

STEP 3: Display the menu to the user. showMenu() function is created for this purpose.

image3

STEP 4: Shows to the customer the main menu. Here do-while loop is used, this loop continues till the user clicks on check-out option. Whenever the check-out option is selected, it directly prints the bill.

  1. First, the customer is asked to input the number for type of product from the menu. If the wrong number is entered, it redirects to the main menu.
    1. If the input is valid, show the products of the selected type. Ask the user to input the number for respective product. If it is not valid, then it redirects to the main menu.
    2. The selected model along with its category gets pushed into the selected_items map and the user is redirected to the main menu.
    3. This process goes on till the user selects to check-out from the main menu.
  2. Else, they can type any other number which will direct them to main menu, from where they can check-out.

Below are some screenshots showing what will be the screen like, when the particular selection is done by the user- 

  • If mobile is selected: The below screenshot shows the Mobile Menu:
    img4
  • If Laptop is selected: The below screenshot shows the Laptop Menu:
    img5
  • If Course is selected: The below screenshot shows the Course Menu:
    img6
  • After the show() of model, select() function gets invoked, for example here is the select function for courses:
    img7
  • Once a purchase is done, they are re-directed to the main menu.
  • If the customer clicks on the check-out option, bill is generated and program terminates.
    img8

    Bill of purchased items



Last Updated : 09 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads