// Project: Patterns2 // Module: state // Source code file: Test.java // Use the State pattern to implement // the states of a gumball machine. import gumball.GumballMachine; public class Test { public static void main(String[] args) { GumballMachine gbm = new GumballMachine(5); System.out.println( "*** Initially gumball machine is in NoQuarter state."); gbm.turnCrank( ); gbm.takeGumball( ); gbm.refillMachine(5); gbm.insertQuarter( ); System.out.println( ); System.out.println( "*** Successful transition to HasQuarter state."); gbm.insertQuarter( ); gbm.takeGumball( ); gbm.refillMachine(5); gbm.turnCrank( ); System.out.println( ); System.out.println( "*** Successful transition to Sold state."); gbm.insertQuarter( ); gbm.turnCrank( ); gbm.refillMachine(5); gbm.takeGumball( ); // Buy 4 more gumballs. for(int i = 2; i <= 5; i++) { System.out.println("\nGumball #" + i); gbm.insertQuarter( ); gbm.turnCrank( ); gbm.takeGumball( ); } System.out.println( "\n*** Try to buy a gumball with empty machine"); gbm.insertQuarter( ); gbm.turnCrank( ); gbm.takeGumball( ); System.out.println( "\n*** Refill machine."); gbm.refillMachine(200); } }