< Previous | Next >

Creating the Depositing state

The Depositing state is the state where a user has started depositing money and has not yet made a selection or canceled. This 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 Simple icon, drop it onto the canvas well to the right of the Idle state, and rename it Depositing.
  2. Create an entry on the Depositing 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 Depositing 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. You should grab the black boxes on the corners of the blue line and drag them out to visually enlarge the transition. We are about to add a lot of information to it.
    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;
      A condition guards the transition and only allows processing when and if it evaluates to 'True'. Otherwise the current state is maintained.
    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. Save your work. We will resolve the errors in the next step.
< Previous | Next >





Feedback



This information center is powered by Eclipse technology. (http://www.eclipse.org)