home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / networking / sockets / example / KKState.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.9 KB  |  75 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.net.*;
  18. import java.io.*;
  19.  
  20. class KKState {
  21.     private static final int WAITING = 0;
  22.     private static final int SENTKNOCKKNOCK = 1;
  23.     private static final int SENTCLUE = 2;
  24.     private static final int ANOTHER = 3;
  25.  
  26.     private static final int NUMJOKES = 5;
  27.  
  28.     private int state = WAITING;
  29.     private int currentJoke = 0;
  30.  
  31.     private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
  32.     private String[] answers = { "Turnip the heat, it's cold in here!",
  33.                                  "I didn't know you could yodel!",
  34.                                  "Bless you!",
  35.                                  "Is there an owl in here?",
  36.                                  "Is there an echo in here?" };
  37.  
  38.     String processInput(String theInput) {
  39.         String theOutput = null;
  40.  
  41.         if (state == WAITING) {
  42.             theOutput = "Knock! Knock!";
  43.             state = SENTKNOCKKNOCK;
  44.         } else if (state == SENTKNOCKKNOCK) {
  45.             if (theInput.equalsIgnoreCase("Who's there?")) {
  46.                 theOutput = clues[currentJoke];
  47.                 state = SENTCLUE;
  48.             } else {
  49.                 theOutput = "You're supposed to say \"Who's there?\"! Try again. Knock! Knock!";
  50.             }
  51.         } else if (state == SENTCLUE) {
  52.             if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
  53.                 theOutput = answers[currentJoke] + " Want another? (y/n)";
  54.                 state = ANOTHER;
  55.             } else {
  56.                 theOutput = "You're supposed to say \"" + clues[currentJoke] + " who?\"" + "! Try again. Knock! Knock!";
  57.                 state = SENTKNOCKKNOCK;
  58.             }
  59.         } else if (state == ANOTHER) {
  60.             if (theInput.equalsIgnoreCase("y")) {
  61.                 theOutput = "Knock! Knock!";
  62.                 if (currentJoke == (NUMJOKES - 1))
  63.                     currentJoke = 0;
  64.                 else
  65.                     currentJoke++;
  66.                 state = SENTKNOCKKNOCK;
  67.             } else {
  68.                 theOutput = "Bye.";
  69.                 state = WAITING;
  70.             }
  71.         }
  72.         return theOutput;
  73.     }
  74. }
  75.