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