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