FileDocCategorySizeDatePackage
KnockKnockProtocol.javaAPI DocExample2191Tue Dec 12 18:58:32 GMT 2000None

KnockKnockProtocol

public class KnockKnockProtocol extends Object

Fields Summary
private static final int
WAITING
private static final int
SENTKNOCKKNOCK
private static final int
SENTCLUE
private static final int
ANOTHER
private static final int
NUMJOKES
private int
state
private int
currentJoke
private String[]
clues
private String[]
answers
Constructors Summary
Methods Summary
public java.lang.StringprocessInput(java.lang.String theInput)


        
        String theOutput = null;

        if (state == WAITING) {
            theOutput = "Knock! Knock!";
            state = SENTKNOCKKNOCK;
        } else if (state == SENTKNOCKKNOCK) {
            if (theInput.equalsIgnoreCase("Who's there?")) {
                theOutput = clues[currentJoke];
                state = SENTCLUE;
            } else {
                theOutput = "You're supposed to say \"Who's there?\"! " +
			    "Try again. Knock! Knock!";
            }
        } else if (state == SENTCLUE) {
            if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
                theOutput = answers[currentJoke] + " Want another? (y/n)";
                state = ANOTHER;
            } else {
                theOutput = "You're supposed to say \"" + 
			    clues[currentJoke] + 
			    " who?\"" + 
			    "! Try again. Knock! Knock!";
                state = SENTKNOCKKNOCK;
            }
        } else if (state == ANOTHER) {
            if (theInput.equalsIgnoreCase("y")) {
                theOutput = "Knock! Knock!";
                if (currentJoke == (NUMJOKES - 1))
                    currentJoke = 0;
                else
                    currentJoke++;
                state = SENTKNOCKKNOCK;
            } else {
                theOutput = "Bye.";
                state = WAITING;
            }
        }
        return theOutput;