001package headfirst.state.gumballstatewinner;
002
003import java.util.Random;
004
005public class HasQuarterState implements State {
006        Random randomWinner = new Random(System.currentTimeMillis());
007        GumballMachine gumballMachine;
008
009        public HasQuarterState(GumballMachine gumballMachine) {
010                this.gumballMachine = gumballMachine;
011        }
012
013        public void insertQuarter() {
014                System.out.println("You can't insert another quarter");
015        }
016
017        public void ejectQuarter() {
018                System.out.println("Quarter returned");
019                gumballMachine.setState(gumballMachine.getNoQuarterState());
020        }
021
022        public void turnCrank() {
023                System.out.println("You turned...");
024                int winner = randomWinner.nextInt(10);
025                if ((winner == 0) && (gumballMachine.getCount() > 1)) {
026                        gumballMachine.setState(gumballMachine.getWinnerState());
027                } else {
028                        gumballMachine.setState(gumballMachine.getSoldState());
029                }
030        }
031
032        public void dispense() {
033                System.out.println("No gumball dispensed");
034        }
035
036        public String toString() {
037                return "waiting for turn of crank";
038        }
039}