001package headfirst.proxy.gumball;
002
003@SuppressWarnings("serial")
004public class WinnerState implements State {
005        transient GumballMachine gumballMachine;
006
007        public WinnerState(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("Please wait, we're already giving you a Gumball");
017        }
018
019        public void turnCrank() {
020                System.out.println("Turning again doesn't get you another gumball!");
021        }
022
023        public void dispense() {
024                System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter");
025                gumballMachine.releaseBall();
026                if (gumballMachine.getCount() == 0) {
027                        gumballMachine.setState(gumballMachine.getSoldOutState());
028                } else {
029                        gumballMachine.releaseBall();
030                        if (gumballMachine.getCount() > 0) {
031                                gumballMachine.setState(gumballMachine.getNoQuarterState());
032                        } else {
033                                System.out.println("Oops, out of gumballs!");
034                                gumballMachine.setState(gumballMachine.getSoldOutState());
035                        }
036                }
037        }
038
039        public String toString() {
040                return "despensing two gumballs for your quarter, because YOU'RE A WINNER!";
041        }
042}