Lab 6. Chapter 4



Program Description

Exercise 19 (pg. 158 of the textbook)

Write an application to simulate a vending machine. The vending machine should be preloaded with different types of candy and a quantity for each. Use a variable for each type of candy that stores the quantity of that candy remaining in the vending machine. You can initialize these variables to any amount you wish. Each candy needs a price and a code (these values should be stored as constants). The code would be a combination of a letter and number. For simplicity, just allow the letters to be an A, B, C, D, or E.

The vending machine must have a mechanism to accept money. You can use a text box and a button to accept it, but you then would need to validate that each value is a valid denomination of currency. A better way is to create a button each for a nickel, dime, quarter, and dollar. As each button is clicked, add the appropriate amount to the total. Also, create a Change Return button to return the money entered to the user.

Create a series of buttons to allow the entry of a digit from 1 to 3 and a letter from A to E so that the user can select a candy to purchase. Since each purchase is signified by a combination of a letter and a number, as the buttons are clicked, display the choice being made.

Finally, add a button to make the purchase. It should validate that you have entered enough money to make the purchase, that you have selected a valid choice for a candy, and that candy still has a quantity greater than 0. The last step is to indicate how much money is returned as change.


Your first step should be to write out the event-driven algorithm.


If your TA does not provide the algorithm, feel free to work with other students in the lab to come up with the algorithm.