Question

In: Computer Science

C++ Drink Machine Simulator Create a class that simulates and manages a soft drink machine. Information...

C++

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 five 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.50 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.

TEST CASE1:

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 1
How much money has been inserted: $5
Do you still want to make a purchase? (Y / N) : y
Here is your Cola, and your change of $4.00

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 2
How much money has been inserted: $6
Do you still want to make a purchase? (Y / N) : y
Here is your Root Beer, and your change of $5.00

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 3
How much money has been inserted: $4
Do you still want to make a purchase? (Y / N) : y
Here is your Orange Soda, and your change of $3.00

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 4
How much money has been inserted: $8
Do you still want to make a purchase? (Y / N) : y
Here is your Grape Soda, and your change of $6.50

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 5
How much money has been inserted: $3
Do you still want to make a purchase? (Y / N) : y
Here is your Bottled Water, and your change of $1.50

Drink Machine Menu
1. Cola          : $1.00
2. Root Beer     : $1.00
3. Orange Soda   : $1.00
4. Grape Soda    : $1.50
5. Bottled Water : $1.50
6. Quit Drink Machine 
Please make a selection : 6
Thank you for shopping!

Drink Machine Daily Report
Cola           : 19
Root Beer      : 19
Orange Soda    : 19
Grape Soda     : 19
Bottled Water  : 19
Total amount collected : $6.00

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <iostream>
#include <iomanip>

using namespace std;

// Creating a Struct of Type Drink
struct Drink
{
// Declaring variables
string name;
double price;
int noOfDrinks;
};

// Creating a class
class SoftDrinkMachine
{
private:
// Declaring instance variables
Drink* drinks;

// This function will prompt to enter the valid amount
double inputMoney(int choice)
{
double amt = 0.0;

cout << "How much money has been inserted:$";
while (true)
{

cin >> amt;
if (amt < drinks[choice - 1].price)
{
cout << "Not enough money,please enter $" << drinks[choice - 1].price
<< " or more:";
}
else
break;
}

return amt;
}
void dailyReport()
{
double totAmt = 0.0;
cout << "\nDrink Machine Daily Report" << endl;
for (int i = 0; i < 5; i++)
{
cout << setw(15) << left << drinks[i].name << setw(10) << left << drinks[i].noOfDrinks
<< endl;
totAmt += (20 - drinks[i].noOfDrinks) * drinks[i].price;
}
cout << "Total Amount collected: $" << totAmt << endl;
}

public:
// Zero argumented constructor
SoftDrinkMachine()
{
// Creating struct array
drinks = new Drink[5];
Drink cola;
cola.name = "Cola";
cola.price = 1.00;
cola.noOfDrinks = 20;
drinks[0] = cola;

Drink rootBeer;
rootBeer.name = "Root beer";
rootBeer.price = 1.00;
rootBeer.noOfDrinks = 20;
drinks[1] = rootBeer;

Drink orange;
orange.name = "Orange Soda";
orange.price = 1.00;
orange.noOfDrinks = 20;
drinks[2] = orange;

Drink grape;
grape.name = "Grape Soda";
grape.price = 1.50;
grape.noOfDrinks = 20;
drinks[3] = grape;

Drink water;
water.name = "Bottled";
water.price = 1.50;
water.noOfDrinks = 20;
drinks[4] = water;
}

int displayChoice()
{
int choice;
cout << "\n";
for (int i = 0; i < 5; i++)
{
cout << (i + 1) << "." << setw(15) << left << drinks[i].name << ":$" << setw(10) << left
<< drinks[i].price << endl;
}
cout << "6. Quit Drink Machine" << endl;
cout << "Please make a selection: ";
cin >> choice;
return choice;
}

void buyDrink(int choice)
{
char ch;
double change = 0.0;
double amt = inputMoney(choice);
cout << "Do you still want to make a purchase? (Y / N) : ";
while (true)
{
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
change = amt - drinks[choice - 1].price;
drinks[choice - 1].noOfDrinks--;

cout << "Here is your " << drinks[choice - 1].name << ", and your change of $"
<< change << endl;

break;
}
else if (ch == 'N' || ch == 'n')
{
break;
}
else
{
cout << "Please enter Y or N:";
}
}
}

// Destructor
~SoftDrinkMachine()
{
dailyReport();
}
};
int main()
{
int choice;
// setting the precision to two decimal places
std::cout << std::setprecision(2) << std::fixed;

SoftDrinkMachine sdm;

/* This while loop continues to execute
* until the user enters a valid number or choice is 6
*/
while (true)
{
choice = sdm.displayChoice();
if (choice == 6)
{
break;
}
else
{
sdm.buyDrink(choice);
}
}


return 0;
}

===========================================

===========================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

write cplus programs for exercises Drink Machine Simulator Create a class that simulates and manages a...
write cplus programs for exercises 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 five of these structures, initialized with the following data. Drink Name      Cost   Number in Machine Cola       1.00  ...
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
The amounts a soft drink machine is designed to dispense for each drink are normally​ distributed,...
The amounts a soft drink machine is designed to dispense for each drink are normally​ distributed, with a mean of 11.9 fluid ounces and a standard deviation of 0.2 fluid ounce. A drink is randomly selected. ​(a) Find the probability that the drink is less than 11.7 fluid ounces. ​(b) Find the probability that the drink is between 11.6 and 11.7 fluid ounces. ​(c) Find the probability that the drink is more than 12.3 fluid ounces. Can this be considered...
A soft drink machine is regulated so that the amount of drink dispensed is approximately normally...
A soft drink machine is regulated so that the amount of drink dispensed is approximately normally distributed with a standard deviation equal to 1.5 deciliters. Find the 95% confidence interval for the mean of all soft drinks dispensed by this machine if a random sample of 36 drinks had an average content of 22.5 deciliters. What statistical table to be used? What is the tabular value? What is the computed lower confidence limit? What is the computed upper confidence limit?
The amounts a soft drink machine is designed to dispense for each drink are normally​ distributed,...
The amounts a soft drink machine is designed to dispense for each drink are normally​ distributed, with a mean of 11.711.7 fluid ounces and a standard deviation of 0.30.3 fluid ounce. A drink is randomly selected. ​(a) Find the probability that the drink is less than 11.511.5 fluid ounces. ​(b) Find the probability that the drink is between 11.311.3 and 11.511.5 fluid ounces. ​(c) Find the probability that the drink is more than 12.312.3 fluid ounces. Can this be considered...
The amounts a soft drink machine is designed to dispense for each drink are normally distributed,...
The amounts a soft drink machine is designed to dispense for each drink are normally distributed, with a mean of 11.7 fluid ounces and a standard deviation of 0.2 fluid ounce. A drink is randomly selected. (a) Find the probability that the drink is less than 11.6 fluid ounces. (b) Find the probability that the drink is between 11.5 and 11.6 fluid ounces. (c) Find the probability that the drink is more than 12 fluid ounces. Can this be considered...
Create a dice simulator in C#. The simulator must roll the dice 6 times and it...
Create a dice simulator in C#. The simulator must roll the dice 6 times and it must add the 3 highest rolls. So say it simulates: 4 4 5 2 1 3 the total would be 13 (4+4+5=13). Repeat this 6 different times. So if the random dice simulator simulates the outcomes below it should output: 13, 13, 14, 12, 16, 12 1) 4 4 5 2 1 3 2) 2 3 6 1 3 4 3) 5 3 1...
Prepare an object-oriented model representing A soft-drink machine
Prepare an object-oriented model representing A soft-drink machine
I need this code in C++ form using visual studios please: Create a class that simulates...
I need this code in C++ form using visual studios please: Create a class that simulates an alarm clock. In this class you should: •       Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have separate private members for the alarm and the clock. Do not forget to have a character variable representing AM or PM.) •       Initialize the clock to a specified time. •       Allow the clock to increment to the...
(Using C++ create a Car Instrument Simulator) Car Instrument Simulator Create the following classes: Car, Odometer,...
(Using C++ create a Car Instrument Simulator) Car Instrument Simulator Create the following classes: Car, Odometer, FuelGauge. The Car class allows for the instantiation of a Car object. The Car object will contain (via composition) an Odometer object, and a FuelGauge object. The Car object will also include "make", "model", and "color" as string properties (instance variables). In the Car constructor method, you will create a FuelGauge object, passing 15 as an argument into the FuelGauge constructor indicating the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT