自动售货机系统程序_第1页
自动售货机系统程序_第2页
自动售货机系统程序_第3页
自动售货机系统程序_第4页
自动售货机系统程序_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Coin 类:#include <iostream>#include <string>#include <vector> using namespace std;class Coin public:/*Constructs a coin with a given name and valueparam n the coin nameparam v the coin value*/Coin(string n, double v);/*Gets the coin name.return the name*/string get_name() const;/*Ge

2、ts the coin valuereturn the value*/double get_value() const;private:string name;double value;Coin:Coin(string n, double v)name = n;value = v;string Coin:get_name() constreturn name; double Coin:get_value() const return value;Product 类:#include <iostream>#include <string>#include <vect

3、or>using namespace std;class Productpublic:Constructs a product with a given name, price and quantityparam n the product nameparam p the priceparam q the quantity*/Product(string n, double p, int q);/*Gets the product namereturn the name*/string get_name() const;/*Gets the product pricereturn the

4、 price*/double get_price() const;/*Gets the product quantityreturn the quantity*/int get_quantity() const;/*Adds to the product quantityparam amount the amount to add*/void add_quantity(int amount);private:string name;double price;int quantity;Product:Product(string n, double p, int q) name = n;pric

5、e = p;quantity = q;string Product:get_name() constreturn name;double Product:get_price() constreturn price;int Product:get_quantity() constreturn quantity;void Product:add_quantity(int amount)quantity = quantity + amount;VendingMachine 类:class VendingMachine public:/*Constructs a vending machine wit

6、h 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 productparam name the product namereturn true if the machine has a product with the given name*/bool select_product(string name);void

7、chaxun_product();查询当前售货机内的商品Adds a coin to pay for the currently selected product.param c the coin to addreturn 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 currentproduct.ret

8、urn 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();current_pay );/对投入的金钱和所购买的商品的价格进行double add_coinbijiao(vector<Coin>比较double return_yiyoucoins(); 统计售货机中已有的货款void setcurrent_product();把

9、当前选择的商品代号置为-1int 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.s

10、ize(); i+)if (productsi.get_name() = p.get_name() && productsi.get_price() = p.get_price()productsi.add_quantity(p.get_quantity();cout«"添加成功! "vvendl;return;!=else if(productsi.get_name() = p.get_name() && productsi.get_price() p.get_price()coutvv"已存在该商品,与您输入的价格不同

11、!"vvendl;return;products.push_back(p);coutvv"添加成功!"vvendl;bool VendingMachine:select_product(string name)/选择商品int i;for ( i = 0; i v products.size(); i+)if (productsi.get_name() = name && productsi.get_quantity() > 0)current_product = i;return true;elseif(productsi.get_name

12、() = name && productsi.get_quantity() = 0) cout << "对不起,该商品已售完!n"return false;if(i=products.size()coutvv"对不起,不存在该商品!"vvendl;return false;double VendingMachine:return_yiyoucoins()统计售货机当前的金钱总额double total = 0;for (int i = coins.size() - 1; i >= 0; i-)total = total

13、+ coinsi.get_value();return total;bool VendingMachine:add_coin(vector<Coin> current_pay )/投币购买商品if (current_product = -1) return false;current_payment.push_back(current_payk);for(int k=0;k < current_pay.size(); k+)double total = 0;for (int i = 0; i < current_payment.size(); i+)计算投入的总钱数to

14、tal = total + current_paymenti.get_value();if (total >= productscurrent_product.get_price()投入的钱数与所购商品的价格比较for (int i = current_payment.size() - 1; i >= 0; i-)coins.push_back(current_paymenti);current_payment.pop_back();productscurrent_product.add_quantity(-1);current_product = -1;coutvv"交

15、易成功!"endl;return true;elsereturn false;double VendingMachine:add_coinbijiao(vector<Coin> current_pay )if (current_product = -1) return false;double total = 0;total = total + current_payk.get_value();double m=productscurrent_product.get_price()-total;if (m>0)return m;elsereturn -1;doubl

16、e VendingMachine:return_coins()统计当前投入的金钱总额double total = 0;for (int i = current_payment.size() - 1; i >= 0; i-)total = total + current_paymenti.get_value();current_payment.pop_back();return total;double VendingMachine:remove_money()取钱double total = 0;for (int i = coins.size() - 1; i >= 0; i-)t

17、otal = total + coinsi.get_value(); coins.pop_back();return total;void VendingMachine:chaxun_product()if(products.size()=0)cout«"暂时没有添加商品!"<<endl;elsecoutvv"商品名"vv"tt"vv" 价格"vv"tt"vv" 数量"vvendl;for (int i = 0; i < products.size

18、(); i+)coutvvproductsi.get_name()vv"tt"vvproductsi.get_price()vv"tt"vvproduc tsi.get_quantity()v<endl;void VendingMachine:setcurrent_product()current_product=-1;int VendingMachine:getcurrent_product()return current_product;Main函数:#include <iostream>#include <string>

19、;#include <vector>#include"coin.h"#include"product.h"#include"machine.h"using namespace std;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_bac

20、k(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 << "商品

21、名:";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(tota

22、l1>=5000) 当售货机中的货款大于等于5000时,暂停售货coutvv"对不起,现在暂停售货!"vvendl;elsemachine.setcurrent_product();cout << "商品名:"string name;cin>>name;machine.select_product(name);else if (command = "p")if(machine.getcurrent_product()!=-1)如果当前已选择商品,才可以投币bool panduan=false;while(

23、!panduan)多次投币的实现cout << "所投钱币名称(以#结束投币过程):"; string name;cin»name;while(name!="#")bool found = false;for (int i = 0; !found && i < coins.size(); i+)if (coinsi.get_name() = name)current_pay.push_back(coinsi); found=true;if(!found)cout << "不存在该货币,请重

24、新投入: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;elsecoutvv"您的金额不足,还差 "vvkvv"是否继续投币 Y N :"string jixu;cin»jixu;if(jixu!="

25、Y")machine.setcurrent_product();double total=0;for (int i = current_pay.size() - 1; i >= 0; i-)total = total + current_payi.get_value(); current_pay.pop_back();cout vv "Returned( 退还)"vvtotalvv "n"panduan=true;elsecoutvv"您还没有选择商品!"vvendl;else if (command = "

26、c")/取消操作并并退还货款machine.setcurrent_product();double total=0;for (int i = current_pay.size() - 1; i >= 0; i-)total = total + current_payi.get_value(); current_pay.pop_back();cout << "Returned( 退还)"wtotalvv "n"else if (command = "r")cout vv "Removed ( 取走)

27、"vv machine.remove_money() vv "n"else if(command="x")machine.chaxun_product();else if(command="e")/查询当前售货机中的货款coutvv"当前金额:"vvmachine.return_yiyoucoins()vvendl;else if(command = "q")more = false;取钱double VendingMachine:remove_money()/double total

28、 = 0;for (int i = coins.size() - 1; i >= 0; i-) total = total + coinsi.get_value(); coins.pop_back();return total;void VendingMachine:chaxun_product()if(products.size()=O)cout«"暂时没有添加商品!"<<endl; else coutvv"商品名"vv"tt"vv" 价格"vv"tt"vv&quo

29、t; 数量"vvendl;for (int i = 0; i < products.size(); i+)coutvvproductsi.get_name()vv"tt"vvproductsi.get_price()vv"tt"vvproductsi.get_quantity()v<endl;void VendingMachine:setcurrent_product()current_product=-1;int VendingMachine:getcurrent_product()return current_product;J

30、AVA import Input.touqian;import java.*;class shangpinString name;int num;public shangpin(String name,int num) =name; this.num=num;class fenpeiqidouble price;shangpin sp=new shangpin3; public fenpeiqi(double price) this.price=price; class xianshichanpinfenpeiqi fpq=new fenpeiqi3;public xians

31、hichanpin()fpq0】=new fenp eiqi(3.0);fpq0.sp0=new shangpin("玉米烤肠",10);fpq0.sp1=new shangpin("可口可乐",15);fpq0.sp2=new shangpin("百事可乐",10);fpq1=new fenpeiqi(5.0);fpq1.sp0=new shangpin("哈德门香烟",10);fpq1.sp1=new shangpin("将军香烟",15);fpq1.sp2=new shangpin(&qu

32、ot;红梅香烟",10);fpq2=new fenpeiqi(10.0);fpq2.sp0=new shangpin(" 一支笔香烟",10);fpq2.sp1=new shangpin("红塔山香烟",10);fpq2.sp2=new shangpin("泰山香烟",15);void show()System.out.println("=");System.out.println("#");System.out.println("#");System.out.pr

33、intln("#欢迎使用自动售货机#");System.out.println("#");System.out.println("#");System.out.println("=");System.out.println();System.out.println(” 编号 tt"+"名称 ttt"+"价格 tt"+"数量 t");for(int i=O;ivfpq.length;i+)if(fpqi!=null)for(int j=O;jvfpqi.sp.length;j+)if(fpqi.spj!=null)System.out.println(i+1)*10+j+1+"tt"++"tt"+fpqi.price+"tt"+fpqi.spj.num);System.out.println("=");class panduanboolean ying()double y=touq

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论