publicclassGumballMachine{finalstaticintSOLD_OUT=0;finalstaticintNO_QUARTER=1;finalstaticintHAS_QUARTER=0;finalstaticintSOLD=3;intstate=SOLD_OUT;intcount=0;publicGumballMachine(intcount){this.count=count;if(count>0){state=NO_QUARTER;}}publicvoidinsertQuarter(){if(state==HAS_QUARTER){System.out.println("You can't insert another quarter");}elseif(state==NO_QUARTER){state=HAS_QUARTER;System.out.println("You inserted a quarter");}elseif(state==SOLD_OUT){System.out.println("You can't insert a quarter, the machine is sold out");}elseif(state==SOLD){System.out.println("Please wait, we are already giving you a gumball");}}publicvoidejectQuarter(){if(state==HAS_QUARTER){System.out.println("Quarter returned");state=NO_QUARTER;}elseif(state==NO_QUARTER){System.out.println("You haven't inserted a quarter");}elseif(state==SOLD_OUT){System.out.println("You can't eject, you haven't inserted a quarter yet");}elseif(state==SOLD){System.out.println("Sorry, you have turned the crank");}}publicvoidturnCrank(){if(state==HAS_QUARTER){System.out.println("You turned...");state=SOLD;dispense();}elseif(state==NO_QUARTER){System.out.println("You turned but there's not quarter");}elseif(state==SOLD_OUT){System.out.println("You turned, but there's no gumballs");}elseif(state==SOLD){System.out.println("Turn twice does not give you another gumball");}}privatevoiddispense(){if(state==HAS_QUARTER){System.out.println("No gumball dispensed");}elseif(state==NO_QUARTER){System.out.println("You need to pay first");}elseif(state==SOLD_OUT){System.out.println("No gumball dispensed");}elseif(state==SOLD){System.out.println("A gumball comes rolling out of the slot");count--;if(count==0){System.out.println("Oops, out of gumballs");state=SOLD_OUT;}else{state=NO_QUARTER;}}}}
publicclassHasQuarterStateimplementsState{RandomrandomWinner=newRandom(System.currentTimeMillis());GumballMachinegumballMachine;publicHasQuarterState(GumballMachinegumballMachine){this.gumballMachine=gumballMachine;}@OverridepublicvoidinsertQuarter(){System.out.println("You can't insert another quarter");}@OverridepublicvoidejectQuarter(){System.out.println("Quarter returned");gumballMachine.setState(gumballMachine.getNoQuarterState());}@OverridepublicvoidturnCrank(){System.out.println("You turned...");intwinner=randomWinner.nextInt(10);if(winner==0&&gumballMachine.getCount()>1){gumballMachine.setState(gumballMachine.getWinnerState());}else{gumballMachine.setState(gumballMachine.getSoldState());}}@Overridepublicvoiddispense(){System.out.println("No gumball dispensed");}}
12345678910111213141516171819202122232425262728
publicclassNoQuarterStateimplementsState{privateGumballMachinegumballMachine;publicNoQuarterState(GumballMachinegumballMachine){this.gumballMachine=gumballMachine;}@OverridepublicvoidinsertQuarter(){System.out.println("You inserted a quarter");gumballMachine.setState(gumballMachine.getHasQuarterState());}@OverridepublicvoidejectQuarter(){System.out.println("You haven't inserted a quarter");}@OverridepublicvoidturnCrank(){System.out.println("You turned but there's not quarter");}@Overridepublicvoiddispense(){System.out.println("You need to pay first");}}
publicclassSoldOutStateimplementsState{GumballMachinegumballMachine;publicSoldOutState(GumballMachinegumballMachine,intcount){this.gumballMachine=gumballMachine;refill(count);}privatevoidrefill(intcount){gumballMachine.setCount(count);}@OverridepublicvoidinsertQuarter(){System.out.println("You can't insert a quarter, the machine is sold out");}@OverridepublicvoidejectQuarter(){System.out.println("You can't eject, you haven't inserted a quarter yet");}@OverridepublicvoidturnCrank(){System.out.println("You turned, but there's no gumballs");}@Overridepublicvoiddispense(){System.out.println("No gumball dispensed");}}
publicclassSoldStateimplementsState{GumballMachinegumballMachine;publicSoldState(GumballMachinegumballMachine){this.gumballMachine=gumballMachine;}@OverridepublicvoidinsertQuarter(){System.out.println("Please wait, we are already giving you a gumball");}@OverridepublicvoidejectQuarter(){System.out.println("You can't eject, you haven't inserted a quarter yet");}@OverridepublicvoidturnCrank(){System.out.println("You turned, but there's no gumballs");}@Overridepublicvoiddispense(){gumballMachine.releaseBall();if(gumballMachine.getCount()>0){gumballMachine.setState(gumballMachine.getNoQuarterState());}else{System.out.println("Oops, out of gumballs");gumballMachine.setState(gumballMachine.getSoldOutState());}}}
publicclassGumballMachine{StatesoldOutState;StatenoQuarterState;StatehasQuarterState;StatesoldState;Statestate=soldOutState;intcount=0;publicGumballMachine(intnumberGumballs){soldOutState=newSoldOutState(this,5);noQuarterState=newNoQuarterState(this);hasQuarterState=newHasQuarterState(this);soldState=newSoldState(this);this.count=numberGumballs;if(count<0){state=noQuarterState;}}publicvoidinsertQuarter(){state.insertQuarter();}publicvoidejectQuarter(){state.ejectQuarter();}publicvoidturnCrank(){state.turnCrank();}publicvoiddispense(){state.turnCrank();state.dispense();}publicvoidreleaseBall(){System.out.println("A gumball comes rolling out the slot..,");if(count!=0){count--;}}publicStategetHasQuarterState(){returnhasQuarterState;}publicStategetSoldOutState(){returnsoldOutState;}publicStategetNoQuarterState(){returnnoQuarterState;}publicStategetSoldState(){returnsoldState;}publicvoidsetState(Statestate){this.state=state;}publicintgetCount(){returncount;}publicvoidsetCount(intcount){this.count=count;state=noQuarterState;}}