< Previous | Next >

Linking the two states together

There are four transitions that link both of your states, and each one provides a different function. The first one registers a coin event, the second one manages dispensing, the third monitors a timeout, and the fourth provides a cancellation option.

In this step, you will create and configure these transitions.
Note: You will be creating four transitions in this task, and they do not all go in the same direction. Pay close attention to the instructions so as not to confuse your target and source states.
  1. Create a transition from the Idle to the Depositing state. This transition recognizes when a coin arrives, and then checks the validity of the coin, updates the total and moves the state machine to the Depositing state.
    1. Drag the deposit operation from the tray onto this transition.
    2. Add a condition named isCoinValid to this transition with the following code:
      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;
      System.out.println("The coin deposited is not a valid coin.");
      return false;
    3. 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");
  2. Create a transition from the Depositing to the Idle state. This transition is executed when the user makes a selection. If the total amount of money is sufficient to dispense the selected item, it will fire an action that delivers an appropriate dispensing message, and moves the state machine to the Idle state.
    1. Drag the select operation onto this transition.
    2. Add a condition named enoughCoin to this transition with the following code:
      String item = select_Input_selection.getString("item");
      double totalTemp = total.doubleValue();
      if (item.equals("pop")) return totalTemp >= 0.5d;
      else if (item.equals("chips")) return totalTemp >= 0.75d;
      else if (item.equals("candy")) return totalTemp >= 1.0d;
      System.out.println("Incorrect item selected.  Please try again.");
      return false;
    3. Add an action named dispenseItem to this transition with the following code:
      String item = select_Input_selection.getString("item");
      double totalTemp = total.doubleValue();
      if (item.equals("pop")) 
      {
      	totalTemp = totalTemp - 0.5d; 
      	System.out.println("A pop has been dispensed.");
      }
      else if (item.equals("chips")) 
      {
      	totalTemp = totalTemp - 0.75d; 
      	System.out.println("A bag of chips has been dispensed.");
      }
      else if (item.equals("candy")) 
      {
      	totalTemp = totalTemp - 1.0d; 
      	System.out.println("A candy bar has been dispensed.");
      }
      total = new Double(totalTemp);
  3. Create a second transition from the Depositing to the Idle state. This transition has a timeout that will move the state machine to the Idle state if and when too much time has elapsed. When the state machine enters the Idle state, any money in the machine is automatically returned, and the total is reset to nill.
    1. Add a timeout named tookTooLong to this transition. In the Details click the Duration and Literal radio buttons, and enter 30 into the Seconds field.
    2. Add an action named timeoutAction to this transition with the following code:
      System.out.println("Took too long: moving to Idle State");
  4. Create a transition from the Depositing to the Idle state. This transition is executed if the user cancels the transaction, and it returns the machine to the Idle state. As before, when the state machine enters the Idle state, any money in the machine is automatically returned, and the total is reset to nill.
    1. Drag the cancel operation onto this transition.
Your completed state machine should look something like the one shown in the screen capture below:
An image of the completed state machine.
Save your work. The problems view should be clean and free of errors.
< Previous | Next >





Feedback



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