糖果机小作业

设计思路

请添加图片描述

代码

//machine.h
#include <bits/stdc++.h>
#define ll long double
using namespace std;
struct node
{
        ll purchase_price;
        ll selling_price;
        ll inventory;
};

class machine
{
public:
        ll profit;
        map<string, node> Candy;
        machine();
        void add_inventory(string, ll, ll, ll);
        void show_profit();
        void find_inventory(string);
        void sell_candy(string, ll);
};
//machine.cpp
#include <bits/stdc++.h>
#include "machine.h"
#define ll long double
using namespace std;
//admin
machine::machine()
{
        Candy.clear();
        profit = 0;
}
void machine::add_inventory(string candyname, ll x, ll y, ll num)
{
        cout << "OK!" << endl;
        if (Candy[candyname].inventory == 0)
        {
                Candy[candyname].purchase_price = x;
                Candy[candyname].selling_price = y;
        }
        Candy[candyname].inventory += num;
        return;
}
void machine::show_profit()
{
        cout << "OK!" << endl;
        cout << "The profit you get is " << profit << endl;
        return;
}
//preson
void machine::find_inventory(string candyname)
{
        cout << "OK!" << endl;
        cout << "The inventory of this candy is " << Candy[candyname].inventory << endl;
        return;
}
void machine::sell_candy(string candyname, ll num)
{
        cout << "OK!" << endl;
        cout << "You get " << num << ' ' << candyname << endl;
        profit += (Candy[candyname].selling_price - Candy[candyname].purchase_price) * num;
        Candy[candyname].inventory -= num;
        return;
}
//main.cpp
#include <bits/stdc++.h>
#include "machine.cpp"
#define ll long double
int main()
{
        cout << "please chose the operator you want to make" << endl;
        cout << "2 - Buy_candy" << endl;
        cout << "3 - Find_inventory" << endl;
        cout << "5 - Output all the candy information" << endl;
        cout << "7 - View the menu" << endl;
        cout << "8 - Administrator mode" << endl;
        machine machine1;
        bool flag = 0;
        while (1)
        {
                int opreator;
                cin >> opreator;
                if (flag)
                {
                        if (opreator == 1)
                        {
                                string candyname;
                                ll x, y, num;
                                cout << "please enter the name of the candy" << endl;
                                cin >> candyname;
                                cout << "please enter the purchase_price of the candy" << endl;
                                cin >> x;
                                cout << "please enter the selling_price of the candy" << endl;
                                cin >> y;
                                cout << "please enter the inventor of the candy" << endl;
                                cin >> num;
                                machine1.add_inventory(candyname, x, y, num);
                        }
                        else if (opreator == 4)
                        {
                                machine1.show_profit();
                        }
                        else if (opreator == 6)
                        {
                                break;
                        }
                        else if (opreator == 7)
                        {
                                cout << "1 - Add_inventory" << endl;
                                cout << "4 - show profit" << endl;
                                cout << "6 - turn off the machine" << endl;
                                cout << "9 - Exit administrator mode" << endl;
                        }
                        else if (opreator == 9)
                        {
                                flag = 0;
                                cout << "OK!" << endl;
                        }
                        else
                        {
                                cout << "invalid opreator,please enter the opreator again!" << endl;
                        }
                }
                else if (!flag)
                {
                        if (opreator == 2)
                        {
                                string candyname;
                                ll num;
                                if (machine1.Candy.size() == 0)
                                {
                                        cout << "no candies left" << endl;
                                }
                                else
                                {
                                        cout << "please enter the name of the candy you want to buy" << endl;
                                        cin >> candyname;
                                        if (machine1.Candy[candyname].inventory == 0)
                                        {
                                                cout << "No this kind of candy!" << endl;
                                                continue;
                                        }
                                        cout << "please enter the number of the candy you want to buy" << endl;
                                        cin >> num;
                                        if (num > machine1.Candy[candyname].inventory)
                                        {
                                                cout << "Out of stock!" << endl;
                                                continue;
                                        }
                                        machine1.sell_candy(candyname, num);
                                }
                        }
                        else if (opreator == 3)
                        {
                                string candyname;
                                cout << "please enter the name of the candy you want to find" << endl;
                                cin >> candyname;
                                machine1.find_inventory(candyname);
                        }
                        else if (opreator == 5)
                        {
                                if (machine1.Candy.size() == 0)
                                {
                                        cout << "no candies left" << endl;
                                }
                                for (auto x : machine1.Candy)
                                {
                                        if (x.second.inventory != 0)
                                        {
                                                cout << "Candyname : " << x.first << endl;
                                                cout << "inventory of this candy : " << x.second.inventory << endl;
                                                cout << "price of this candy : " << x.second.selling_price << endl;
                                        }
                                        else
                                                continue;
                                }
                        }
                        else if (opreator == 7)
                        {
                                cout << "2 - Buy_candy" << endl;
                                cout << "3 - Find_inventory" << endl;
                                cout << "5 - Output all the candy information" << endl;
                                cout << "7 - View the menu" << endl;
                                cout << "8 - Administrator mode" << endl;
                        }
                        else if (opreator == 8)
                        {
                                cout << "please enter the password" << endl;
                                while (1)
                                {
                                        string s;
                                        cin >> s;
                                        if (s == "777")
                                        {
                                                flag = 1;
                                                cout << "OK!" << endl;
                                                break;
                                        }
                                        else
                                        {
                                                cout << "Wrong!" << endl;
                                                cout << "if you want to do it again,please enter 1,else enter 0" << endl;
                                                string t;
                                                cin >> t;
                                                if (t == "1")
                                                {
                                                        continue;
                                                }
                                                else
                                                {
                                                        break;
                                                }
                                        }
                                }
                        }
                        else
                        {
                                cout << "invalid opreator,please enter the opreator again!" << endl;
                        }
                }
        }
        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值