Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates a vending machine. The vending machine sells thre

ID: 662221 • Letter: W

Question

Write a program that simulates a vending machine.

The vending machine sells three types of food: 1) Patato chips $1.25; 2) Cookies $0.85; 3) Candies $0.95. The program will prompt for the buyer to enter the amount in quarters (25 cents), dimes (10 cents), and nickels (5 cents). The program will then present a selection menu for the foods. The buyer makes a selection. If the buyer selects a food that is sold out, the program will display a message and redisplay the selection menu. If the buyer has put in enough money, the selected food will be dispensed, with a message "Please take a your (food)". If the amount is more than the cost, an appropriate amount of change (in quarter, dime or nickel) will be dispensed.

Every food is initialized with a quantity. The quantity is reduced by one every time the food is dispensed. When the quantity of a food is 0, no more should be sold, with an error stating "Sold out. Please make another choice".

Note:

1 dollar = 4 quarters = 10 dimes = 20 nickels
1 dollar = 100 cents
1 quarter = 25 cents
1 dime = 10 cents
1 nickel = 5 cents

1. Buyer selects a food that is available, and inserts the exact amount as the price;
2   Buyer selects a food that is in stock, and inserts less money than the price;
3   Buyer selects a food that is in stock, and inserts more money than the price;
4   Buyer selects a food that is sold out;
5   Buyer selects a food that does not exist.

Explanation / Answer

//main.cpp
#include <iostream.h>
#include <stdlib.h>
#include "candy.h"

void main()
{
int sel;  
Candy c(4,50);
Chip ch(6,60);
Gum g(8,70);
Cookies co(10,80);
double pr;

do
{
//   system ("cls");
cout<<endl;
cout<<"*** Welcome to Candy Shop ***"<<endl
<<"To select an item enter"<<endl
<<"1.   for Candy"<<endl
<<"2.   for Chips"<<endl
<<"3.   for Gum"<<endl
<<"4.   for Cookies"<<endl
<<"9.   to exit"<<endl<<endl;

cin>>sel;

while ((sel !=1) && (sel !=2) && (sel !=3)
&& (sel !=4) && (sel !=9))
{
cout << "You have made an invalid selection."
<< "Please enter an item."
<<endl;
cin>>sel;
}
switch(sel)
{
case 1:
{  
if (c.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << c.GetPrice() << "cents"
<<endl;
cin >> pr;
if (pr = c.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
c.SetStock(c.GetStock()-1);
}
else if (pr > c.GetPrice())
{  
cout << "Collect your item at the bottom and Enjoy. "
<< "Your changes is:" << pr - c.GetPrice()
<< endl;
c.SetStock(c.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 2:
{
if (ch.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << ch.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr == ch.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
ch.SetStock(ch.GetStock()-1);
}
else if (pr > ch.GetPrice())
{  
cout << "Collect your item at the bottom and Enjoy. "
<< "Your changes is:" << pr - ch.GetPrice()
<< endl;
ch.SetStock(ch.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 3:
{
if (g.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << g.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr == g.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
g.SetStock(g.GetStock()-1);
}
else if (pr > g.GetPrice())
{  
cout << "Collect your item at the bottom and Enjoy. "
<< "Your changes is:" << pr - g.GetPrice()
<< endl;
g.SetStock(g.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 4:
{
if (co.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << co.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr == co.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
co.SetStock(co.GetStock()-1);
}
else if (pr > co.GetPrice())
{  
cout << "Collect your item at the bottom and Enjoy. "
<< "Your changes is:" << pr - co.GetPrice()
<< endl;
co.SetStock(co.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 9:
{
cout<<"Goodbye"<<endl;
break;
}
}
}while(sel !=9);
}

//candy.cpp
#include <iostream.h>
#include "candy.h"

Candy::Candy(int i, double d)
{
price = d;
stock = i;
}

Chip::Chip(int i, double d)
{
price = d;
stock = i;
}

Gum::Gum(int i, double d)
{
price = d;
stock = i;
}

Cookies::Cookies(int i, double d)
{
price = d;
stock = i;
}

void Candy::SetStock(int i)
{
stock = i;
}

int Candy::GetStock()
{
return stock;
}

double Candy::GetPrice()
{
return price;
}

void Chip::SetStock(int i)
{
stock = i;
}

int Chip::GetStock()
{
return stock;
}

double Chip::GetPrice()
{
return price;
}

void Gum::SetStock(int i)
{
stock = i;
}

int Gum::GetStock()
{
return stock;
}

double Gum::GetPrice()
{
return price;
}

void Cookies::SetStock(int i)
{
stock = i;
}

int Cookies::GetStock()
{
return stock;
}

double Cookies::GetPrice()
{
return price;
}

//candy.h
class Candy
{
private:
int stock;
double price;
public:
Candy(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};

class Chip
{
private:
int stock;
double price;
public:
Chip(int,double);
void SetStock(int);
int GetStock();  
double GetPrice();
};

class Gum
{
private:
int stock;
double price;
public:
Gum(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};

class Cookies
{
private:
int stock;
double price;
public:
Cookies(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote