Advertisement
Guest User

FixedMessageSequenceProtocol

a guest
Oct 4th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. public class FixedMessageSequenceProtocol {
  2.  
  3. int currentJoke;
  4.  
  5. public FixedMessageSequenceProtocol (int step) {
  6. this.currentJoke = step;
  7. }
  8.  
  9. private static final int WAITING = 0;
  10. private static final int SENTKNOCKKNOCK = 1;
  11. private static final int SENTCLUE = 2;
  12. private static final int ANOTHER = 3;
  13.  
  14. private static final int NUMJOKES = 10;
  15.  
  16. private int state = WAITING;
  17. // private int currentJoke = 0;
  18.  
  19. private String[] clues = { "Turnip", "Little Old Lady", "Doctor",
  20. "Teacher", "Dog", "Firefighter",
  21. "Zoologist", "Atch", "Who", "Who" }; // 10 Items
  22. private String[] answers = { "Turnip the heat, it's cold in here!",
  23. "I didn't know you could yodel!",
  24. "Is there an owl doctor?",
  25. "Teacher who says do ur HW please",
  26. "Dog who runs",
  27. "Firefighter who saves people",
  28. "Zoologist who discovers",
  29. "Bless you!",
  30. "Is there an owl in here?",
  31. "Is there an echo in here?" };
  32.  
  33. public String processInput(String theInput) {
  34.  
  35. String theOutput = null;
  36.  
  37. if (state == WAITING) {
  38. theOutput = "Knock! Knock!";
  39. state = SENTKNOCKKNOCK;
  40. } else if (state == SENTKNOCKKNOCK) {
  41. if (theInput.equalsIgnoreCase("Who's there?")) {
  42. theOutput = clues[currentJoke];
  43. state = SENTCLUE;
  44. } else {
  45. theOutput = "You're supposed to say \"Who's there?\"! " +
  46. "Try again. Knock! Knock!";
  47. }
  48. } else if (state == SENTCLUE) {
  49. if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
  50. theOutput = answers[currentJoke] + " Want another? (y/n)";
  51. state = ANOTHER;
  52. } else {
  53. theOutput = "You're supposed to say \"" +
  54. clues[currentJoke] +
  55. " who?\"" +
  56. "! 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement