Reviewing a guarded withdrawal against its state machine

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:


Java
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.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Implementing Classes Based on Interaction and State Diagrams Questions: