< Previous | Next >

Creating the Depositing state

The Depositing state keeps track of how much money has been deposited into the vending machine.

In this step, you will do the following:
  1. From the palette, click the state icon, drop it onto the canvas well to the right of the Idle state, and rename it Depositing.
  2. Create an entry on the Deposit state named enterDepositing with the following code:
    System.out.println("Entering the Depositing state");
  3. Similarly, create an exit on the Deposit state named leavingDepositing with the following code:
    System.out.println("Leaving the Depositing state");
  4. Create and configure a self transition on the Deposit state. A self transition is one in which the source state and the target state are the same. In this case, the transition going out of the Depositing state is executed with each subsequent coin that is deposited. It checks the validity of the coin, adds the value of the coin to the running total, and then returns to the Depositing state where the state machine will then wait for the next event to occur.
    1. Hover over the Deposit state until a yellow grabber appears as shown in this image. The Deposit state with the yellow grabber.
    2. Left-click your mouse to create the beginning of the transition, and then drag the cursor back over the Deposit state and click it. A self transition will appear as shown in this image: A self-transition on the Depositing state.
    3. Drag the deposit operation onto this transition.
    4. Add a condition named isCoinValid to this transition with the following code:
      System.out.println("Money is being deposited in VendingMachine. Checking to see if the coin is valid");
      double coin = deposit_Input_coin.getDouble("value");
      if (coin == 1.0d || coin == 2.0d || coin == 0.25d || coin == 0.1d || coin == 0.05d)
      	return true;
      return false;
    5. Add an action named updateTotal to this transition with the following code:
      double coin = deposit_Input_coin.getDouble("value");
      double newTotal = total.doubleValue() + coin;
      total = new Double(newTotal);
      System.out.println("Successfully deposited "+coin+" in the VendingMachine");
Your Depositing state and its self-transitions should look like this image: The Depositing state and its self-transition.

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