一个自动贩卖机的C++程序

今天一上午用类做了一个自动贩卖机的程序需要.h头文件和.cpp源文件。源代码如下

//candy Machine Header File
#ifndef ITEM_BASE//为避免类重复定义,需要在头文件的开头和结尾加上如这个所示
#define ITEM_BASE
class cashRegister
{
public:
	int getCurrentBalance() const;
	//该函数来显示当前的现金,返回收钞口的值
	void acceptAmount(int amountIn);
	//接受顾客的钱并更新当前的现金值
	cashRegister(int cashIn=500);
	//将总现金设定为特定的值,若没有输入值,则默认为500;
private:
	int cashOnHand;
};
class dispenserType
{
public:
	int getNoOfItems() const;
	//展示机器中某商品的数量
	int getCost() const;
	//展示某商品的价格
	void makeSale();
	//减少机器中的商品数量
	dispenserType(int setNoOfItems=50,int setCost=50);
	//将货架上商品的数量和价格设定为特定的值,若没有值输入则设定为默认50,50
private:
	int numberOfItems;//储存货架上商品的数量
	int cost;//储存商品的价格
};
#endif
下面是源文件
#include<iostream>
#include"candyMachine.h"
using namespace std;
int cashRegister::getCurrentBalance()const
{
	return cashOnHand;
}
void cashRegister::acceptAmount(int amountIn)
{
	cashOnHand+=amountIn;
}
cashRegister::cashRegister(int cashIn)
{
	if(cashIn>=0)
		cashOnHand=cashIn;
	else
		cashOnHand=500;
}
int dispenserType::getNoOfItems() const
{
	return numberOfItems;
}
int dispenserType::getCost() const
{
	return cost;
}
void dispenserType::makeSale()
{
	numberOfItems--;
}
dispenserType::dispenserType(int setNoOfItems,int setCost)
{
	if(setNoOfItems>=0)
		numberOfItems=setNoOfItems;
	else
		numberOfItems=50;
	if(setCost>=0)
		cost=setCost;
	else
		cost=50;
}
//主程序
#include<iostream>
#include"candyMachine.h"
using namespace std;
void showSelection();
void sellProduct(dispenserType& product,cashRegister& pCounter);
int main()
{
	cashRegister counter;
	dispenserType candy(100,50);
	dispenserType chips(100,65);
	dispenserType gum(75,45);
	dispenserType cookies(100,85);
	int choice;
	showSelection();
	cin>>choice;
	while(choice!=9)
	{
		switch(choice)
		{
		case 1:sellProduct(candy,counter);
			break;
		case 2:sellProduct(chips,counter);
			break;
		case 3:sellProduct(gum,counter);
			break;
		case 4:sellProduct(cookies,counter);
			break;
		default:cout<<"Invaild selection."<<endl;
		}
	showSelection();
	cin>>choice;
	}
return 0;
}
void showSelection()
{
	cout<<"***Welcome to diyhoo's candy shop***"<<endl;
	cout<<"To select an item,enter"<<endl;
	cout<<"1 for candy"<<endl;
	cout<<"2 for chips"<<endl;
	cout<<"3 for gum"<<endl;
	cout<<"4 for cookies"<<endl;
	cout<<"9 for exit"<<endl;
}
void sellProduct(dispenserType& product,cashRegister& pCounter)
{
	int amount;
	int amount2;
	if(product.getNoOfItems()>0)
	{
		cout<<"Please deposit"<<product.getCost()<<"cents"<<endl;
		cin>>amount;
		if(amount<product.getCost())
		{
			cout<<"Please deposit another"<<product.getCost()-amount<<"cents"<<endl;
			cin>>amount2;
			amount+=amount2;
		}
		if(amount>=product.getCost())
		{
			pCounter.acceptAmount(amount);
			product.makeSale();
			cout<<"Collect your item at the bottom and enjoy."<<endl;
		}
		else
			cout<<"The amount is not enough.Collect what you deposited"<<endl;
		cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*--*-*--"<<endl<<endl;
	}
	else
		cout<<"sorry,this item is sold out."<<endl;
}


  • 18
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值