Guest User

FixedMessageSequenceProtocol

a guest
Mar 12th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.27 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4.  
  5.  
  6. public class FixedMessageSequenceProtocol {
  7.  
  8.  
  9.     static int currentPopulationRequest ;  
  10.  
  11.     public FixedMessageSequenceProtocol (int step) {
  12.         this.currentPopulationRequest =  step ; //adding one because, we want to move to the next state
  13.     }                
  14.  
  15.    
  16.     private static final int WAITING = 0;
  17.     private static final int SENTPERMISSION = 1;
  18.     private static final int SENTCLUE = 2;
  19.     private static final int ANOTHER = 3;
  20.  
  21.     private static final int NUMSTATES = 50;
  22.  
  23.     private int state = WAITING;
  24.     private int currentState = getCurrentState(); // get this from the Constructor;
  25.  
  26.     private String[] clues = {  "What is Alabama population",
  27.                                 "What is Alaska population",
  28.                                 "What is Arizona population",   //2
  29.                                 "What is Arkansas population",
  30.                                 "What is California population",  
  31.                                 "What is Colorado population",
  32.                                 "What is Connecticutt population",  
  33.                                 "What is Delaware population",
  34.                                 "What is Florida population",   //8
  35.                                 "What is Georgia population" ,
  36.                                 "What is Hawaii population",  
  37.                                 "What is Idaho population",
  38.                                 "What is Illinois population",
  39.                                 "What is Indiana population" ,
  40.                                 "What is Iowa population",
  41.                                 "What is Kansas population",
  42.                                 "What is Kentucky population",
  43.                                 "What is Louisiana population",
  44.                                 "What is Maine population",
  45.                                 "What is Maryland population",
  46.                                 "What is Massachusetts population",
  47.                                 "What is Michigan population",
  48.                                 "What is Minnesota population",
  49.                                 "What is Mississippi population",
  50.                                 "What is Missouri population",
  51.                                 "What is Montana population",
  52.                                 "What is Nebraska population",
  53.                                 "What is Nevada population",
  54.                                 "What is New Hampshire population",
  55.                                 "What is New Jersey population",
  56.                                 "What is New Mexico population",
  57.                                 "What is New York population",
  58.                                 "What is North Carolina population",
  59.                                 "What is North Dakota population",
  60.                                 "What is Ohio population",
  61.                                 "What is Oklahoma population",
  62.                                 "What is Oregon population",
  63.                                 "What is Pennsylvania population",
  64.                                 "What is Rhode Island population",
  65.                                 "What is South Carolina population",
  66.                                 "What is South Dakota population",
  67.                                 "What is Tennessee population",
  68.                                 "What is Texas population",
  69.                                 "What is Utah population",
  70.                                 "What is Vermont population",
  71.                                 "What is Virginia population",
  72.                                 "What is Washington population",
  73.                                 "What is West Virginia population",
  74.                                 "What is Wisconsin population",
  75.                                 "What is Wyoming population"                                
  76.  
  77.                             };
  78.    
  79.  
  80.     private String[] answers = { "Alabama has a pop. of 4,889,232",
  81.                                  "Alaska has a pop. of 736,232",
  82.                                  "Arizona has a pop. of 6,731,232",
  83.                                  "Arkansas has a pop. of 2,966,112",
  84.                                  "California has a pop. of 38,800,232",
  85.                                  "Colorado has a pop. of 5,359,232",
  86.                                  "Connecticutt has a pop. of 3,574,097",
  87.                                  "Delaware has a pop. of 897,934",
  88.                                  "Florida has a pop. of 18,801,310",
  89.                                  "Georgia has a pop. of 9,687,653",
  90.                                  "Hawaii has a pop. of 1,360,301",
  91.                                  "Idaho has a pop. of 1,567,582",
  92.                                  "Illinois has a pop. of 12,830,632",
  93.                                  "Indiana has a pop. of 6,483,802",
  94.                                  "Iowa has a pop. of 3,083,226",
  95.                                  "Kansas has a pop. of 2,883,802",
  96.                                  "Kentucky has a pop. of 4,333,802",
  97.                                  "Louisiana has a pop. of 4,533,802",
  98.                                  "Maine has a pop. of 1,333,802",
  99.                                  "Maryland has a pop. of 5,773,802",
  100.                                  "Massachusetts has a pop. of 6,547,802",
  101.                                  "Michigan has a pop. of 9,883,112",
  102.                                  "Minnesota has a pop. of 5,307,621",
  103.                                  "Mississippi has a pop. of 2,967,702",  
  104.                                  "Missouri has a pop. of 5,988,412",
  105.                                  "Montana has a pop. of 989,102",  
  106.                                  "Nebraska has a pop. of 1,827,102",
  107.                                  "Nevada has a pop. of 2,700,402",
  108.                                  "New Hampshire has a pop. of 1,316,802",
  109.                                  "New Jersey has a pop. of 8,791,442",
  110.                                  "New Mexico has a pop. of 2,059,332",
  111.                                  "New York has a pop. of 19,378,282",
  112.                                  "North Carolina has a pop. of 9,547,802",
  113.                                  "North Dakota has a pop. of 670,802",
  114.                                  "Ohio has a pop. of 11,547,332",
  115.                                  "Oklahoma has a pop. of 3,707,802",
  116.                                  "Oregon has a pop. of 4,547,802", // check
  117.                                  "Pennsylvania has a pop. of 7,247,802",
  118.                                  "Rhode Island has a pop. of 1,547,802",
  119.                                  "South Carolina has a pop. of 1,147,802",
  120.                                  "South Dakota has a pop. of 547,802",
  121.                                  "Tennessee has a pop. of 5,547,802",
  122.                                  "Texas has a pop. of 22,547,802",
  123.                                  "Utah has a pop. of 2,547,802",
  124.                                  "Vermont has a pop. of 2,007,802",
  125.                                  "Virginia has a pop. of 10,547,802",
  126.                                  "Washington has a pop. of 7,547,802",
  127.                                  "West Virginia has a pop. of 2,117,802",
  128.                                  "Wisconsin has a pop. of 4,222,112",
  129.                                  "Wyoming has a pop. of 747,802"
  130.  
  131.                                  };  
  132.  
  133.  
  134.  
  135.     public String processInput(String theInput) {
  136.  
  137.         String theOutput = null;
  138.  
  139.         if (state == WAITING) {
  140.             theOutput = "May I have permission to ask you to send a state-population request?";
  141.             state = SENTPERMISSION  ;
  142.         } else if (state == SENTPERMISSION) {
  143.  
  144.             if (theInput.equalsIgnoreCase("Permission granted.")) {  
  145.                 theOutput =  "Provide a state-population query.";  
  146.  
  147.                 state = SENTCLUE;
  148.             } else {
  149.                 theOutput = "You're supposed to say \"Permission granted. \"! " +
  150.                 "Try again. Request to send a state population";
  151.             }
  152.         } else if (state == SENTCLUE) {
  153.             if (theInput.equalsIgnoreCase(clues[currentPopulationRequest])) {  
  154.                 theOutput = answers[currentPopulationRequest] + " Want another? (y/n)";
  155.                 currentPopulationRequest++;
  156.                 state = ANOTHER ;  
  157.             } else {
  158.                 theOutput = "You're supposed to say \"" +
  159.                 clues[currentPopulationRequest]  +
  160.                 "! Try again. Request to send a state population";
  161.                 state = SENTPERMISSION;
  162.             }
  163.         } else if (state == ANOTHER) {
  164.             if (theInput.equalsIgnoreCase("y")) {
  165.                 theOutput = "Request to send a state population";
  166.                 if (currentPopulationRequest == NUMSTATES)
  167.                     currentPopulationRequest = 0;
  168.                 else
  169.                     currentState++;  
  170.                 state = SENTPERMISSION;
  171.             } else {
  172.                 theOutput = "Bye.";      
  173.                 state = WAITING;
  174.             }
  175.         }
  176.         return theOutput;
  177.     }
  178.  
  179.  
  180.    public int getCurrentState(){      
  181.        return currentPopulationRequest;
  182.    }
  183.  
  184. }// end class FixedMessageSequenceProtocol
Add Comment
Please, Sign In to add comment