
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































public void insertQuarter() { if (state == HAS_QUARTER) { System.out.println( "You can’t insert another quarter"); } else if (state == NO_QUARTER) { state = HAS_QUARTER; System.out.println( "You inserted a quarter"); } else if (state == SOLD_OUT) { System.out.println( "You can’t insert a quarter, the machine is sold out"); } else if (state == SOLD) { System.out.println( "Please wait, we’re already giving you a gumball"); } }
public void turnCrank() { if (state == SOLD) { System.out.println( "Turning twice doesn't get you another gumball!"); } else if (state == NO_QUARTER) { System.out.println( "You turned but there's no quarter"); } else if (state == SOLD_OUT) { System.out.println( "You turned, but there are no gumballs"); } else if (state == HAS_QUARTER) { System.out.println("You turned..."); state = SOLD; dispense(); } }
public void dispense() { if (state == SOLD) { System.out.println( "A gumball comes rolling out the slot"); count = count - 1 ; if (count == 0 ) { System.out.println( "Oops, out of gumballs!"); state = SOLD_OUT; } else { state = NO_QUARTER; } } else if (state == NO_QUARTER) { System.out.println("You need to pay first"); } else if (state == SOLD_OUT) { System.out.println("No gumball dispensed"); } else if (state == HAS_QUARTER) { System.out.println("No gumball dispensed"); } }
public void insertQuarter() { switch (state) { case State.HAS_QUARTER: System.out.println("You can’t... quarter"); break; case State.NO_QUARTER: state = State.HAS_QUARTER; System.out.println("You inserted a quarter"); break; case State.SOLD_OUT: System.out.println("You can’t... sold out"); break; case State.SOLD: System.out.println("Please wait.. ."); break; } }
public class SoldState implements State { GumballMachine gumballMachine; public SoldState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println( "Please wait, we're already giving you a gumball"); } public void ejectQuarter() { System.out.println( "Sorry, you already turned the crank"); }
public void turnCrank() { System.out.println( "Turning twice doesn't get you another gumball!"); } public void dispense() { gumballMachine.releaseBall(); if (gumballMachine.getCount() > 0 ) { gumballMachine.setState( gumballMachine.getNoQuarterState()); } else { System.out.println("Oops, out of gumballs!"); gumballMachine.setState( gumballMachine.getSoldOutState()); } }