Question

In: Computer Science

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   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

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

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...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Your shopping cart should support...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Using the CLASSES BELOW Your...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). **PLEASE USE THE CLASSES BELOW***...
Java - Create a program that simulates a slot machine. When the program runs, it should...
Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
Write a program that simulates a vending machine. The machine holds six snack items labeled them...
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices: Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 The program then should ask the user to enter the item to purchase along with a sum of money....
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT