Activity 10: State machine of the user interface
To specify the behavior of the interface, first define and initialize two variables:
-
a variable of type integer to represent the credit of the user, initialize it to 0;
-
a variable of type pricesMap (see VendingMachine.types) to represent the prices of the product, where cola costs 3, juice costs 2, and water costs 1.
-
The Tutorial Example contains an example of maps.
-
See also statements and expressions.
Next define the state machine of the IUser interface according to the following description:
-
In the initial state, when there is no credit, only command InsertCoin is allowed. This command has an output parameter representing the credit when the command has been completed. This is specified as the first parameter in the reply, where the second one is of type CoinResult.
See the notes and example about a reply in transitions.
-
InsertCoin may have three possible results:
-
The coin is accepted and the credit is increased by one.
-
The coin is not accepted and the credit is not changed.
-
The machine is not operational; in this case the value of the out parameter is not specified, that is, any value is allowed.
-
-
When the credit is positive, ReturnMoney is allowed, returning the total amount of credit. Finally, the command OrderProduct is allowed, which may have a number of possible responses:
-
NOT_OPERATIONAL to indicate the machine is not operational
-
NOT_ENOUGH_MONEY to indicate that the credit is not sufficient
-
NO_PRODUCTS_AVAILABLE to indicate that the credit is sufficient, but no products are available
-
DELIVERED to indicate that the credit is sufficient and the product is delivered
-
It is not possible to use local variables in a transition, but note that it is allowed to have statements after a reply.