Topic is: Abstract Data Structures language is C++ should not include .txt file
ID: 665148 • Letter: T
Question
Topic is: Abstract Data Structures
language is C++
should not include .txt file
Please explain the steps with comment
13. Drink Machine Simulator Write a program that simulates a soft drink machine. The program should use a structure that stores the following data: Drink Name Drink Cost Number of Drinks in Machine Programming Challenges 655 The program should create an array of five structures. The elements should be initialized with the following data: Drink Name Cost Cola .75 Root Beer .75 Lemon-Lime .75 Grape Soda.80 Cream Soda.80 Number in Machine 20 20 20 20 20 Each time the program runs, it should enter a loop that performs the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed. The loop then repeats. When the user chooses to quit the program it should display the total amount of money the machine earned Input Validation: When the user enters an amount of money, do not accept negative values or values greater than S1.00Explanation / Answer
Ans:
C++ coding for Dink Machine Simulator :
#include <iostream.h>
#include <iomanip.h>
#include<String.h>
struct Drink
{
// Public constructor / with default constructor
Drink(string n, double c, int co)
{
name = n;
cost = c;
count = co;
}
Drink()
{
name = " ";
cost = 0;
count = 0;
}
// Local variables
string name; // Name of the drink ie: Cola
double cost; // Cost ie: $.75
int count; // Number of items in vending machine
// Function prototypes
int displayChoices();
double buyDrink(int c); // Choice as argument so it knows what drink to process
double inputMoney(double c); // Cost as argument so it knows how much to collect
int dailyReport(double m, int i); // MoneyBack as arugment so it knows how to process quantity
};
// Code implmentation for the functions
int Drink::displayChoices()
{
int choice;
cout << "This is a vending machine program. "
<< setw(7) << "Item" << setw(22) << "Cost "
<< "1. Cola .75 "
<< "2. Root Beer .75 "
<< "3. Lemon-Lime .75 "
<< "4. Grape Soda .75 "
<< "5. Cream Soda 1.00 "
<< "6. Quit the Program "
<< "Enter choice: ";
cin >> choice;
// Validate the choice
while (choice < 1 || choice > 6)
{
cout << "Please select items 1-6"
<< "Re-enter: ";
cin >> choice;
}
return choice;
}
double Drink::buyDrink(int choice)
{
string tempName;
switch (choice)
{
case 1:
tempName = "Cola";
break;
case 2:
tempName = "Root Beer";
break;
case 3:
tempName = "Lemon-Lime";
break;
case 4:
tempName = "Grape Soda";
break;
case 5:
tempName = "Cream Soda";
break;
default:
tempName = " ";
break;
}
cout << "You selected " << tempName << ". ";
double cost,
moneyBack; // the cost argument returned back from inputMoney
if (choice >= 1 && choice <= 4)
{
cost = .75;
moneyBack = inputMoney(cost);
return moneyBack;
}
else
{
cost = 1.00;
moneyBack = inputMoney(cost);
return moneyBack;
}
}
double Drink::inputMoney(double cost)
{
double money = 0;
if (cost == .75)
{
cout << "Your item costs $" << cost << ". "
<< "Enter " << cost << " to insert 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 Drink::dailyReport(double moneyBack, int count)
{
if (moneyBack > 0)
{
cout << " Thank you for your purchase. ";
count--;
return count--;
}
else
{
cout << "Come back next time! ";
return count;
}
}
#include <iostream.h>
#include <iomanip.h>
#include<String.h>
struct Drink
{
// Public constructor / with default constructor
Drink(string n, double c, int co)
{
name = n;
cost = c;
count = co;
}
Drink()
{
name = " ";
cost = 0;
count = 0;
}
// Local variables
string name; // Name of the drink ie: Cola
double cost; // Cost ie: $.75
int count; // Number of items in vending machine
// Function prototypes
int displayChoices();
double buyDrink(int c); // Choice as argument so it knows what drink to process
double inputMoney(double c); // Cost as argument so it knows how much to collect
int dailyReport(double m, int i); // MoneyBack as arugment so it knows how to process quantity
};
// Code implmentation for the functions
int Drink::displayChoices()
{
int choice;
cout << "This is a vending machine program. "
<< setw(7) << "Item" << setw(22) << "Cost "
<< "1. Cola .75 "
<< "2. Root Beer .75 "
<< "3. Lemon-Lime .75 "
<< "4. Grape Soda .75 "
<< "5. Cream Soda 1.00 "
<< "6. Quit the Program "
<< "Enter choice: ";
cin >> choice;
// Validate the choice
while (choice < 1 || choice > 6)
{
cout << "Please select items 1-6"
<< "Re-enter: ";
cin >> choice;
}
return choice;
}
double Drink::buyDrink(int choice)
{
string tempName;
switch (choice)
{
case 1:
tempName = "Cola";
break;
case 2:
tempName = "Root Beer";
break;
case 3:
tempName = "Lemon-Lime";
break;
case 4:
tempName = "Grape Soda";
break;
case 5:
tempName = "Cream Soda";
break;
default:
tempName = " ";
break;
}
cout << "You selected " << tempName << ". ";
double cost,
moneyBack; // the cost argument returned back from inputMoney
if (choice >= 1 && choice <= 4)
{
cost = .75;
moneyBack = inputMoney(cost);
return moneyBack;
}
else
{
cost = 1.00;
moneyBack = inputMoney(cost);
return moneyBack;
}
}
double Drink::inputMoney(double cost)
{
double money = 0;
if (cost == .75)
{
cout << "Your item costs $" << cost << ". "
<< "Enter " << cost << " to insert 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 Drink::dailyReport(double moneyBack, int count)
{
if (moneyBack > 0)
{
cout << " Thank you for your purchase. ";
count--;
return count--;
}
else
{
cout << "Come back next time! ";
return count;
}
}