1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include<string>
using namespace std;
struct DrinkInfo
{ string drinkName;
float drinkPrice;
int drinkQuantity;
};
#ifndef DRINK_H
#define DRINK_H
class DrinkMachine
{
private:
DrinkInfo drinkArray[5];
void inputMoney(short);
void dailyreport();
public:
DrinkMachine();
void displayChoises();
void buyDrink(short);
~DrinkMachine();
};
#endif
DrinkMachine::DrinkMachine()
{ drinkArray[5] = {DrinkInfo("Cola", 0.75,20),DrinkInfo("Root beer",0.75,20),
DrinkInfo("Orange Soda",0.75,20),DrinkInfo("Grape Soda",0.75,20),
DrinkInfo("Bottled Water",0.75,20)};
}
void DrinkMachine::displayChoises()
{
cout << "\tDRINK " << "PRICE" << endl;
for(int index = 0;index < 5;index++)
{
cout << "\t" << (index+1) <<"."<< setw(10) << drinkArray[index].drinkName << "$"
<< drinkArray[index].drinkPrice << endl;
}
}
//buyDrink() function definiton
//calls the input money if the soda is avaliable
//otherwise displays a sorry message
void DrinkMachine::buyDrink(short choise)
{
if(drinkArray[choise-1].drinkQuantity > 0)
inputMoney(choise);
else
cout << "The specific soda has run out.We are sorry!\n";
}
void DrinkMachine::inputMoney(short sodaType)
{
float moneyEntered = 0;
float totalMoney = 0;
cout << "PRICE $" << drinkArray[sodaType -1].drinkPrice;
while(totalMoney < drinkArray[sodaType -1].drinkPrice)
{
cout << "Please insert money or press 0 to esape.\n";
cin >> moneyEntered;
totalMoney += moneyEntered;
if(totalMoney == drinkArray[sodaType -1].drinkPrice)
{
cout << "Here is your drink.Thank You!" << endl;
break;
}
else if(totalMoney > drinkArray[sodaType -1].drinkPrice)
{ float change = totalMoney - drinkArray[sodaType -1].drinkPrice;
cout << "You change is " << change << endl;
cout << "Here is your drink.Thank you!" << endl;
break;
}
if(moneyEntered == 0)
{
cout << "Purchase never fiished!\n";
cout << "Here is your $" << totalMoney << " back.\n";
break;
}
}
}
| |