自动售货机系统程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Coin类:#includeiostream#includestring#includevectorusingnamespacestd;classCoin{public:/**Constructsacoinwithagivennameandvalue@paramnthecoinname@paramvthecoinvalue*/Coin(stringn,doublev);/**Getsthecoinname.@returnthename*/stringget_name()const;/**Getsthecoinvalue@returnthevalue*/doubleget_value()const;private:stringname;doublevalue;};Coin::Coin(stringn,doublev){name=n;value=v;}stringCoin::get_name()const{returnname;}doubleCoin::get_value()const{returnvalue;}Product类:#includeiostream#includestring#includevectorusingnamespacestd;classProduct{public:/**Constructsaproductwithagivenname,priceandquantity@paramntheproductname@paramptheprice@paramqthequantity*/Product(stringn,doublep,intq);/**Getstheproductname@returnthename*/stringget_name()const;/**Getstheproductprice@returntheprice*/doubleget_price()const;/**Getstheproductquantity@returnthequantity*/intget_quantity()const;/**Addstotheproductquantity@paramamounttheamounttoadd*/voidadd_quantity(intamount);private:stringname;doubleprice;intquantity;};Product::Product(stringn,doublep,intq){name=n;price=p;quantity=q;}stringProduct::get_name()const{returnname;}doubleProduct::get_price()const{returnprice;}intProduct::get_quantity()const{returnquantity;}voidProduct::add_quantity(intamount){quantity=quantity+amount;}VendingMachine类:classVendingMachine{public:/**Constructsavendingmachinewithnocurrentproductselection.*/VendingMachine();/**Addsproducttothemachine.@paramptheproducttoadd*/voidadd_product(Productp);/**Setsthecurrentlyselectedproduct@paramnametheproductname@returntrueifthemachinehasaproductwiththegivenname*/boolselect_product(stringname);voidchaxun_product();//查询当前售货机内的商品/**Addsacointopayforthecurrentlyselectedproduct.@paramcthecointoadd@returntrueifsufficientcoinshavebeenaddedtopayfortheselectedproduct.*/booladd_coin(vectorCoincurrent_pay);/**Removesallcoinsthatwereaddedtopayforthecurrentproduct.@returnthevalueofthereturnedcoins*/doublereturn_coins();/**Removesallmoneythatwaspaidforproducts.@returnthevalueofthemoney*/doubleremove_money();doubleadd_coinbijiao(vectorCoincurrent_pay);//对投入的金钱和所购买的商品的价格进行比较doublereturn_yiyoucoins();//统计售货机中已有的货款voidsetcurrent_product();//把当前选择的商品代号置为-1intgetcurrent_product();//得到当前选择的商品代号private:vectorProductproducts;intcurrent_product;vectorCoincurrent_payment;vectorCoincoins;};VendingMachine::VendingMachine(){current_product=-1;}voidVendingMachine::add_product(Productp)//添加商品{for(inti=0;iproducts.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;}elseif(products[i].get_name()==p.get_name()&&products[i].get_price()!=p.get_price()){cout已存在该商品,与您输入的价格不同!endl;return;}}products.push_back(p);cout添加成功!endl;}boolVendingMachine::select_product(stringname)//选择商品{inti;for(i=0;iproducts.size();i++){if(products[i].get_name()==name&&products[i].get_quantity()0){current_product=i;returntrue;}else{if(products[i].get_name()==name&&products[i].get_quantity()==0){cout对不起,该商品已售完!\n;returnfalse;}}}if(i==products.size()){cout对不起,不存在该商品!endl;returnfalse;}}doubleVendingMachine::return_yiyoucoins()//统计售货机当前的金钱总额{doubletotal=0;for(inti=coins.size()-1;i=0;i--){total=total+coins[i].get_value();}returntotal;}boolVendingMachine::add_coin(vectorCoincurrent_pay)//投币购买商品{if(current_product==-1)returnfalse;for(intk=0;kcurrent_pay.size();k++){current_payment.push_back(current_pay[k]);}doubletotal=0;for(inti=0;icurrent_payment.size();i++)//计算投入的总钱数{total=total+current_payment[i].get_value();}if(total=products[current_product].get_price())//投入的钱数与所购商品的价格比较{for(inti=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;returntrue;}else{returnfalse;}}doubleVendingMachine::add_coinbijiao(vectorCoincurrent_pay){if(current_product==-1)returnfalse;doubletotal=0;for(intk=0;kcurrent_pay.size();k++){total=total+current_pay[k].get_value();}doublem=products[current_product].get_price()-total;if(m0)returnm;elsereturn-1;}doubleVendingMachine::return_coins()//统计当前投入的金钱总额{doubletotal=0;for(inti=current_payment.size()-1;i=0;i--){total=total+current_payment[i].get_value();current_payment.pop_back();}returntotal;}doubleVendingMachine::remove_money()//取钱{doubletotal=0;for(inti=coins.size()-1;i=0;i--){total=total+coins[i].get_value();coins.pop_back();}returntotal;}voidVendingMachine::chaxun_product(){if(products.size()==0){cout暂时没有添加商品!endl;}else{cout商品名\t\t价格\t\t数量endl;for(inti=0;iproducts.size();i++){coutproducts[i].get_name()\t\tproducts[i].get_price()\t\tproducts[i].get_quantity()endl;}}}voidVendingMachine::setcurrent_product(){current_product=-1;}intVendingMachine::getcurrent_product(){returncurrent_product;}Main函数:#includeiostream#includestring#includevector#includecoin.h#includeproduct.h#includemachine.husingnamespacestd;voidmain(){vectorCoincoins;vectorCoinc

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功