001package headfirst.state.gumball;
002
003public class GumballMachine {
004
005        final static int SOLD_OUT = 0;
006        final static int NO_QUARTER = 1;
007        final static int HAS_QUARTER = 2;
008        final static int SOLD = 3;
009
010        int state = SOLD_OUT;
011        int count = 0;
012
013        public GumballMachine(int count) {
014                this.count = count;
015                if (count > 0) {
016                        state = NO_QUARTER;
017                }
018        }
019
020        public void insertQuarter() {
021                if (state == HAS_QUARTER) {
022                        System.out.println("You can't insert another quarter");
023                } else if (state == NO_QUARTER) {
024                        state = HAS_QUARTER;
025                        System.out.println("You inserted a quarter");
026                } else if (state == SOLD_OUT) {
027                        System.out.println("You can't insert a quarter, the machine is sold out");
028                } else if (state == SOLD) {
029                        System.out.println("Please wait, we're already giving you a gumball");
030                }
031        }
032
033        public void ejectQuarter() {
034                if (state == HAS_QUARTER) {
035                        System.out.println("Quarter returned");
036                        state = NO_QUARTER;
037                } else if (state == NO_QUARTER) {
038                        System.out.println("You haven't inserted a quarter");
039                } else if (state == SOLD) {
040                        System.out.println("Sorry, you already turned the crank");
041                } else if (state == SOLD_OUT) {
042                        System.out.println("You can't eject, you haven't inserted a quarter yet");
043                }
044        }
045
046
047
048
049        public void turnCrank() {
050                if (state == SOLD) {
051                        System.out.println("Turning twice doesn't get you another gumball!");
052                } else if (state == NO_QUARTER) {
053                        System.out.println("You turned but there's no quarter");
054                } else if (state == SOLD_OUT) {
055                        System.out.println("You turned, but there are no gumballs");
056                } else if (state == HAS_QUARTER) {
057                        System.out.println("You turned...");
058                        state = SOLD;
059                        dispense();
060                }
061        }
062
063        public void dispense() {
064                if (state == SOLD) {
065                        System.out.println("A gumball comes rolling out the slot");
066                        count = count - 1;
067                        if (count == 0) {
068                                System.out.println("Oops, out of gumballs!");
069                                state = SOLD_OUT;
070                        } else {
071                                state = NO_QUARTER;
072                        }
073                } else if (state == NO_QUARTER) {
074                        System.out.println("You need to pay first");
075                } else if (state == SOLD_OUT) {
076                        System.out.println("No gumball dispensed");
077                } else if (state == HAS_QUARTER) {
078                        System.out.println("No gumball dispensed");
079                }
080        }
081
082        public void refill(int numGumBalls) {
083                this.count = numGumBalls;
084                state = NO_QUARTER;
085        }
086
087        public String toString() {
088                StringBuilder result = new StringBuilder();
089                result.append("\nMighty Gumball, Inc.");
090                result.append("\nJava-enabled Standing Gumball Model #2004\n");
091                result.append("Inventory: " + count + " gumball");
092                if (count != 1) {
093                        result.append("s");
094                }
095                result.append("\nMachine is ");
096                if (state == SOLD_OUT) {
097                        result.append("sold out");
098                } else if (state == NO_QUARTER) {
099                        result.append("waiting for quarter");
100                } else if (state == HAS_QUARTER) {
101                        result.append("waiting for turn of crank");
102                } else if (state == SOLD) {
103                        result.append("delivering a gumball");
104                }
105                result.append("\n");
106                return result.toString();
107        }
108}
109
110