. Use (Switch case) command.
HINT: you have to set some drinks and set them some valid values that should not exceed
more then 1$ (Avoid Negative values)
Requirements:
Following are the requirements for the above discussed problem:
a. Vending Machine must keep track of the inventory
b. A person should be able to insert cash into the machine & choose an item
c. The Machine should confirm the inserted cash with the price of the selected item
d. The machine must display an error in case of insufficient cash or unavailable item
e. Finally, if all the above steps succeed then the user gets the selected item
#include <iostream>
using namespace std;
int main(){
cout<<"***VENDING MACHINE***\n";
int coke = 1, dew = 2, choice;
float cash = 0, price = 0.5;
do{
cout<<"Pick an option\n";
cout<<"1. Show inventory\n";
cout<<"2. Insert cash\n";
cout<<"3. Select an item\n";
cout<<"0. Quit\n";
cin>>choice;
switch(choice){
case 1: cout<<"There are "<<coke + dew<<" items left\n"; break;
case 2: cout<<"Enter amount: ";
float temp;
cin>>temp;
if(temp < 0){
cout<<"No negative cash allowed\n";
}
else cash += temp;
cout<<"Available cash balance is : "<<cash<<endl;
break;
case 3: cout<<"1. Mountain Dew\t Price: $0.5\n";
cout<<"2. Coke\t Price: $0.5\n";
cin>>choice;
if(coke + dew < 1){
cout<<"No items left in the vending machine\n";
break;
}
switch(choice){
case 1: if(cash >= price){
if(dew < 1){
cout<<"Mountain Dew is not available\n";
break;
}
dew--;
cash -= price;
cout<<"Please pick your item below\n";
if(cash > 0) cout<<"Here is your "<<cash<<" change\n";
cash = 0;
}
else{
cout<<"Insufficient funds\n";
}
break;
case 2: if(cash >= price){
if(coke < 1){
cout<<"Coke is not available\n";
break;
}
coke--;
cash -= price;
cout<<"Please pick your item below\n";
if(cash > 0) cout<<"Here is your "<<cash<<" change\n";
cash = 0;
}
else{
cout<<"Insufficient funds\n";
}
break;
default: cout<<"Invalid choice\n";
}
break;
default:break;
}
}while(choice != 0);
return 0;
}
Comments
Leave a comment