$30
CS1337.005 Assignment 1 1
Assignment 1
Drink Machine Simulator
Write a program that simulates a soft drink machine. The program must use a number of parallel arrays
that store the following data:
Drink name (type of drink)
Drink cost
Number of drinks (of this type) in the machine
Number of drinks (of this type) sold – This is a calculated value
There will be up to 20 drinks in the machine. Your program will read the information from a file. It must
be able to handle any number of drinks, up to 20. If there are more than 20 types of drinks in the file or
if there are less than 1 drink in the file your program should issue an error message and quit.
The following file contains the data needed for 6 drinks:
Cola 1.75 25
Root-beer 2.00 20
Lemon-lime 1.50 25
Water 1.00 40
Orange 1.80 5
Iced-tea 1.65 35
The input file is included on eLearning and has a name of "drink_information.txt".
The first value is the drink name (you can make this a string), the second value is the price and the third
value is the quantity (number of drinks in the machine).You will need arrays for all of these items.
You will be using parallel arrays to implement this application. You will need an array for the drink
names, another one for the drink prices, a third one for the drink quantities, and a fourth array for the
number of drinks sold. Your program will keep track of the number of drinks sold for each drink type.
Each time the program runs it should read in the "drink_information.txt" and populate the
arrays used to simulate the drink machine.
Your program will then start a processing loop that performs the following steps:
Display a list of drinks. This list of drinks is built from the arrays you created using the
drink_information.txt file.
The user can then either pick a drink or quit the application.
If the user selects a drink that is available (is in the menu list and has a quantity of 1 or more):
o The user will enter in the amount of money that is being inserted into the machine to
purchase the drink.
CS1337.005 Assignment 1 2
o You program should make sure that the amount of money entered is sufficient for the
purchase. The amount needs to be greater than 0 (0), less than or equal to $5 (<=5)
and greater than or equal to the amount of the purchase. If the input amount isn’t
sufficient then your program should display a message and ask for a valid amount. The
program should keep prompting the user until they enter in a valid amount.
o If the amount of money is sufficient for the purchase your program will display the
amount of change that would be returned to the user and will subtract one from the
drink quantity for that drink. The amount of change is just the amount of change, you do
not have to figure out what coins to return back as change. There may not be any
change and your program needs to handle that case.
o If the purchase was a success you need to count this purchase for the specified drink
and you need to decrement the quantity for the drink sold.
If the user selects a drink that is sold out, a message should be displayed and the loop should be
repeated.
If the user enters an invalid drink value, an error message should be displayed and the loop
should be repeated.
If the user has asked to quit the program, the program should display the total amount of
money the machine has earned. For each drink you need to display the drink name, cost,
quantity available, and quantity sold.
When the user enters an amount of money, do not accept negative values or values greater than $5.00.
If the value is not greater than 0 and less than or equal to $5 then your program should prompt for the
value until the user enters in a valid amount.
When the program ends you need to sort the various arrays based on the number of drinks sold. You
will order the arrays in descending order (the drink that sold the most should be displayed first). You
will need to create a sort function that sorts multiple arrays based on the contents of one of the arrays.
Your program should not use any global variables except for const global variables. The max number
of drinks (the size of the arrays) should NOT be a global variable. It should be a const int in your
main function. It should be passed to the read function. The read function should return the actual
number of drinks being used. The actual number used should be passed to any functions that need it.
The main should only be the driver and should contain the main processing loop. You must delegate
work to other functions. You need functions to read in the file, display the menu, and so on.
The above description is for the entire program. It is recommended that build the program in parts. See
the details below in Parts 1 through 6.
Here is sample output several runs of the final application. Your program should create the same output
for the same input values. Your interface should look just like the samples runs shown here. User input is
in bold.
CS1337.005 Assignment 1 3
For the first run the user enters quit right away and no processing is performed.
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
0
The drink machine sales were $0.00
Drink Cost Quantity Sold
Cola 1.75 25 0
Root-beer 2.00 20 0
Lemon-lime 1.50 25 0
Water 1.00 40 0
Orange 1.80 5 0
Iced-tea 1.65 35 0
Another run showing invalid amounts entered by the program’s user. We also see change from one
purchase and no change from another purchase:
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
1
The Cola drink you are requesting costs $1.75
Enter the payment amount for the drink: 1
The payment amount is less than the cost of the drink
The Cola drink you are requesting costs $1.75
Enter the payment amount for the drink: -1
The payment amount must be between $0.00 and $5.00
The Cola drink you are requesting costs $1.75
Enter the payment amount for the drink: 5.01
The payment amount must be between $0.00 and $5.00
The Cola drink you are requesting costs $1.75
Enter the payment amount for the drink: 1.80
Your change is $0.05
CS1337.005 Assignment 1 4
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 1.80
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
0
The drink machine sales were $3.55
Drink Cost Quantity Sold
Cola 1.75 24 1
Orange 1.80 4 1
Lemon-lime 1.50 25 0
Water 1.00 40 0
Root-beer 2.00 20 0
Iced-tea 1.65 35 0
And still another run. Here we show invalid values entered for the menu:
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
7
The drink you requested could not be found
Please enter the number of the drink being purchased
CS1337.005 Assignment 1 5
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
-1
The drink you requested could not be found
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
1
The Cola drink you are requesting costs $1.75
Enter the payment amount for the drink: 1.90
Your change is $0.15
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
0
The drink machine sales were $1.75
Drink Cost Quantity Sold
Cola 1.75 24 1
Root-beer 2.00 20 0
Lemon-lime 1.50 25 0
Water 1.00 40 0
Orange 1.80 5 0
Iced-tea 1.65 35 0
CS1337.005 Assignment 1 6
One final (long) run. In this one we run out of Orange. The quantity for the Orange drink is 5. We
successfully purchase 5 drinks, but we cannot purchase a sixth one:
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 2
Your change is $0.20
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 2
Your change is $0.20
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 2
Your change is $0.20
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
CS1337.005 Assignment 1 7
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 2
Your change is $0.20
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
The Orange drink you are requesting costs $1.80
Enter the payment amount for the drink: 2
Your change is $0.20
Thank you for your purchase
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
5
Sorry! We are out of Orange
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
4
The Water drink you are requesting costs $1.00
Enter the payment amount for the drink: 1
Thank you for your purchase
CS1337.005 Assignment 1 8
Please enter the number of the drink being purchased
0 Stop processing drink orders
1 Cola $1.75
2 Root-beer $2.00
3 Lemon-lime $1.50
4 Water $1.00
5 Orange $1.80
6 Iced-tea $1.65
0
The drink machine sales were $10.00
Drink Cost Quantity Sold
Orange 1.80 0 5
Water 1.00 39 1
Lemon-lime 1.50 25 0
Root-beer 2.00 20 0
Cola 1.75 25 0
Iced-tea 1.65 35 0
Part 1: Reading in the file
Make sure your variable names have meaningful names and that you have included appropriate
comments in your application.
You must include the following as your first two comments:
// Assignment 1 for CS 1337.013
// Programmer: <Your name goes here
Make sure you replace <Your name goes here with your name.
You need to ensure you have comments throughout your program and not just the comments at the top
of the program.
For this part you need to read in the input file to fill in the drink machine arrays.
Recall that the input file is "drink_information.txt".
Make sure you handle the cases where the input file has 0 drinks or more than 20 drinks. You will have
to do end of file checking. Make sure you close your file when you are done with it. If the input file
contains more than 20 drinks you should only read in the first 20. Actually the size of the array (20 for
our case) will be passed to the read function as an argument. You must not hard code 20 in your read
function.
To make it easier to build your application you must put all of the logic for reading in the file and filling
in the arrays into a function. You will need to pass the arrays and the array size from the main to the
CS1337.005 Assignment 1 9
function you create. The function should then read in the file and fill in the arrays. Your program should
check to make sure the open of the file works properly and display a message if it does not. The
application should display an error message and quit if it cannot read in the
drink_information.txt file.
If must not try and read in more values than will fit into the arrays. The max size of the arrays needs to
be passed to the read function as a parameter.
You can use the following function to display the contents of the arrays once you have created them.
This will allow you to verify that the arrays were built properly. Your program will not need to call this
function once you have part 1 working. The output from the displayDrinkMachine function can be used
to display the total sold for each drink at the end of the program. It is also useful for debugging.
// Display the contents of the drink machine arrays
void displayDrinkMachine(const string drinkName[],
const double drinkCost[],
const int drinkQty[],
const int drinksSold[], int numberDrinks)
{
// Output the headings for the table
cout << fixed << setprecision(2);
cout << setw(15) << "Drink";
cout << setw(10) << "Cost";
cout << setw(10) << "Quantity";
cout << setw(10) << "Sold" << endl;
// for each drink output the details of that drink
for (int index = 0; index < numberDrinks; index++)
{
cout << setw(15) << drinkName[index];
cout << setw(10) << drinkCost[index];
cout << setw(10) << drinkQty[index];
cout << setw(10) << drinksSold[index] << endl;
}
}
Part 2: Processing the orders
Next you should get the primary programming loop built and implement the logic to display a menu,
read in the input value from the menu, and process the order.
You should put the menu display and input logic into a function (you may want to use more than one
function for this, it is up to you). The function will need some of the drink machine arrays so it can build
the menu and check to see if the input is valid or not. The function should return -1 to indicate that you
CS1337.005 Assignment 1 10
are going to quit the application. If the user selects a drink, your function should return back the index of
the chosen drink. You should create a global const int value for the -1 value.
If the value indicates that you need to quit the application, your program loop should stop. The value -1
is returned by your input / menu function to indicate that you should quit the program. Your program
needs to check the global constant you created in the previous paragraph and not use the literal -1.
If the value returned back from your input / menu function is an index, your program should ask for the
amount from the user and return back any change. Once again you should put this drink processing
(asking for the amount and calculating the change) into a function. The function should return back a
bool value to indicate that the drink was processed (at this point the drink order is always processed).
For part 2 you can assume that all input values are valid – you do not have to check the input values
from the menu processing or the input amount. You will be adding code to do those in parts 3 and 4.
Part 3: Checking the validity of the menu input
Update your input / menu function (or functions) to validate the input for the menu. The menu input
value should either be the quit value, or for one of the up to the number of drinks read in from the input
file.
If the value is invalid the, input / menu function should display an error message and prompt for a new
input value. The input validation will need to be in a loop. The function should not exit out of the menu
until the input value is valid.
Part 4: Checking the validity of the amount entered by the user
Next, your program needs to be updated to make sure the amount entered by the user is greater than
0.0 (dollars) and less than or equal to 5.0 (dollars). If the amount is not valid, the application should
output an error message and ask the user to enter the amount again. The program also needs to make
sure the amount entered is enough to pay for the drink. It if is not, then the program should display a
message and ask the user to enter a valid amount. Again it is helpful if you do this validity checking in a
loop in a function.
If the amount entered by the user is greater than the cost of the drink, your program must output the
change due to the customer. This will be in the format $x.xx where x.xx is the amount. Note we need
exactly two digits to the right of the decimal point.
Part 5: Keeping track of inventory
In your drink processing function you need to check to make sure the quantity of drinks is 1 or more. If it
is, you should process the drink order and decrement the quantity by 1.
If the drink quantity is < 1 your function should display a message and should return back a false value to
indicate to the main function that the drink order could not be fulfilled.
CS1337.005 Assignment 1 11
Part 6: Keeping track of the amount earned
Your main function should check the return value from the drink processing function. If the drink was
processed, the drink cost should be added to an accumulator that keeps track of the total amount
earned. You should also update the quantity of the drinks sold.
After the user has asked you to quit the execution of your program, your program should exit out of the
processing loop, it should display the amount of money earned. You also need to sort your arrays based
on the (descending) quantity of drinks sold. You then should output all of the drinks, and the number of
each drink sold.
Grading:
20% of the grade will for comments you have in your program, for having meaningful variable names,
and for formatting your code as is shown in the C++ text book.
80% will be for the program itself. Make sure you do all of the parts and that your output contains all of
the information in the above sample runs. Make sure your menus alight properly and make sure your
currency output has a leading $ and two digits to the right of the decimal point. Note that the
displayDrinkMachine function I provided does not have a leading $ for the cost. You do not have to
change that. The call to displayDrinkMachine is only there to ensure you are sorting the arrays
properly.
If you have any questions at all about this assignment let me know.