CMPS 1053 – Program 2 – Spring 2012

advertisement
CMPS 1053 – Program 2 – Spring 2012
Drink Machine Simulator using struct and arrays
Due: Monday, February 13
(I will not respond to program questions over the weekend before the due date & do not expect an extension.)
You will develop a program to simulate a drink machine. It will manage the sale of bottled drinks and
keep track of the inventory and cash-flow. The program will begin by initializing the machine. Then
filling the machine with drinks from data provided. It will then loop, repeatedly printing the menu and
processing purchases as specified below. When the machine is “turned off” a final report will be printed,
also as specified below. Use your own data for testing. You will be given data to use for handing in.
The specifications below are the MINIMUM acceptable. You may, of course, use addition arrays,
parameters, or functions that help to solve the problem in a reasonable fashion. You are welcome to
discuss your additions with me if you have doubts. I recommend you plan your solution “by hand” before
beginning to code.
Declare a constant MAX as the maximum number of bottles of each drink the machine can hold. Set the
value to 5. Use the constant MAX wherever appropriate.
DATA STRUCTURE: Declare a struct named Drink with 3 components; string DrinkName, float Cost, int
NumInMachine. Declare an array Machine[10] of structs (at most 10 different drinks). Initialize the
array with the following data, a full machine: (You choose the method of initialization.)
Cola
.75
5
Root-beer
.75
5
Orange-soda .75
5
Dr-Pepper
.75
5
Sprite
.75
5
Dasani
1.0
5
Orange-juice 1.0
5
Cranberry
1.0
5
Perrier
2.0
5
Fruit-Punch 1.0
5
Note: There can be NO spaces in the drink Name!!!
MAIN overview:
Initialize Machine with maximum number of drinks
Print Menu
Ask user to enter selection (1 through 11) and validate input. Keep asking until correct.
Loop until machine is “turned off”
Call ProcessPurchase function
If any two drink types are empty call the FillMachine function
Print menu
Ask user to enter selection (1 through 11) and validate input. Keep asking until correct.
Call FillMachine
Print Final Report
PrintMenu: Parameter, the drink machine array. Print as shown below: Print menu only to screen – NOT
to the final file for turning in.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Cola
0.75
Root-beer
0.75
Orange-soda 0.75
Dr-Pepper
0.75
Sprite
0.75
Dasani
1.00
Orange-juice 1.00
Cranberry
1.00
Perier
2.00
Fruit-Punch
1.00
Turn off machine
FillMachine function:
2 parameters: Drink machine array, maximum number of bottles of each drink
Fill each drink type to the maximum. Keep track of the total number sold of each type of drink.
Print message “Refilled Machine”
ProcessPurchase function:
Parameter: number of drink selection (1-10)
Ask user for amount of money inserted.
If item is sold out, print appropriate message, return money, exit function
Check money amount:
If too small, print message, return money, exit function
If too much, print amount of change
Print “Thank You for purchasing a” {Print drink name here}
Update drink total
Update money total
FinalReport function:
At least one parameter, the machine array; others as necessary.
Print a summary report to a file as follows:
<Programmer Name: your name here>
Drink Name
Cola
Number Sold
Total
4
$3.00
……ETC. for all 10 drinks……
____________________________________
Totals
XXX
$XX.XX
NOTE: The drink name column must be left justified and the numeric columns must be right justified.
Number sold is an integer with no decimal places printed. Total is currency with a $ and 2 decimal
places. Confirm your results. Echo print all activity to the file except the Print Menu. Do not print the
menu in the file!
Download