In: Computer Science
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 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.
For exact input
output messages and menu choices, please refer to the test cases
file below.
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 TEST CASE 2: 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: $1 Do you still want to make a purchase? (Y / N) : y Here is your Cola, and your change of $0.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: $1 Do you still want to make a purchase? (Y / N) : n 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: $1 Not enough money, please enter $1.50 or more: $1 Not enough money, please enter $1.50 or more: $2 Do you still want to make a purchase? (Y / N) : t Please enter Y or N: a Please enter Y or N: b Please enter Y or N: c Please enter Y or N: d Please enter Y or N: y Here is your Grape Soda, and your change of $0.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 : 9 Please enter a valid choice (1 - 6): 1 How much money has been inserted: $6 Do you still want to make a purchase? (Y / N) : y Here is your Cola, 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 : 6 Thank you for shopping! Drink Machine Daily Report Cola : 18 Root Beer : 20 Orange Soda : 19 Grape Soda : 19 Bottled Water : 20 Total amount collected : $4.50
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