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