Source of Oracle.java


  1: //Oracle.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: public class Oracle
  6: {
  7:     private String oldAnswer = "The answer is in your heart.";
  8:     private String newAnswer;
  9:     private String question;
 10: 
 11:     public void chat()
 12:     {
 13:         System.out.print("I am the oracle. ");
 14:         System.out.println("I will answer any one-line question.");
 15:         Scanner keyboard = new Scanner(System.in);
 16:         String response;
 17:         do
 18:         {
 19:             answer();
 20:             System.out.println("Do you wish to ask another question?");
 21:             response = keyboard.next();
 22:         }
 23:         while (response.equalsIgnoreCase("yes"));
 24:         System.out.println("The oracle will now rest.");
 25:     }
 26: 
 27:     private void answer()
 28:     {
 29:         System.out.println("What is your question?");
 30:         Scanner keyboard = new Scanner(System.in);
 31:         question = keyboard.nextLine();
 32:         seekAdvice();
 33:         System.out.println("You asked the question:");
 34:         System.out.println("  " + question);
 35:         System.out.println("Now, here is my answer:");
 36:         System.out.println("  " + oldAnswer);
 37:         update();
 38:     }
 39: 
 40:     private void seekAdvice()
 41:     {
 42:         System.out.println("Hmm, I need some help on that.");
 43:         System.out.println("Please give me one line of advice.");
 44:         Scanner keyboard = new Scanner(System.in);
 45:         newAnswer = keyboard.nextLine();
 46:         System.out.println("Thank you. That helped a lot.");
 47:     }
 48: 
 49:     private void update()
 50:     {
 51:         oldAnswer = newAnswer;
 52:     }
 53: }