Guidelines for HW 5: You are encouraged to work teams of two or three (not more than three) for this assignment, although individual submissions will be accepted. When you program as a team, you should work together, using the style of pair programming (http://www.pairprogramming.com), as demonstrated in lecture. If you work in teams then the names of all the students must appear in the program.
Your name must appear at the top of the program in comments, such as:
/************************************************************/
/* Authors: Amy Apon and Brent Hollosi */
/* Filename: Vending.c */
/* */
/* This program implements the functions of a vending */
/* machine. The machine sells three items: A sweet roll */
/* for $.85, a candy bar for $.75, and gum for $.45. */
/* */
/* A customer is asked to select an item. After selecting */
/* an item, the program receives coins ($.05, $.10 or $.25) */
/* from the customer until either the amount of money is */
/* greater than the cost of the item, or the customer */
/* selects quit. */
/* */
/* At the end, the program prints the item purchased, if */
/* any, and the amount of change, if any. */
/************************************************************/
Assignment Part One: [60 points] This assignment is to implement the functionality of a vending machine. The vending machine sells three items: A sweet roll for $.85, a candy bar for $.75, and gum for $.45. A customer is asked to select an item. After selecting an item, the program receives coins ($.05, $.10 or $.25) from the customer until either the amount of money is greater than the cost of the item, or the customer selects cancel. At the end, the program prints the item purchased, if any, and the amount of change, if any.
You will use a switch statement and functions to implement this program.
For interactive execution the screen dialogue should appear similar to this:
aapon@turing:~ $ ./VendingYour program must have at least a main function and a function to get coins from the customer. You may also use other functions if you like. You may wish to use a function to calculate the value of the coins entered, for example.
Welcome to the Vending Machine! What would you like to buy?
Items:
Sweet Roll: $0.85
Candy Bar: $0.75
Gum: $0.45
Enter an item, or q to quit (s=sweet roll, c=candy bar, g=gum, q=quit): s
You have selected Sweet Roll.
Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit): 3
You have entered $0.25.
Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit): 3
You have entered $0.50
Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit): 3
You have entered $0.75
Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit): 3
You have entered $1.00
You have purchased a Sweet Roll.
Your change is $0.15.
Have a Nice Day!
You may use the following pseudocode for the main program:
int main() {
define variables: char itemtype, ...
print welcome message and prompt
itemtype = getchar();
switch (itemtype) {
case 's': print message
retval = getcoins(85);
break;
case 'c': print message
retval = getcoins(75);
break;
case 'g': print message
retval = getcoins(45);
break;
default: print message
}
if (retval < 0) printf("No product purchased!\n");
else print amount of change
print Have a Nice Day!
}
Use a function prototype similar to the following for getcoins:
int getcoins(int cost);The getcoins function accepts a single parameter, which is the cost of the item. It runs in a loop, accepting coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit), until either q is selected or the value of the coins is greater than or equal to the cost of the item. You may use a float type for the parameter and return value if you prefer.
The getcoins function returns a -1 if the quit is selected. Otherwise it returns the amount of change that is to be returned to the customer. The amount of change is the value of the coins minus the cost of the purchase. You do not need to specify the exact coins that are returned, but you may get extra credit for this and other extra functionality.
You may use the following pseudocode to help you get started on the getcoins function:
define variablesIn addition to the program, I would like you to develop at least five test cases for the program. The test cases should test a variety of execution paths for the program, including the selection of a sweet roll, candy bar, and gum, and also the quit option. You should have at least one more test case to test the amounts of money that can be input to make sure that change is calculated correctly. The execution trace above can be used as one test case, and you should develop four more yourself.
print prompt, Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit):
coin = getchar();
calculate value
while coin is not quit and value is less than amount {
print prompt, Enter coins or q to quit (1=nickle, 2=dime, 3=quarter, q=quit):
coin = getchar();
calculate value
} /* end while */
if (coin == 'q') return(-1);
else return the amount of the change
In your HW5 directory, place the five example execution traces, as plain text files, similar to that shown above.
For fun: As described the vending machine dispenses a sweet roll, a candy bar, and gun, and accepts U.S. money. If you like you may change the items to something else (e.g., an epic sword, bronze armour, a fish), and you may use another type of money (e.g., gold coins) as long as the monetary units (5, 10 25) and the items costs (85, 75, 45) stay the same.
Grading: Grades on this program will be assigned using these guidelines:
Grading: Grades on this program will be assigned using these guidelines:
What you turn in: Each student must submit a separate program using the submit utility on turing. Do these steps:
Enjoy!