自动售货机系统程序_自动售货机完整程序

2020-02-28 其他范文 下载本文

自动售货机系统程序由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“自动售货机完整程序”。

Coin类:

#include #include #include

using namespace std;

cla Coin { public: /** Constructs a coin with a given name and value @param n the coin name @param v the coin value */ Coin(string n, double v);/** Gets the coin name.@return the name */ string get_name()const;/** Gets the coin value @return 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()const { return name;}

double Coin::get_value()const { return value;}

Product类: #include #include #include

using namespace std;

cla Product { public: /** Constructs a product with a given name, price and quantity @param n the product name @param p the price @param q the quantity */ Product(string n, double p, int q);/** Gets the product name @return the name */ string get_name()const;/** Gets the product price @return the price */ double get_price()const;/** Gets the product quantity @return the quantity */ int get_quantity()const;/** Adds to the product quantity @param 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;price = p;quantity = q;}

string Product::get_name()const { return name;}

double Product::get_price()const { return price;}

int Product::get_quantity()const { return quantity;}

void Product::add_quantity(int amount){ quantity = quantity + amount;}

VendingMachine类:

cla 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 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 current_pay);//对投入的金钱和所购买的商品的价格进行比较

double return_yiyoucoins();//统计售货机中已有的货款 void setcurrent_product();//把当前选择的商品代号置为-1 int getcurrent_product();//得到当前选择的商品代号 private: vector products;int current_product;vector current_payment;vector coins;};

VendingMachine::VendingMachine(){ current_product =-1;}

void VendingMachine::add_product(Product p)//添加商品 { for(int i = 0;i

{

cout

return;

} } products.push_back(p);cout

bool VendingMachine::select_product(string name)//选择商品 {

int i;for(i = 0;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

double VendingMachine::return_yiyoucoins()//统计售货机当前的金钱总额 { double total = 0;for(int i = coins.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

{ return false;} }

double VendingMachine::add_coinbijiao(vector current_pay){ if(current_product ==-1)return false;double total = 0;for(int k=0;k

} 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 + coins[i].get_value();coins.pop_back();} return total;}

void VendingMachine::chaxun_product(){

if(products.size()==0){ cout

void VendingMachine::setcurrent_product(){ current_product=-1;}

int VendingMachine::getcurrent_product(){ return current_product;}

Main函数:

#include #include #include #include“coin.h” #include“product.h” #include“machine.h” using namespace std;

void main(){ vector coins;vector 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 >command;if(command == “a”){ cout >name;

cout > price;cout > quantity;machine.add_product(Product(name, price, quantity));} else if(command == “s”){

double total1;

total1=machine.return_yiyoucoins();

if(total1>=5000)//当售货机中的货款大于等于5000时,暂停售货

{

cout

}

else

{ machine.setcurrent_product();cout >name;machine.select_product(name);

} } else if(command == “p”){

if(machine.getcurrent_product()!=-1)//如果当前已选择商品,才可以投币

{ bool panduan=false;while(!panduan)//多次投币的实现

{ cout >name;while(name!=“#”){ bool found = false;for(int i = 0;!found && i >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--){ total = total + current_pay[i].get_value();current_pay.pop_back();} cout

else

cout

{

machine.setcurrent_product();

double total=0;

for(int i = current_pay.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

void VendingMachine::setcurrent_product(){ current_product=-1;}

int VendingMachine::getcurrent_product(){ return current_product;} }

JAVA

import Input.touqian;import java.*;cla shangpin {

} cla fenpeiqi {

}

cla xianshichanpin {

fenpeiqi fpq[]=new fenpeiqi[3];public xianshichanpin(){

fpq[0]=new fenpeiqi(3.0);fpq[0].sp[0]=new shangpin(“玉米烤肠

”,10);fpq[0].sp[1]=new shangpin(“可口可乐

”,15);fpq[0].sp[2]=new shangpin(“百事可乐

”,10);fpq[1]=new fenpeiqi(5.0);fpq[1].sp[0]=new shangpin(“哈德门香烟”,10);fpq[1].sp[1]=new shangpin(“将军香烟

”,15);fpq[1].sp[2]=new shangpin(“红梅香烟

”,10);fpq[2]=new fenpeiqi(10.0);fpq[2].sp[0]=new shangpin(“一支笔香烟”,10);fpq[2].sp[1]=new shangpin(“红塔山香烟”,10);fpq[2].sp[2]=new shangpin(“泰山香烟

”,15);double price;shangpin sp[]=new shangpin[3];public fenpeiqi(double price){ } this.price=price;String name;int num;public shangpin(String name,int num){

} this.name=name;this.num=num;

#“);#”);#“);#”);#“);

} } void show(){ System.out.println(”===============

System.out.println(“#

System.out.println(”#

System.out.println(“#

欢迎使用自动售货机

System.out.println(”#

System.out.println(“#

=================”);System.out.println(“===============

System.out.println();System.out.println(”编号tt“+”名称ttt“+”价格tt“+”数量t“);for(int i=0;i

} if(fpq[i]!=null){

} for(int j=0;j

} } if(fpq[i].sp[j]!=null){ =================”);System.out.println((i+1)*10+j+1+“tt”+fpq[i].sp[j].name+“tt”+fpq[i].price+“tt”+fpq[i].sp[j].num);System.out.println(“=============== } =================”);cla panduan { boolean ying()

} {

} boolean zhi(){

}

int z=touqian.in();

if(z==5||z==10){ } else { } return false;return true;double y=touqian.dou();if(y==1||y==0.5){ } else { } return false;return true;cla jiabi {

switch(touqian.in())void ceshi(){ System.out.println(“请选择投放钱的类型 ”);System.out.println(“1 表示投放 硬币”);System.out.println(“2 表示投放 纸币”);System.out.println(“请输入您选择的钱的类型 :”);

panduan pd=new panduan();

{ case 1:

System.out.println(“请您投入有效的面值”);

if(pd.ying())

{

System.out.println(“您投入的是有效地硬币”);

System.out.println();

// System.out.println(“^_^ 请选择您所需要的物品^_^”);

}

else

}

cla chanpin {

{

System.out.println(“您投入的不是有效地硬币,请您重新投入”);

}

break;

case 2 :

System.out.println(“请您投入有效的面值”);

if(pd.zhi())

{

//

}

else

{

}

break;

default :

System.out.println(“请您重新投入有效地货币”);

} }

System.out.println(“您投入的不是有效的纸币,请您重新投入”);System.out.println(“您投入的是有效的纸币”);System.out.println();System.out.println(“^_^ 请选择您所需要的物品^_^”);

} } public static void main(String args[]){

System.out.println(“请输入您想要产品的编码:”);int bianma;bianma=touqian.in();System.out.println(“请输入您想要产品的数量:”);int shuliang;shuliang=touqian.in();System.out.println(“您一共要付:”);double sumjine;sumjine= xianshichanpin xscp=new xianshichanpin();xscp.show();

《自动售货机系统程序.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
自动售货机系统程序
点击下载文档
相关专题 自动售货机完整程序 售货机 程序 系统 自动售货机完整程序 售货机 程序 系统
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文