package org.xander.behavioral.state;

public class GumballMachine {
    State soldOutState;
    State noQarterState;
    State hasQuarterState;
    State soldState;
    State winnerState;

    State state = soldOutState;
    int count = 0;

    public GumballMachine(int numberOfBalls) {
        soldOutState = new SoldOutState(this);
        noQarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        this.count = numberOfBalls;

        if (numberOfBalls > 0) {
            state = noQarterState;
        }
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejectQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    void setState(State state) {
        this.state = state;
    }

    void releaseBall() {
        System.out.println("A gumball comes rolling out the slot...");
        if (count != 0) {
            count = count - 1;
        }
    }

    void refill(int numberOfBalls) {
        System.out.println("\n-----Refilling the machine with " + numberOfBalls + " balls");
        this.count = numberOfBalls;
        state = noQarterState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public int getCount() {
        return count;
    }

    public State getWinnerState() {
        return winnerState;
    }

    @Override
    public String toString() {
        return "\nGumBall Machine Model #T1000"
                + "\nInventory: "
                + getCount()
                + " gumballs"
                + "\nMachine is waiting for quarter...";
    }
}

@@ -94,24 +94,25 @@
    State noQ
+u
 arterState;%0A
@@ -336,24 +336,25 @@
 %0A        noQ
+u
 arterState =
@@ -604,32 +604,33 @@
      state = noQ
+u
 arterState;%0A    
@@ -1293,24 +1293,25 @@
  state = noQ
+u
 arterState;%0A
@@ -1447,16 +1447,17 @@
 turn noQ
+u
 arterSta

Update GumballMachine.java