State pattern not used
Address: http://blog.csdn.net/caroline_wendy
Status ModeStatus conversion can be controlled, not usedDesign ModeThe program will be very complicated.
Specific Method:
1. Status conversion class.
/*** @ Time July 11, 2014 */package state;/*** @ author C. l. wang **/public class gumballmachine {final static int sold_out = 0; Final Static int no_quarter = 1; Final Static int has_quarter = 2; Final Static int sold = 3; int state = sold_out; int COUNT = 0; Public gumballmachine (INT count) {This. count = count; If (count> 0) {state = no_quarter; // Conversion Mode} public void insertquarter () {// enter 25 cent if (State = ha S_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 ejectquarter () {// Refund 25 cent if (State = has_quarter) {system. out. println ("quarter returned. "); State = no_quarter;} else if (State = no_quarter) {system. out. println ("You haven't inserted a quarter. ");} else if (State = sold) {system. out. println ("sorry, you already turned the crank. ");} else if (State = sold_out) {system. out. println ("You can't eject, you haven't inserted a quarter yet. ") ;}} public void turncrank () {// Shake the lever if (State = sold) {system. out. println ("turning twice doesn't get 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 gumbils");} else if (State = has_quarter) {system. out. println ("You turned... "); State = sold; dispense () ;}} public void dispense () {If (S Tate = sold) {system. out. println ("A gumball comes rolling out the slot. "); count --; If (COUNT = 0) {system. out. println ("OPPs, out of gumbils. "); 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 refill (INT numgumbils) {This. count = numgumbils; State = no_quarter;} Public String tostring () {stringbuffer result = new stringbuffer (); result. append ("\ nmighty Gumball, Inc. "); result. append ("\ njava-enabled standing gumball model #2004 \ n"); result. append ("inventory:" + Count + "gumball"); If (count! = 1) {result. append ("S");} result. append ("\ nmachine is"); If (State = sold_out) {result. append ("sold out. ");} else if (State = no_quarter) {result. append ("waiting for quarter. ");} else if (State = has_quarter) {result. append ("waitring for turn of crank. ");} else if (State = sold) {result. append ("delivering a gumball. ");} result. append ("\ n"); return result. tostring ();}}
2. Test the code.
/*** @ Time July 11, 2014 */package state;/*** @ author C. l. wang **/public class gumballmachinetestdrive {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubgumballmachine gumballmachine = new gumballmachine (5); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. turncrank (); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. ejectquarter (); gumballmachine. turncrank (); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. turncrank (); gumballmachine. insertquarter (); gumballmachine. turncrank (); gumballmachine. ejectquarter (); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. insertquarter (); gumballmachine. turncrank (); gumballmachine. insertquarter (); gumballmachine. turncrank (); gumballmachine. insertquarter (); gumballmachine. turncrank (); system. out. println (gumballmachine );}}
3. output.
Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 5 gumballsMachine is waiting for quarter.You inserted a quarter.You turned...A gumball comes rolling out the slot.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 4 gumballsMachine is waiting for quarter.You inserted a quarter.Quarter returned.You turned but there‘s no quarter.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 4 gumballsMachine is waiting for quarter.You inserted a quarter.You turned...A gumball comes rolling out the slot.You inserted a quarter.You turned...A gumball comes rolling out the slot.You haven‘t inserted a quarter.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 2 gumballsMachine is waiting for quarter.You inserted a quarter.You can‘t insert another quarter.You turned...A gumball comes rolling out the slot.You inserted a quarter.You turned...A gumball comes rolling out the slot.Opps, out of gumballs.You can‘t insert a quarter, the machine is sold out.You turned, but there are no gumballsMighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 0 gumballsMachine is sold out.