⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 candymachine.cpp

📁 Candy Mechine on c
💻 CPP
字号:
#include <iostream>

#include "cashRegister.h"

#include "dispenserType.h"

using namespace std;

void showSelection();
void sellProduct(dispenserType& product, cashRegister& pCounter);

int main()
{
	cashRegister counter;						//Step 1
	dispenserType candy(100,50); 				//Step 2
	dispenserType chips(100,65);				//Step 2
	dispenserType gum(75,45);					//Step 2
	dispenserType cookies(100,85);				//Step 2

	int choice;  								//Step 3

	showSelection();							//Step 4
	cin>>choice;								//Step 5

	while(choice != 9)							//Step 6
	{
		switch(choice)							//Step 6a
		{
		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<<"Bad Selection"<<endl;
		}//end switch

		showSelection();						//Step 6b
		cin>>choice;							//Step 6c
	}//end while

	return 0;
}//end main


void showSelection()
{
	cout<<"*** Welcome to Shelly'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 to exit"<<endl;
}//end showSelection


void sellProduct(dispenserType& product, cashRegister& pCounter)
{
	int amount;  //variable to hold the amount entered
	int amount2; //variable to hold the extra amount needed

	if(product.count() > 0)  							//Step a
	{
		cout<<"Please deposit "<<product.productCost()
			<<" cents"<<endl;							//Step a.i
		cin>>amount;									//Step a.ii

		if(amount < product.productCost())				//Step a.iii
		{
			cout<<"Please deposit another "
				<<product.productCost() - amount		//Step a.iii.1
				<<" cents"<<endl;				
			cin>>amount2;								//Step a.iii.2
			amount = amount + amount2;					//Step a.iii.3
		}

		if(amount >= product.productCost())				//Step a.iv
		{
			pCounter.acceptAmount(amount);				//Step a.iv.1
			product.makeSale();							//Step a.iv.2
			cout<<"Collect your item at the bottom"
				<<" and enjoy."<<endl; 					//Step a.iv.3
		}
		else
			cout<<"The amount is not enough. " 
				<<"Collect what you deposited."<<endl;  //Step a.v

		cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
			<<endl<<endl;
	}
	else
		cout<<"Sorry this item is sold out."<<endl;		//Step b
}//end sellProduct


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -