21

Click here to load reader

Assignment C++ Vending Machine System

Embed Size (px)

Citation preview

Page 1: Assignment C++ Vending Machine System

/******************************************************************************

******************************************************************************

VENDING MACHINE SYSTEM

******************************************************************************

******************************************************************************/

#include <cstdlib>

#include <iostream>

#include <iomanip>

using namespace std;

const int TEH_TARIK = 0;

const int NESCAFE = 1;

const int HORLICK = 2;

const int TEH_O = 3;

Page 2: Assignment C++ Vending Machine System

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;

Page 3: Assignment C++ Vending Machine System

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);

Page 4: Assignment C++ Vending Machine System

cout.precision(2);

cout<<" "<<endl<<endl;

cout<<" ********************************** "<<endl;

cout<<" * * "<<endl;

cout<<" * VENDING MACHINE * "<<endl;

cout<<" * * "<<endl;

cout<<" * Teh Tarik\t- "<<gPrice[TEH_TARIK]<<"\t- Press 1 * "<<endl;

cout<<" * Nescafe\t- "<<gPrice[NESCAFE]<<"\t- Press 2 * "<<endl;

cout<<" * Horlick\t- "<<gPrice[HORLICK]<<"\t- Press 3 * "<<endl;

cout<<" * Teh 'O'\t- "<<gPrice[TEH_O]<<"\t- Press 4 * "<<endl;

cout<<" * Print Report\t\t- Press 5 * "<<endl;

cout<<" * Terminate\t\t- Press 6 * "<<endl;

cout<<" * * "<<endl;

cout<<" ********************************** "<<endl;

}

// End of function Write_Menu

void Get_Selection(int& selection, float &price)

{

int n;

Page 5: Assignment C++ Vending Machine System

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 - "<<TEH_TARIK<<endl;

price= gPrice[TEH_TARIK];

break;

case 2:

cout<<" Your chose Nescafe - "<<NESCAFE<<endl;

price= gPrice[NESCAFE];

break;

case 3:

cout<<" Your chose Horlick - "<<HORLICK<<endl;

Page 6: Assignment C++ Vending Machine System

price= gPrice[HORLICK];

break;

case 4:

cout<<" Your chose Teh 'O' - "<<TEH_O<<endl;

price= gPrice[TEH_O];

break;

case 5:

Write_Report_Cash_Out(gCoins);

Write_Report_Drinks(gDrinks);

Write_Report_Cash_In();

price=0;

break;

case 6:

price=0;

break;

default:

price=0;

cout<<" You must enter a selection between 1 and 6, Thank You!"<<endl;

cout<<endl;

}

}

Page 7: Assignment C++ Vending Machine System

// 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 ";

Page 8: Assignment C++ Vending Machine System

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;

Page 9: Assignment C++ Vending Machine System

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;

Page 10: Assignment C++ Vending Machine System

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;

Page 11: Assignment C++ Vending Machine System

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;

Page 12: Assignment C++ Vending Machine System

gCash = gCash+money; //adding value of money into gcash

cout<<endl;

cout<<" Your change is RM "<<change<<endl;

cout<<endl;

//adding 1 into the value of gDrinks[n] for cup usage transactions record

/* four condition statement to make sure value 1 is added

into correct type of cup usage*/

if (price>=1.2)

{

gDrinks[2]++;

}

else if (price>=1.1)

{

gDrinks[1]++;

}

else if (price>=1.0)

{

gDrinks[0]++;

}

else

{

Page 13: Assignment C++ Vending Machine System

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*/

Page 14: Assignment C++ Vending Machine System

{

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;

Page 15: Assignment C++ Vending Machine System

}

// 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;

Page 16: Assignment C++ Vending Machine System

}

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

Page 17: Assignment C++ Vending Machine System

cout<<" 50 sen = "<<gCoins[0]<<" coins "<<endl;

cout<<" 20 sen = "<<gCoins[1]<<" coins "<<endl;

cout<<" 10 sen = "<<gCoins[2]<<" coins "<<endl;

if (temp[0]=gCoins[0])

{

cout<<" 50 sen = "<<gCoins[0]<<" coins "<<endl;

cout<<" 10 sen = "<<gCoins[1]<<" coins "<<endl;

cout<<" 20 sen = "<<gCoins[2]<<" coins "<<endl;

}

else if (temp[1]=gCoins[1])

{

cout<<" 10 sen = "<<gCoins[0]<<" coins "<<endl;

cout<<" 20 sen = "<<gCoins[1]<<" coins "<<endl;

cout<<" 50 sen = "<<gCoins[2]<<" coins "<<endl;

}

else if (temp[2]=gCoins[2])

{

cout<<" 20 sen = "<<gCoins[0]<<" coins "<<endl;

cout<<" 50 sen = "<<gCoins[1]<<" coins "<<endl;

cout<<" 10 sen = "<<gCoins[2]<<" coins "<<endl;

Page 18: Assignment C++ Vending Machine System

}

else

{

cout<<" 10 sen = "<<gCoins[0]<<" coins "<<endl;

cout<<" 50 sen = "<<gCoins[1]<<" coins "<<endl;

cout<<" 20 sen = "<<gCoins[2]<<" coins "<<endl;

}

cout<<endl;

cout<<" Total cash dispensed from vending machine : RM "<<cash<<endl;

cout<<endl<<endl;

gCoins[0]=temp[0];

gCoins[1]=temp[1];

gCoins[2]=temp[2];

}

// End of function Write_Report_Cash_Out

void Write_Report_Drinks(const int drinks[])

{

int total_cups;

cout<<" ************* "<<endl;

Page 19: Assignment C++ Vending Machine System

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 = "<<gDrinks[0]<<endl;

cout<<" Nescafe = "<<gDrinks[1]<<endl;

cout<<" Horlick = "<<gDrinks[2]<<endl;

cout<<" Teh 'O' = "<<gDrinks[3]<<endl;

cout<<endl;

total_cups = gDrinks[0]+gDrinks[1]+gDrinks[2]+gDrinks[3];/*Calculating the

number of cup used*/

cout<<" Total cups used = "<<total_cups<<endl;

cout<<endl<<endl;

}

// End of function Write_Report_Drinks

Page 20: Assignment C++ Vending Machine System

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 "<<gCash<<endl;

cout<<endl;

/*Calculating differences between the dispense and cash

receive to get total income.*/

total_income = gCash-((gCoins[0]*.5)+(gCoins[1]*.2)+(gCoins[2]*.1));

cout<<" Total income accumulated so far : RM "<<total_income<<endl;

cout<<endl;

cout<<" ***************************************************** "<<endl;

}

//End of function Write_Report_Cash_In