📜  在C ++中实现交互式在线购物

📅  最后修改于: 2021-05-31 23:35:13             🧑  作者: Mango

在线购物是关于计算客户选择的商品的总金额。在本文中,我们将讨论用于在线购物的菜单驱动的C++程序。

提供的功能

  1. 用户将能够购买笔记本电脑,手机,计算机课程。
  2. 用户将能够添加项目。
  3. 用户将能够减少数量或删除项目。
  4. 用户将能够打印账单。

方法

首先,菜单将显示给客户。选择后,将显示所有产品及其价格。然后,客户将选择产品并选择数量(产品数量)。这个过程一直持续到购物完成为止。每当客户完成购物时,都会显示商品,数量,成本以及最终要支付的总金额。

下面是上述功能的实现:

C++
// C++ program to implement the program
// that illustrates Online shopping
#include 
#include 
#include 
#include 
using namespace std;
  
char c1, confirm_quantity;
float quantity;
int selectedNum;
double total_amount = 0;
int flag = 0;
  
// Stores items with their corresponding
// price
map items = {
    { "Samsung", 15000 },
    { "Redmi", 12000 },
    { "Apple", 100000 },
    { "Macbook", 250000 },
    { "HP", 40000 },
    { "Lenovo", 35000 },
    { "C", 1000 },
    { "C++", 3000 },
    { "Java", 4000 },
    { "Python", 3500 }
};
  
// Stores the selected items with
// their quantity
map selected_items;
  
// Function to print the bill after shopping
// is completed prints the items, quantity,
// their cost along with total amount
void printBill(map items,
               map selected_items,
               float total_amount)
{
    cout << "Item      "
         << "Quantity      "
         << "Cost\n";
  
    for (auto j = selected_items.begin();
         j != selected_items.end(); j++) {
        cout << j->first << "        ";
        cout << j->second << "          ";
        cout << (selected_items[j->first])
                    * (items[j->first])
             << endl;
    }
  
    cout << "-----------------------"
         << "-------------\n";
    cout << "Total amount:             "
         << total_amount << endl;
    cout << "-----------------------"
         << "-------------\n";
    cout << "*****THANK YOU && HAPPY"
         << " ONLINE SHOPPING*****";
}
  
// Function to ask the basic details of
// any customer
void customerDetails()
{
  
    cout << "Enter your name: ";
    string customer_name;
    getline(cin, customer_name);
  
    cout << "WELCOME ";
    for (int i = 0;
         i < customer_name.length();
         i++) {
        cout << char(toupper(
            customer_name[i]));
    }
    cout << "\n";
}
  
// showMenu() is to print the
// menu to the user
void showMenu()
{
    cout << "Menu\n";
    cout << "=  =  =  =  =  =  =  = "
         << " =  =  =  =  = \n";
    cout << "1.Mobile\n2.laptop\n3"
         << ".Computer courses\n";
    cout << "=  =  =  =  =  =  =  = "
         << " =  =  =  =  = \n";
}
  
// Function to display the mobile products
void showMobileMenu()
{
    cout << "- - - - - - - - - - -"
         << " - -\nItem       Cost\n";
    cout << "1.Samsung  Rs.15, 000/-\n";
    cout << "2.Redmi    Rs.12, 000/-\n";
    cout << "3.Apple    Rs.1, 00, 000/-\n";
    cout << "- - - - - - - - - - - - -\n";
}
  
// Function to display Laptop products
void showLaptopMenu()
{
    cout << "- - - - - - - - - - -"
         << " - -\nItem       Cost\n";
    cout << "1.Macbook  Rs.2, 00, 000/-\n";
    cout << "2.HP       Rs.40, 000/-\n";
    cout << "3.Lenovo   Rs.35, 000/-\n";
    cout << "- - - - - - - - - - - - -\n";
}
  
// if the user selects computer couses,
// then courses list will be displayed
void showComputerCourseMenu()
{
    cout << "- - - - - - - - - - "
         << " - -\nItem       Cost\n";
    cout << "1.C        Rs.1, 000/-\n";
    cout << "2.C++      Rs.3, 000/-\n";
    cout << "3.Java     Rs.4, 000/-\n";
    cout << "4.Python   Rs.3, 500/-\n";
    cout << "- - - - - - - - - - - - -\n";
}
  
// Function to display the mobile category
void selectedMobile()
{
    cout << "Do you wish to conti"
         << "nue?(for yes" + "press (Y/y ), "
         << " if no press other letter ): ";
    cin >> c1;
  
    if (c1 == 'Y' || c1 == 'y') {
        cout << "Enter respective number: ";
        cin >> selectedNum;
  
        if (selectedNum == 1
            || selectedNum == 2
            || selectedNum == 3) {
  
            // Selected Samsung
            if (selectedNum == 1) {
  
                cout << "selected Samsung\n";
                do {
                    cout << "Quantity: ";
  
                    cin >> quantity;
  
                    cout << "You have selected Samsung - "
                         << quantity << endl;
                    cout << "Are you sure?"
                         << "(for yes press (Y/y ), "
                         << " if no press other letter): ";
  
                    cin >> confirm_quantity;
  
                } while ((confirm_quantity != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity) != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
                    total_amount += quantity
                                    * items["Samsung"];
                    selected_items["Samsung"] = quantity;
                    cout << "amount  =  "
                         << total_amount << endl;
                }
            }
  
            // Selected Redmi
            if (selectedNum == 2) {
  
                cout << "selected Redmi\n";
  
                do {
                    cout << "Quantity: ";
                    cin >> quantity;
                    cout << "You have selec"
                         << "ted Redmi - "
                         << quantity << endl;
                    cout << "Are you sure?(f"
                         << "or yes press (Y/y ), "
                         << " if no press other letter ): ";
                    cin >> confirm_quantity;
                } while ((confirm_quantity != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity)
                             != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
  
                    total_amount += quantity
                                    * items["Redmi"];
                    selected_items["Redmi"] = quantity;
                    cout << "amount  =  "
                         << total_amount << endl;
                }
            }
  
            // Selected Apple
            if (selectedNum == 3) {
  
                cout << "You have selected Apple\n";
  
                do {
                    cout << "Quantity: ";
                    cin >> quantity;
                    cout << "You have selected"
                         << " Apple - "
                         << quantity
                         << endl;
                    cout << "Are you sure?"
                         << "(for yes press (Y/y )"
                         << ", if no press other letter ): ";
                    cin >> confirm_quantity;
                } while ((confirm_quantity != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity)
                             != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
                    total_amount += quantity
                                    * items["Apple"];
                    selected_items["Apple"] = quantity;
                    cout << "amount  =  "
                         << total_amount
                         << endl;
                }
            }
        }
        else {
            flag = 1;
        }
    }
    else {
        flag = 1;
    }
}
  
// If Laptop category is selected
void selectedLaptop()
{
    cout << "Do you wish to continue?"
         << "(for yes press (Y/y ), "
         << "if no press other letter): ";
    cin >> c1;
    if (c1 == 'Y' || c1 == 'y') {
  
        cout << "Enter respective number: ";
        cin >> selectedNum;
  
        if (selectedNum == 1
            || selectedNum == 2
            || selectedNum == 3) {
  
            // selected Macbook
            if (selectedNum == 1) {
                cout << "selected Macbook\n";
                do {
  
                    cout << "Quantity: ";
                    cin >> quantity;
  
                    cout << "You have selected"
                         << " Macbook - "
                         << quantity << endl;
                    cout << "Are you sure?"
                         << "(for yes press (Y/y ), "
                         << " if no press other letter ): ";
                    cin >> confirm_quantity;
                } while ((confirm_quantity != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity)
                             != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
                    total_amount += quantity
                                    * items["Macbook"];
                    selected_items["Macbook"] = quantity;
                    cout << "amount  =  "
                         << total_amount
                         << endl;
                }
            }
  
            // selected HP
            if (selectedNum == 2) {
                cout << "selected HP\n";
                do {
                    cout << "Quantity: ";
                    cin >> quantity;
                    cout << "You have selected"
                         << " HP - "
                         << quantity << endl;
                    cout << "Are you sure?"
                         << "(for yes press (Y/y ), "
                         << " if no press other letter ): ";
                    cin >> confirm_quantity;
                } while ((confirm_quantity
                              != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity)
                             != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
                    total_amount += quantity
                                    * items["HP"];
                    selected_items["HP"] = quantity;
                    cout << "amount  =  "
                         << total_amount
                         << endl;
                }
            }
  
            // selected Lenovo
            if (selectedNum == 3) {
                cout << "selected Lenovo\n";
                do {
  
                    cout << "Quantity: ";
                    cin >> quantity;
  
                    cout << "You have selected"
                            " Lenovo - "
                         << quantity << endl;
                    cout << "Are you sure?"
                         << "(for yes press (Y/y ), "
                         << "if no press other letter ): ";
                    cin >> confirm_quantity;
                } while ((confirm_quantity != 'y'
                          && confirm_quantity != 'Y')
                         || (quantity < 0)
                         || (ceil(quantity)
                             != floor(quantity)));
  
                if (confirm_quantity == 'y'
                    || confirm_quantity == 'Y') {
                    total_amount += quantity
                                    * items["Lenovo"];
                    selected_items["Lenovo"] = quantity;
                    cout << "amount  =  "
                         << total_amount
                         << endl;
                }
            }
        }
        else {
            flag = 1;
        }
    }
    else {
        flag = 1;
    }
}
  
// If computer course
// category is selected
void selectedCourses()
{
    cout << "Do you wish to continue?"
         << "(for yes press (Y/y ), "
         << " if no press other letter ): ";
    cin >> c1;
    if (c1 == 'Y' || c1 == 'y') {
        cout << "Enter the respective number: ";
        cin >> selectedNum;
        if (selectedNum == 1
            || selectedNum == 2
            || selectedNum == 3
            || selectedNum == 4) {
  
            // selected C
            if (selectedNum == 1) {
                cout << "selected C Language"
                     << " course\n";
                total_amount += items["C"];
                selected_items["C"]++;
                cout << "amount  =  "
                     << total_amount
                     << endl;
            }
  
            // selected C++
            if (selectedNum == 2) {
                cout << "selected C++ Language course\n";
                total_amount += items["C++"];
                selected_items["C++"]++;
                cout << "amount  =  " << total_amount << endl;
            }
  
            // selected Java
            if (selectedNum == 3) {
                cout << "selected Java Language course\n";
                total_amount += items["Java"];
                selected_items["Java"]++;
                cout << "amount  =  " << total_amount << endl;
            }
  
            // selected python
            if (selectedNum == 4) {
                cout << "selected Python"
                     << " Language course\n";
                total_amount += items["Python"];
                selected_items["Python"]++;
                cout << "amount  =  "
                     << total_amount
                     << endl;
            }
        }
        else {
            flag = 1;
        }
    }
    else {
        flag = 1;
    }
}
  
// Driver code
int main()
{
    // function call
    customerDetails();
  
    do {
        showMenu();
        cout << "Do you wish to continue?"
             << "(for yes press (Y/y ), "
             << " if no press other letter ): ";
        char c;
        cin >> c;
        if (c == 'Y' || c == 'y') {
            cout << "Enter respective number: ";
            int num;
            cin >> num;
            if (num == 1 || num == 2
                || num == 3) {
                switch (num) {
                case 1:
  
                    // For Mobile
                    showMobileMenu();
                    selectedMobile();
                    break;
  
                case 2:
  
                    // For Laptop
                    showLaptopMenu();
                    selectedLaptop();
                    break;
  
                case 3:
  
                    // For computer course
                    showComputerCourseMenu();
                    selectedCourses();
                    break;
                }
            }
            else {
                flag = 1;
            }
        }
        else {
            flag = 1;
        }
  
    } while (flag == 0);
  
    // print bill
    printBill(items, selected_items,
              total_amount);
}


输出:

让我们假设,某人需要购买2台Redmi手机,1台HP笔记本电脑和一门Java课程。

视频输出:

示范:

步骤1:首先,构造一个映射(例如map < 字符串,long double> items) ,该映射存储产品及其成本。构造另一个地图(例如map < 字符串,long double> selected_items),该地图用于按其数量推送所选项目。然后将total_amount(存储总金额)初始化为0。使用标志并初始化为0。如果客户输入错误,则标志变为1,并通过打印带有价格的项目直接退出,然后打印总量。

第2步:询问详细信息例如-客户名称。为此,在我们的代码中构造了customerDetails()函数。 toupper()用于将字符串的所有字符转换为大写。

步骤3:向用户显示菜单。为此创建了showMenu()函数。

步骤4:询问用户是否喜欢继续。在这里使用do-while循环,此循环将继续进行,直到标志更改为1。每当标志更改为1时,它将直接打印钞票。

  1. 如果是,则他需要输入Y / y,然后要求用户从菜单中输入相应的数字。如果输入了错误的数字,则标志更改为1。
    1. 如果输入有效,则显示所选类型的产品。要求用户输入相应的号码。如果无效,则标志更改为1。
    2. 由于产品很多,因此使用了开关盒,其中的参数是用户输入的编号(项目的相应编号)。
    3. 现在执行相应的案件。首先,询问数量,然后询问用户是否确定输入的数量。如果他不确定(或)数量是否不是整数,则将再次询问他,直到同时满足两个条件。
    4. 如果他对所选产品的数量有把握,那么该产品及其数量将被推入selected_items映射中。
    5. 这个过程一直进行到标志变为1为止。
  2. 否则,他可以键入Y / y以外的任何其他字母。然后标志更改为1。

以下是一些屏幕快照,显示了当用户完成特定选择后屏幕的显示情况-

  1. 如果选择了移动设备:以下屏幕截图显示了“移动设备”菜单:

  2. 如果选择了笔记本电脑:以下屏幕截图显示了笔记本电脑菜单:
  3. 如果选择了计算机课程:以下屏幕显示“计算机课程”菜单:
  4. 如果标记更改为1,我们将使用printBill()函数打印账单。
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”