/************************************ /***************************************************** *********************************** ************************* ******* ************************************ ***************************************************** *********************************** ************************* *******
VENDING MACHINE SYSTEM
************************************ ***************************************************** *********************************** ************************* ******* ************************************ ***************************************************** *********************************** *************************/ *******/
#include
#include #include
using namespace std;
const int TEH_TARIK = 0; const int NESCAFE = 1; const int HORLICK = 2; const int TEH_O = 3;
float gPrice[4] = {1,1.1,1.2,.9}; int gDrinks[4] = {0,0,0,0}; int gCoins[4] = {0,0,0,0}; float gCash = 0;
void Write_Menu(); void Get_Selection (int&, float&); float Get_Money(float&); Get_Money(float&); float Get_Change(float, float); void Write_Change(float); Write_Change(float); void Write_Report_Cash_Out(const Write_Report_Cash_Out(const int coin[]); void Write_Report_Drinks (const int drinks[]); void Write_Report_Cash_In(); Write_Report_Cash_In();
int main(int argc, char *argv[])
{ int selection=0; float price; float money; float change;
while(selection !=6) { Write_Menu(); Get_Selection(selection, Get_Selection(selectio n, price); if(selection<5) { money= Get_Money(gCash); Get_Money(gCash); change= Get_Change(money Get_Change(money,, price); Write_Change(change); } } system("PAUSE"); return EXIT_SUCCESS; EXIT_SUCCESS; } // End of function main
void Write_Menu() { cout.setf(ios::fixed); cout.setf(ios::showpoint);
cout.precision(2);
cout<<" "<
* "<
cout<<" * VENDING MACHINE cout<<" *
* "<
* "<
cout<<" * Teh Tarik\t- "<
* "<
cout<<" ********************************* *********************************** "<
void Get_Selection(int& selection, float &price) { int n;
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
cout<<" Please enter your selection. "< "; cin>>selection;
switch (selection) { case 1: cout<<" Your chose Teh Tarik - "<
price= gPrice[HORLICK]; gPrice[HORLICK]; break; case 4: cout<<" Your chose Teh 'O' - "<
// End of function Get_Selection
float Get_Money (float& cash) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
float money;
cout<<" Please enter the RM amount"< RM "; cin>>money; cout<
while (money<0) /*Looping function to make sure amount entered by customers is valid*/ { cout<<" The amount you have entered is invalid!"<
cin>>money; cout<
while (money>2.0) /*Looping function to make sure customers are not spending more than RM 2.00*/ { cout<<" This vending machine cannot accept more than RM 2.00"< RM "; cin>>money; cout<
while (money<0) /*Looping function to make sure amount entered by customers is valid*/ { cout<<" The amount you have entered is invalid!"<>money;
cout<
return money; } // End of function Get_Money
float Get_Change(float money, float price) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
float change, add;
while (money
cout<<" Your current amount : RM "<>add; cout<>add; cout<
while (money>2.0) /*Looping function to make sure customers are not spending more than RM 2.00*/ { cout<
cout<<" This vending machine cannot accept more than RM 2.00"< RM "; cin>>money;
while (money<0) /*Looping function to make sure customers are not spending more than RM 2.00*/ { cout<<" The amount you have entered is invalid!"<>money; cout<
gCash = gCash+money; //adding value of money into gcash cout<=1.2) { gDrinks[2]++; } else if (price>=1.1) { gDrinks[1]++; } else if (price>=1.0) { gDrinks[0]++; } else {
gDrinks[3]++; } return change; } // End of function Get_Change
void Write_Change(float change) { int sen50=0, sen20=0, sen10=0;
while(change>=0.5) /*looping function to control the number of 50 sen coins regarding the denominations order*/ { sen50++; gCoins[0]++; change = change-0.5; }
while (change>0.19&&change<0.5)/*looping (change>0.19&&change<0.5)/*looping function to control the number of 20 sen coins regarding the denominations order*/
{ sen20++; gCoins[1]++; change = change-0.2; }
while (change>0.09&&change<0.2)/*looping (change>0.09&&change<0.2)/*looping function to control the number of 10 sen coins regarding the denominations order*/ { sen10++; gCoins[2]++; change = change-0.1; } //Value of change now is zero
cout<<" 50 sen x "<
} // End of function Write_Change
void Write_Report_Cash_Out(const Write_Report_Cash_Out(const int coins[]) { float cash; int k, indexOfMin, pass, j, swap, temp[3]={0,0,0}; const int max=3; temp[0]=coins[0]; temp[1]=coins[1]; temp[2]=coins[2]; cash = (coins[0]*.5)+(coins[1]*.2)+(coins[2]*.1); /*Calculate the value of cash dispensed*/ for (pass = 0; pass < max - 1; pass++) { indexOfMin = pass;
for (j = pass + 1; j < max; j++){ if (gCoins[j] < gCoins[indexOfMin]) gCoins[indexOfMin]) indexOfMin = j;
} swap = gCoins[pass]; gCoins[pass] = gCoins[indexOfMin]; gCoins[indexOfMin]; gCoins[indexOfMin] gCoins[indexOfMin] = swap; }
cout<
TRANSACTIONS TRANSACTIONS RECORD
** "<
cout<<" ********************************* *************************************************** ******************** ** "<
//gCoins[] store the number of coins has been dispensed from vending machine //display nunber of coins dispensed by vending machine
cout<<" 50 sen = "<
if (temp[0]=gCoins[0]) (temp[0]=gCoins[0]) { cout<<" 50 sen = "<
} else { cout<<" 10 sen = "<
} // End of function Write_Report_Cash_Out
void Write_Report_Drinks(const int drinks[]) { int total_cups; cout<<" ************* "<
cout<<" * CUP USAGE * "<
//gDrinks[] store the number of cups has been used //display nunber of cups has been used by vending machine cout<<" Teh Tarik = "<
total_cups = gDrinks[0]+gDrinks[1] gDrinks[0]+gDrinks[1]+gDrinks[2]+gDrinks[ +gDrinks[2]+gDrinks[3];/*Calculating 3];/*Calculating the number of cup used*/ cout<<" Total cups used = "<
void Write_Report_Cash_In() Write_Report_Cash_In() { float total_income; cout<<" *********** "<
cout<<" Total income accumulated so far : RM "<