State patterns not using state patterns

Source: Internet
Author: User

The state mode controls the transition of the State, and the program is cumbersome when the design pattern is not used.

Specific methods:

1. state transition 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 Insertquarte R () {//Enter 25 cents 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 are sold out."); else if (state = = sold) {SysTEM.OUT.PRINTLN ("Please wait, we ' re already to giving you a gumball."); } public void Ejectquarter () {//return 25 cents if (state = = Has_quarter) {Syste  
            M.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 gumballs");  
            else if (state = = Has_quarter) {System.out.println ("you turned ...");  
            state = sold;  
        Dispense (); } public void Dispense () {if (state = Sold) {System.out.println ("A gum  
            Ball comes rolling out the slot. ");  
            count--;  
                if (count = = 0) {System.out.println ("opps, out of gumballs.");  
            state = Sold_out;  
            else {state = No_quarter;  
        } else if (state = = No_quarter) {System.out.println ("You need to pay");  
        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 numgumballs) {This.count = nUmgumballs;  
    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 (); }     
}

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.