State Diagrams

The following is an implementation of a portion of the Vending machine state diagram that we discussed in class.

Code for Vending.java

Click here for the code --> Vending code

This code is the main applet. Notice how the interface with the user is kept to a minimum and is still conceptually readable.

Code for States.java

Click here for the code --> States code

This is the state diagram. It is run with its own thread and has methods for the applet to interface with. Notice how the state diagram continues to run once it is started. It is only stopped when the State is set to zero or when the applet calls the pleaseStop() method.

Notice how the states are divided into cases and the methods called by the buttons are only effective if the state is correct. This provides the programmer with the ability to control when user actions can be heeded and disregarded.

Code for Selection.java

Click here for the code --> Selection code

This code models the items available in the vending machine. Note that there is no limitation of the number of these items in the vending machine.

Code for Money.java

Click here for the code --> Money code

This code models the amount contained in the vending machine. It could also simply have a collection of Coin objects as well, if that was deemed appropriate.

 

Java
Sample Code