📄 machine.h
字号:
class VendingMachine
{
public:
/*
Constructs a vending machine with no current
product selection.
*/
VendingMachine();
/*
Adds product to the machine.
@param p the product to add
*/
void add_product(Product p);
/*
Sets the currently selected product
@param name the product name
@return true if the machine has a product with the given
name
*/
bool select_product(string name);
void chaxun_product();//查询当前售货机内的商品
/*
Adds a coin to pay for the currently selected product.
@param c the coin to add
@return true if sufficient coins have been added to pay
for the selected product.
*/
bool add_coin(vector<Coin> current_pay);
/*
Removes all coins that were added to pay for the current
product.
@return the value of the returned coins
*/
double return_coins();
/*
Removes all money that was paid for products.
@return the value of the money
*/
double remove_money();
double add_coinbijiao(vector<Coin> current_pay );//对投入的金钱和所购买的商品的价格进行比较
double return_yiyoucoins();//统计售货机中已有的货款
void setcurrent_product();//把当前选择的商品代号置为-1
int getcurrent_product();//得到当前选择的商品代号
private:
vector<Product> products;
int current_product;
vector<Coin> current_payment;
vector<Coin> coins;
};
VendingMachine::VendingMachine()
{
current_product = -1;
}
void VendingMachine::add_product(Product p)//添加商品
{
for (int i = 0; i < products.size(); i++)
{
if (products[i].get_name() == p.get_name() && products[i].get_price() == p.get_price())
{
products[i].add_quantity(p.get_quantity());
cout<<"添加成功!"<<endl;
return;
}
else if(products[i].get_name() == p.get_name() && products[i].get_price() != p.get_price())
{
cout<<"已存在该商品,与您输入的价格不同!"<<endl;
return;
}
}
products.push_back(p);
cout<<"添加成功!"<<endl;
}
bool VendingMachine::select_product(string name)//选择商品
{
int i;
for ( i = 0; i < products.size(); i++)
{
if (products[i].get_name() == name && products[i].get_quantity() > 0)
{
current_product = i;
return true;
}
else
{
if(products[i].get_name() == name && products[i].get_quantity() == 0)
{
cout << "对不起,该商品已售完!\n";
return false;
}
}
}
if(i==products.size())
{
cout<<"对不起,不存在该商品!"<<endl;
return false;
}
}
double VendingMachine::return_yiyoucoins()//统计售货机当前的金钱总额
{
double total = 0;
for (int i = coins.size() - 1; i >= 0; i--)
{
total = total + coins[i].get_value();
}
return total;
}
bool VendingMachine::add_coin(vector<Coin> current_pay )//投币购买商品
{
if (current_product == -1) return false;
for(int k=0;k < current_pay.size(); k++)
{
current_payment.push_back(current_pay[k]);
}
double total = 0;
for (int i = 0; i < current_payment.size(); i++)//计算投入的总钱数
{
total = total + current_payment[i].get_value();
}
if (total >= products[current_product].get_price())//投入的钱数与所购商品的价格比较
{
for (int i = current_payment.size() - 1; i >= 0; i--)
{
coins.push_back(current_payment[i]);
current_payment.pop_back();
}
products[current_product].add_quantity(-1);
current_product = -1;
cout<<"交易成功!"<<endl;
return true;
}
else
{
return false;
}
}
double VendingMachine::add_coinbijiao(vector<Coin> current_pay )
{
if (current_product == -1) return false;
double total = 0;
for(int k=0;k < current_pay.size(); k++)
{
total = total + current_pay[k].get_value();
}
double m=products[current_product].get_price()-total;
if (m>0)
return m;
else
return -1;
}
double VendingMachine::return_coins()//统计当前投入的金钱总额
{
double total = 0;
for (int i = current_payment.size() - 1; i >= 0; i--)
{
total = total + current_payment[i].get_value();
current_payment.pop_back();
}
return total;
}
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 + -