/****************************************************************************** ******************************************************************************
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&); float Get_Change(float, float); void Write_Change(float); void Write_Report_Cash_Out(const int coin[]); void Write_Report_Drinks (const int drinks[]); void 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, price); if(selection<5) { money= Get_Money(gCash); change= Get_Change(money, price); Write_Change(change); } } system("PAUSE"); return EXIT_SUCCESS; } // End of function main
void Write_Menu() { cout.setf(ios::fixed); cout.setf(ios::showpoint);
cout.precision(2);
cout<<" "<<endl<<endl; cout<<" ********************************** "<<endl; cout<<" *
* "<<endl;
cout<<" * VENDING MACHINE cout<<" *
* "<<endl;
* "<<endl;
cout<<" * Teh Tarik\t- "<
* "<<endl;
cout<<" ********************************** "<<endl; } // End of function Write_Menu
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. "<<endl; cout<<" Then press Enter. "<<endl; cout<<" Your selection > "; cin>>selection;
switch (selection) { case 1: cout<<" Your chose Teh Tarik - "<
price= 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"<<endl; cout<<" you are spending. Then press Enter."<<endl; cout<<" Your spending > RM "; cin>>money; cout<<endl;
while (money<0) /*Looping function to make sure amount entered by customers is valid*/ { cout<<" The amount you have entered is invalid!"<<endl; cout<<" Please enter a valid amount : RM ";
cin>>money; cout<<endl; }
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"<<endl; cout<<" Please take back your all your amount"<<endl; cout<<endl; cout<<" Re-enter new amount not more than RM 2.00"<<endl; cout<<" Your spending > RM "; cin>>money; cout<<endl;
while (money<0) /*Looping function to make sure amount entered by customers is valid*/ { cout<<" The amount you have entered is invalid!"<<endl; cout<<" Please enter a valid amount : RM "; cin>>money;
cout<<endl; } }
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<price) /* Looping function to make sure that amount entered by customers is enough to buy a selected drink*/ { cout<<" Your current amount is not enough to buy your selected drink "; cout<<endl;
cout<<" Your current amount : RM "<<money<<endl; cout<<endl; cout<<" Please add more amount : RM "; cin>>add; cout<<endl; while (add<0) /*Looping function to make sure amount entered by customers is valid*/ { cout<<" The amount you have entered is invalid!"<<endl; cout<<" Please enter a valid amount : RM "; cin>>add; cout<<endl; } money = money+add;// adding value of add into money
while (money>2.0) /*Looping function to make sure customers are not spending more than RM 2.00*/ { cout<<endl<<endl; cout<<" Your total spending is exceeding RM 2.00"<<endl;
cout<<" This vending machine cannot accept more than RM 2.00"<<endl; cout<<" Please take back your all your amount"<<endl; cout<<endl; cout<<" Re-enter new amount not more than RM 2.00"<<endl; cout<<" Your spending > 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!"<<endl; cout<<" Please enter a valid amount : RM "; cin>>money; cout<<endl; } cout<<endl; } } change = money-price; /*calculate the value of change to be given to the customer.*/ cout<<" Total amount entered : RM "<<money<<endl;
gCash = gCash+money; //adding value of money into gcash cout<<endl; cout<<" Your change is RM "<=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 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 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 "<<sen50<<" coins"<<endl; cout<<" 20 sen x "<<sen20<<" coins"<<endl; cout<<" 10 sen x "<<sen10<<" coins"<<endl; /*display nunber of coins dispensed by vending machine*/ cout<<endl; cout<<" Thank you. Please come again."<<endl<<endl;
} // End of function Write_Change
void 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]) indexOfMin = j;
} swap = gCoins[pass]; gCoins[pass] = gCoins[indexOfMin]; gCoins[indexOfMin] = swap; }
cout<<endl; cout<<" ***************************************************** "<<endl; cout<<" **
TRANSACTIONS RECORD
** "<<endl;
cout<<" ***************************************************** "<<endl; cout<<endl; cout<<" ************ "<<endl; cout<<" * CASH OUT * "<<endl; cout<<" ************ "<<endl; cout<<endl; cout<<" The number of coins has been given to customers : "<<endl; cout<<endl;
//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]) { 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<<" ************* "<<endl;
cout<<" * CUP USAGE * "<<endl; cout<<" ************* "<<endl; cout<<endl; cout<<" The number of cups used for each type of drinks : "<<endl; cout<<endl;
//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[2]+gDrinks[3];/*Calculating the number of cup used*/ cout<<" Total cups used = "<
void Write_Report_Cash_In() { float total_income; cout<<" *********** "<<endl; cout<<" * CASH IN * "<<endl; cout<<" *********** "<<endl; cout<<endl; //gCash store the value of total cash received by vending machine. cout<<" Total cash received from customers : RM "<
cout<<" Total income accumulated so far : RM "<
Related Documents