001package headfirst.proxy.gumballmonitor;
002
003@SuppressWarnings("serial")
004public class WinnerState implements State {
005        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                try {
026                        gumballMachine.releaseBall();
027                        if (gumballMachine.getCount() == 0) {
028                                gumballMachine.setState(gumballMachine.getSoldOutState());
029                        } else {
030                                gumballMachine.releaseBall();
031                                if (gumballMachine.getCount() > 0) {
032                                        gumballMachine.setState(gumballMachine.getNoQuarterState());
033                                } else {
034                                        System.out.println("Oops, out of gumballs!");
035                                        gumballMachine.setState(gumballMachine.getSoldOutState());
036                                }
037                        }
038                } catch (Exception e) {
039                        e.printStackTrace();
040                }
041        }
042
043        public String toString() {
044                return "despensing two gumballs for your quarter, because YOU'RE A WINNER!";
045        }
046}