An Account state machine has states ACTIVE and FROZEN, and a new account starts in ACTIVE. The diagram draws a self-transition on ACTIVE for withdraw(amt), guarded by amt ≤ balance, with the action of subtracting amt from balance. It also draws ACTIVE to FROZEN on freeze and FROZEN to ACTIVE on unfreeze. Neither state has entry or exit actions. The team's policy is that an event with no transition from the current state throws IllegalStateException. When the guard of a drawn transition fails, the team throws IllegalArgumentException and leaves the balance and state unchanged. Review this implementation, in which the constructor sets the opening balance and freeze and unfreeze already follow the diagram and the policy:
public class Account { private enum State { ACTIVE, FROZEN } private State state = State.ACTIVE; private double balance; public Account(double openingBalance) { balance = openingBalance; } public void freeze() { if (state != State.ACTIVE) { throw new IllegalStateException("freeze in " + state); } state = State.FROZEN; } public void unfreeze() { if (state != State.FROZEN) { throw new IllegalStateException("unfreeze in " + state); } state = State.ACTIVE; } public void withdraw(double amt) { if (amt > balance) { throw new IllegalArgumentException("insufficient funds"); } balance -= amt; } }
Which statements about this withdraw method are true? Select all that apply.
A unit test that freezes the account and then withdraws less than the balance would expose the defect.
The guard check must come after balance -= amt, because guards are evaluated once the action has run.
When the account is FROZEN, a call to withdraw for an amount within the balance still reduces the balance, which contradicts the diagram.
The method is defective because it never assigns state = State.ACTIVE for the withdraw self-transition.
Unlock full course notes by signing up
Counting undrawn state-event combinations
A ParcelLocker class is designed with a state machine diagram that has 5 states and 6 events. The diagram draws 9 transition arrows. In exactly two places, a pair of arrows leaves the same state on the same event with complementary guards. Every other arrow is the only one for its state and event. The team's policy is that any state-event combination with no drawn transition throws IllegalStateException. How many state-event combinations fall under this policy? Give your answer as a whole number.