Assignment C++ Vending Machine System

  • Uploaded by: safuan_alcatra
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Assignment C++ Vending Machine System as PDF for free.

More details

  • Words: 1,333
  • Pages: 20
/****************************************************************************** ******************************************************************************

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 = "<
cout<<" Total income accumulated so far : RM "<

Related Documents