Drink Machine Simulator Create a class that simulates and manages a soft drink m
ID: 3757716 • Letter: D
Question
Drink Machine Simulator
Create a class that simulates and manages a soft drink machine. Information on each drink
type should be stored in a structure that has data members to hold the drink name, the
drink price, and the number of drinks of that type currently in the machine.
The class should have an array of ve of these structures, initialized with the following data.
Drink Name Cost Number in Machine
Cola 1.00 20
Root beer 1.00 20
Orange soda 1.00 20
Grape soda 1.00 20
Bottled water 1.50 20
The class should have two public member functions, displayChoices (which displays a
menu of drink names and prices) and buyDrink (which handles a sale). The class should
also have at least two private member functions, inputMoney, which is called by buyDrink
to accept, validate, and return (to buyDrink) the amount of money input, and
dailyReport, which is called by the destructor to report how many of each drink type
remain in the machine at the end of the day and how much money was collected.
You may want to use additional functions to make the program more modular.
The client program that uses the class should have a main processing loop which calls the
displayChoices class member function and allows the patron to either pick a drink or
quit the program. If the patron selects a drink, the buyDrink class member function is
called to handle the actual sale. This function should be passed the patron’s drink choice.
Here is what the buyDrink function should do:
• Call the inputMoney function, passing it the patron’s drink choice.
• If the patron no longer wishes to make the purchase, return all input money.
• If the machine is out of the requested soda, display an appropriate “sold out” message and return all input money.
• If the machine has the soda and enough money was entered, complete the sale by updating the quantity on hand and money collected information, calculating any change due to be returned to the patron, and delivering the soda.
This last action can be simulated by printing an appropriate “here is your beverage” message.
Input Validation: Only accept valid menu choices. Do not deliver a beverage if the
money inserted is less than the price of the selected drink.
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace stddrink;
struct SoftDrink
{
SoftDrink(string n, double c, int co)
{
name = n; // for name of the drink
cost = c; // for cost of the drink
count = co; //count
}
SoftDrink()
{
name = " "; // with out arguments name of the drink with empty value
cost = 0; // cost of the drink initial with zero value
count = 0; // count
}
// Local variables declaration
string name; // Name of the drink ie: Cola,Rootbeer,orange soda,Grape soda,bottled water
double cost; // Cost ie: 1.00
int count; // Number of items in drink machine
// Function with prototypes
int displaytheChoices();
double buySoftDrink(int c); // Choice as argument so it knows what drink to process
double drinkMoney(double c); // Cost as argument so it knows how much to collect
int drinkdailyReport(double m, int i); // MoneyBack as arugment so it knows how to process quantity
};
// The Code implmentation for the functions
int SoftDrink::displaytheChoices()
{
int ch;
cout << "This is a Soft drink machine program. "
<< setw(7) << "Item" << setw(22) << "Cost "
<< "1. Cola 1.00 "
<< "2. RootBeer 1.00 "
<< "3. Orange Soda 1.00 "
<< "4. Grape Soda 1.00 "
<< "5. Bottled Water 1.50 "
<< "6. Quit Program "
<< "Enter choice: ";
cin >> ch;
// Validate the choices of soft drink
while (ch < 1 || ch > 6)
{
cout << "Please select the items 1-6"
<< "Re-enter: ";
cin >> ch;
}
return choice;
}
double SoftDrink::buySoftDrink(int ch)
{
string DrinkName;
switch (ch)
{
case 1:
DrinkName = "Cola";
break;
case 2:
DrinkName = "Root Beer";
break;
case 3:
DrinkName = "Orange Soda";
break;
case 4:
DrinkName = "Grape Soda";
break;
case 5:
DrinkName = "Bottled Water";
break;
default:
DrinkName = " ";
break;
}
cout << "You are selected " << DrinkName << ". ";
double cost,
cashback; // the cost of the argument returned back from inputMoney
if (ch >= 1 && ch <= 4)
{
cost = 1.00;
moneyBack = inputMoney(cost);
return cashback;
}
else
{
cost = 1.50;
moneyBack = inputMoney(cost);
return cashback;
}
}
double SoftDrink::drinkMoney(double cost)
{
double money = 0;
if (cost == 1.00)
{
cout << "Your item costs $" << cost << ". "
<< "Enter " << cost << " to insert the(your) cash or 0 to quit. ";
cin >> money;
if (money > cost)
{
money = money - cost;
cout << "Your change is $" << money << ".";
}
return money;
}
else if (cost == 1.00)
{
cout << "Your item costs $" << cost << ". "
<< "Enter " << cost << " to insert your cash or 0 to quit. ";
cin >> money;
if (money > cost)
{
money = money - cost;
cout << fixed << setprecision(2) << "Your change is $" << money << ". ";
}
return money;
}
if (money == 0 && money < cost)
{
money = 0;
cout << "You have decided to cancel your purchase. "
<< "It is done so. ";
return money;
}
return money;
}
int SoftDrink::drinkdailyReport(double cashback, int count)
{
if (cashback> 0)
{
cout << " Thank you for your purchase. ";
count--;
return count--;
}
else
{
cout << "back next time ";
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.