001package headfirst.proxy.gumballmonitor;
002
003public class GumballMachine {
004        State soldOutState;
005        State noQuarterState;
006        State hasQuarterState;
007        State soldState;
008        State winnerState;
009
010        State state = soldOutState;
011        int count = 0;
012        String location;
013
014        public GumballMachine(String location, int count) {
015                soldOutState = new SoldOutState(this);
016                noQuarterState = new NoQuarterState(this);
017                hasQuarterState = new HasQuarterState(this);
018                soldState = new SoldState(this);
019                winnerState = new WinnerState(this);
020
021                this.count = count;
022                if (count > 0) {
023                        state = noQuarterState;
024                }
025                this.location = location;
026        }
027
028        public void insertQuarter() {
029                state.insertQuarter();
030        }
031
032        public void ejectQuarter() {
033                state.ejectQuarter();
034        }
035
036        public void turnCrank() {
037                state.turnCrank();
038                state.dispense();
039        }
040
041        void setState(State state) {
042                this.state = state;
043        }
044
045        void releaseBall() {
046                System.out.println("A gumball comes rolling out the slot...");
047                if (count != 0) {
048                        count = count - 1;
049                }
050        }
051
052        public int getCount() {
053                return count;
054        }
055
056        public void refill(int count) {
057                this.count = count;
058                state = noQuarterState;
059        }
060
061        public State getState() {
062                return state;
063        }
064
065        public String getLocation() {
066                return location;
067        }
068
069        public State getSoldOutState() {
070                return soldOutState;
071        }
072
073        public State getNoQuarterState() {
074                return noQuarterState;
075        }
076
077        public State getHasQuarterState() {
078                return hasQuarterState;
079        }
080
081        public State getSoldState() {
082                return soldState;
083        }
084
085        public State getWinnerState() {
086                return winnerState;
087        }
088
089        public String toString() {
090                StringBuilder result = new StringBuilder();
091                result.append("\nMighty Gumball, Inc.");
092                result.append("\nJava-enabled Standing Gumball Model #2004");
093                result.append("\nInventory: " + count + " gumball");
094                if (count != 1) {
095                        result.append("s");
096                }
097                result.append("\n");
098                result.append("Machine is " + state + "\n");
099                return result.toString();
100        }
101}