📄 main.cpp
字号:
#include <iostream>
#include <string>
#include <vector>
#include"coin.h"
#include"product.h"
#include"machine.h"
using namespace std;
/*
double VendingMachine::remove_money();
void VendingMachine::chaxun_product();
void VendingMachine::setcurrent_product();
int VendingMachine::getcurrent_product();自己加的*/
void main()
{
vector<Coin> coins;
vector<Coin> current_pay;
coins.push_back(Coin("nickel", 0.05));
coins.push_back(Coin("dime", 0.1));
coins.push_back(Coin("quarter", 0.25));
coins.push_back(Coin("rmb", 1.00));
VendingMachine machine;
bool more = true;
machine.chaxun_product();
while (more)
{
cout << "a)添加商品 x)查询商品 s)选择商品 p)投币 c)取消 e)查询当前金额 r)取款 q)退出: ";
string command;
cin>>command;
if (command == "a")
{
cout << "商品名: ";
string name;
cin>>name;
cout << "价格: ";
double price;
cin >> price;
cout << "数量: ";
int quantity;
cin >> quantity;
machine.add_product(Product(name, price, quantity));
}
else if (command == "s")
{
double total1;
total1=machine.return_yiyoucoins();
if(total1>=5000)//当售货机中的货款大于等于5000时,暂停售货
{
cout<<"对不起,现在暂停售货!"<<endl;
}
else
{
machine.setcurrent_product();
cout << "商品名: ";
string name;
cin>>name;
machine.select_product(name);
}
}
else if (command == "p")
{
if(machine.getcurrent_product()!=-1)//如果当前已选择商品,才可以投币
{
bool panduan=false;
while(!panduan)//多次投币的实现
{
cout << "所投钱币名称( 以 # 结束投币过程):";
string name;
cin>>name;
while(name!="#")
{
bool found = false;
for (int i = 0; !found && i < coins.size(); i++)
{
if (coins[i].get_name() == name)
{
current_pay.push_back(coins[i]);
found=true;
}
}
if(!found)
{
cout << "不存在该货币,请重新投入:\n";
}
cin>>name;
}
double k=machine.add_coinbijiao(current_pay );
if(k==-1)
{
machine.add_coin(current_pay);
for (int i = current_pay.size() - 1; i >= 0; i--)
{
current_pay.pop_back();
}
panduan=true;
}
else
{
cout<<"您的金额不足,还差 "<<k<<" 是否继续投币 Y N :";
string jixu;
cin>>jixu;
if(jixu!="Y")
{
machine.setcurrent_product();
double total=0;
for (int i = current_pay.size() - 1; i >= 0; i--)
{
total = total + current_pay[i].get_value();
current_pay.pop_back();
}
cout << "Returned(退还) " <<total<< "\n";
panduan=true;
}
}
}
}
else
cout<<"您还没有选择商品!"<<endl;
}
else if (command == "c")//取消操作并并退还货款
{
machine.setcurrent_product();
double total=0;
for (int i = current_pay.size() - 1; i >= 0; i--)
{
total = total + current_pay[i].get_value();
current_pay.pop_back();
}
cout << "Returned(退还) " <<total<< "\n";
}
else if (command == "r")
cout << "Removed (取走) " << machine.remove_money() << "\n";
else if(command=="x")
{
machine.chaxun_product();
}
else if(command=="e")//查询当前售货机中的货款
cout<<"当前金额:"<<machine.return_yiyoucoins()<<endl;
else if(command == "q")
more = false;
}
}
double VendingMachine::remove_money()//取钱
{
double total = 0;
for (int i = coins.size() - 1; i >= 0; i--)
{
total = total + coins[i].get_value();
coins.pop_back();
}
return total;
}
void VendingMachine::chaxun_product()
{
if(products.size()==0)
{
cout<<"暂时没有添加商品!"<<endl;
}
else
{
cout<<"商品名"<<"\t\t"<<"价格"<<"\t\t"<<"数量"<<endl;
for (int i = 0; i < products.size(); i++)
{
cout<<products[i].get_name()<<"\t\t"<<products[i].get_price()<<"\t\t"<<products[i].get_quantity()<<endl;
}
}
}
void VendingMachine::setcurrent_product()
{
current_product=-1;
}
int VendingMachine::getcurrent_product()
{
return current_product;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -