These are my requirements.
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
Coke .75 20
Root Beer .75 20
Orange Soda .75 20
Grape Soda .75 20
Bottled Water 1.00 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.
This is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <string>
#include "Drink.h"
using namespace std;
int main()
{
Drink vending;
int choice;
choice = vending.displayChoices();
vending.buyDrink(choice);
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// Class Drink implmentation code "Drink.h"
#include <iostream>
using namespace std;
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;
}
const int DRINKS = 5; // Max number of drinks
// New array using structure constructor to fill in data for all the drinks, costs, and inventory
int data[DRINKS] = {Drink("Coke", .75, 20),
Drink("Root Beet", .75, 20),
Drink("Orange Soda", .75, 20),
Drink("Grape Soda", .75, 20),
Drink("Bottled Water", 1.00, 20)};
// Local variables
string name; // Name of the drink ie: Coke
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(int c); // Choice as argument so it knows what drink to process
};
// Code implmentation for the functions
int Drink::displayChoices()
{
int choice;
do
{
cout << "This is a vending machine program.\n"
<< "1. Coke\n"
<< "2. Root Beer\n"
<< "3. Orange Soda\n"
<< "4. Grape Soda\n"
<< "5. Bottled Water\n"
<< "6. Quit the Program\n"
<< "Enter choice: ";
cin >> choice;
// Validate the choice
while (choice < 1 || choice > 6)
{
cout << "Please select items 1-6"
<< "Re-enter: ";
cin >> choice;
return choice;
}
} while (choice != 6);
}
double buyDrink(int choice)
{
cout << "You selected #" << choice << "."
}
double inputMoney(int choice)
{
int money,
total;
cout << "You selected #" << choice << "."
<< "Enter 1 to insert a quarter or 0 to quit.\n";
cin >> money;
while (money != 0);
{
total += .25;
cout << "Total change entered: " << total << "."
<< "Enter 1 to insert a quarter or 0 to quit.\n";
cin >> money;
}
return money;
}
|
My question is, does my array of structures need to be implemented inside the class Drink or my main program?
Also, how do I access the array of initialized information once I initialize it.
And, other thoughts and opinions on this process would be most appreciated.
Thank you.