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