Write a C program for a Vending machine that gives the exact
Write a C++ program for a Vending machine that gives the exact change for the customer.
create a 3d Array for each 5 products, in the Array store (price, quantity, and product Name).
The program must ask the user to input the amount of money, (money has to be either: 20$, 10$, 5$ or 1$ and must have a while loop to check if the amount entered is correct, and if not prompt user 3 times or display goodbye msg and exit program.
after that user will be able to choose from list of products and after choosing, a method will check if quantity is not equal to 0, and if it\'s not will display price and return exact change and minus 1 from quantity, if quantity is 0 then user is prompter to choose another product.
******************
Sample output:
\"Welcome to our Vending Machine\"
\"Please enter amount of money: \"
User enters
50>>\"please enter amount between $20 and $1\"
$10>>
\"Please chose from product list: \"
1 Pepsi $1.75 20ct
2 Coke $1.45 9 ct
3 7up $2.20 ct
4 Dr. Pepper 79c 3 ct
5 Water $1.00 15 ct
6 Mt. Dew $1.32 0ct
User chooses
4>>\" Thank you your change is:1x5 and dollar bill 4x 1 dollar bill and 2xDimes and 1x1 Penny.
6>>\"Product Unavailable Please choose a different product\".
*****************
Solution
//vendmachine.h
#ifndef VENDMACHINE_H_INCLUDED
#define VENDMACHINE_H_INCLUDED
#include<string>
class VendMachine
{
String stock[5][3];
public:
VendMachine(); //default constructor
void addStock();//add to existing stock in the machine
void getStock();//display the current stock
void getProduct(double cash);//get product code from user
void checkProduct(int opt);//check if entered product is in stock
void SellProduct(int opt,double cash);//decreases stock of sold product by 1 and returns change amount in double
void displayChange(double change);//display correct change in currency
};
#endif // VENDMACHINE_H_INCLUDED
_________________________________
//vendmachine.cpp
#include<iostream>
#include<cmath> //for modf()
#include\"VendMachine.h\"
using namespace std;
VendMachine::VendMachine()
{
cout<<\"Enter the Product Details :\"<<endl;
for(int i=0;i<5;i++)
{
cout<<\"Enter Product Name : \";
cin>>stock[i][2];
cout<<\"Enter Price : $\";
cin>>stock[i][0];
cout<<\"Enter Quantity : \";
cin>>stock[i][1];
}
}
void VendMachine::addStock()
{
int newStock=0;
cout<<\"Current Stock in the Machine :-\"<<endl;
this.getStock();
for(int i=0;i<5;i++)
{
cout<<\"Product Name : \"<<stock[i][2]<<endl;
cout<<\"Price : \"<<stock[i][0]<<endl;
cout<<\"Enter Quantity : \";
cin>>newStock;
stock[i][1]+=newStock;
}
}
void VendMachine::getStock()
{
for(int i=0;i<5;i++)
cout<<i+1<<\" \"<<stock[i][2]<<\" $\"<<stock[i][0]<<\" \"<<stock[i][1]<<\"ct\"<<endl;
}
void VendMachine::sellProduct(int opt, double cash)
{
double change;
stock[opt][1]--;
change=cash-stod(stock[opt][0]);
this.displayChange(change);
}
int VendMachine::checkProduct(int opt,double c)
{
if(stock[--opt][1]>0) //decrement opt as the array index starts from 0 and the options displayed for user start from 1
this.sellProduct(opt,c);
else
{
cout<<\"Product Unavailable. Please choose different product.\"<<endl;
getProduct(double c);
}
}
void VendMachine::getProduct(double c)
{
int opt;
cin>>opt;
this.checkProduct(opt,c);
}
void VendMachine::displayChange(double ch)
{
double d_dollars, d_cents;
int dollars, cents;
int dollarchange10,dollarchange5,dollarchange1;
int centchange50,centchange25,centchange10,centchange5,centchange1;
d_cents=modf(ch,&d_dollars); //separate dollars and cents
cents=(int)d_cents;
dollars=(int)d_dollars;
cout<<\"Your change is \";
if(dollars>=10)
{
dollarschange10=dollars/10;
dollars=dollars%10;
if(dollarschange10>0)
cout<<dollarschange<<\"X 10 dollars bill \";
if(dollars>0||cents>0)
cout<<\"and \";
}
if(dollars>=5)
{
dollarschange5=dollars/5;
dollars=dollars%5;
if(dollarschange5>0)
cout<<dollarschange<<\"X 5 dollars bill \";
if(dollars>0||cents>0)
cout<<\"and \";
}
if(dollars>=1)
{
dollarschange1=dollars/1;
dollars=dollars%1;
if(dollarschange1>0)
cout<<dollarschange<<\"X 1 dollar bill \";
if(cents>0)
cout<<\"and \";
}
if(cents>=50)
{
centschange50=cents/50;
cents%50;
if(centschange50>0)
cout<<centschange<<\"X 50 cents \";
if(cents>0)
cout<<\"and \";
}
if(cents>=25)
{
centschange25=cents/25;
cents%25;
if(centschange25>0)
cout<<centschange<<\"X 25 cents \";
if(cents>0)
cout<<\"and \";
}
if(cents>=10)
{
centschange10=cents/10;
cents%10;
if(centschange10>0)
cout<<centschange<<\"X Dimes \";
if(cents>0)
cout<<\"and \";
}
if(cents>=5)
{
centschange5=cents/5;
cents%5;
if(centschange5>0)
cout<<centschange<<\"X 5 cents \";
if(cents>0)
cout<<\"and \";
}
if(cents>=1)
{
centschange1=cents/1;
cents%1;
if(centschange1>0)
if(centschange>1)
cout<<centschange<<\"X 1 pennies \";
else
cout<<centschange<<\"X 1 penny \";
}
}
_____________
//main.cpp
#include <iostream>
#include \"VendMachine.h\"
using namespace std;
int main()
{
VendMachine m=new VendMachine();
int opt;
double cash,count=0;
cout << \"Welcome to our Vending Machine\" << endl<<\"Please enter the amount of money\"<<endl;
cin>>cash;
count++
while(cash<>20&&cash<>10&&cash<>5&&cash<>1&&count<3)
{
cout<<\"please enter amount between $1 & $5\";
count++;
}
if(count==3)
return 1;
cout<<\"Please choose from product list:\"<<endl;
m.getstock();
m.getProduct(cash);
return 0;
}



