001package headfirst.proxy.gumballmonitor;
002
003@SuppressWarnings("serial")
004public class SoldState implements State {
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                try {
026                        if (gumballMachine.getCount() > 0) {
027                                gumballMachine.setState(gumballMachine.getNoQuarterState());
028                        } else {
029                                System.out.println("Oops, out of gumballs!");
030                                gumballMachine.setState(gumballMachine.getSoldOutState());
031                        }
032                } catch (Exception e) {
033                        e.printStackTrace();
034                }
035        }
036
037        public String toString() {
038                return "dispensing a gumball";
039        }
040}