< Previous | Next >

Creating the Idle state

When it is in the Idle state, the state machine is quite simply waiting for an event, in the form of a coin, to arrive.

In this step, you will do the following:
  1. Click the InitialState1, and rename it to Ready.
  2. Similarly, rename FinalState1 to Off, and State1 to Idle.
  3. Add and configure an entry for this state This entry action will execute the moment this state is entered, and will return any change that is currently in the state machine, and reset the total to nill.
    1. Click Idle, and select the Add an Entry icon from the action menu that pops up.
    2. Click this new entry, and rename it to resetTotal.
    3. In the Properties view, click the Details tab, and click the Java radio button.
    4. Paste the following code into the Java editor:
      System.out.println("Entering the Idle State");
      if (total.doubleValue() > 0) { 
      	System.out.println("VendingMachine returning change...$" + total.toString() + " has been returned.");
      	total = new Double(0.0d);
      }
  4. Similarly, create an exit state on the Idle state named exitIdle with the following code:
    System.out.println("Exiting the Idle state");
  5. Create an action on the first transition. This action will display a welcome message to the user that lists the items available, and their cost.
    1. Click the transition between the Ready and Idle states.
    2. From the action menu, click the Add an Action icon, and name it Welcome.
    3. Add the following code to the Java editor:
      System.out.println("VendingMachine is turned on...Prices:");
      System.out.println("VENDINGMACHINE: pop: $0.5");
      System.out.println("VENDINGMACHINE: chips: $0.75");
      System.out.println("VENDINGMACHINE: candy: $1.0");
      total = new Double(0.0d);
  6. Expand the interface VendingMachienInterface in the tray on the right, drag and drop the off operation to the transition between the Idle and the Off states.
Your states and transitions should look like the ones in this image:
The Ready, Idle and Off states as they should look as you begin to create this state machine.

Feedback
(C) Copyright IBM Corporation 2005, 2006. All Rights Reserved.
< Previous | Next >