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