001package headfirst.state.gumballstatewinner;
002
003public class SoldState implements State {
004        GumballMachine gumballMachine;
005
006        public SoldState(GumballMachine gumballMachine) {
007                this.gumballMachine = gumballMachine;
008        }
009
010        public void insertQuarter() {
011                System.out.println("Please wait, we're already giving you a gumball");
012        }
013
014        public void ejectQuarter() {
015                System.out.println("Sorry, you already turned the crank");
016        }
017
018        public void turnCrank() {
019                System.out.println("Turning twice doesn't get you another gumball!");
020        }
021
022        public void dispense() {
023                gumballMachine.releaseBall();
024                if (gumballMachine.getCount() > 0) {
025                        gumballMachine.setState(gumballMachine.getNoQuarterState());
026                } else {
027                        System.out.println("Oops, out of gumballs!");
028                        gumballMachine.setState(gumballMachine.getSoldOutState());
029                }
030        }
031
032        public String toString() {
033                return "dispensing a gumball";
034        }
035}