import java.util.Scanner;
import java.util.Random;

/**********************************************************************
 * Class purpose: Simulate an 8-ball
 * 
 * @author  Archer Harris updated Nancy Harris
 * @version 10/10/2013
 *********************************************************************/
public class EightBall
{

  /******************************************************************
   * Method purpose: simulate an 8-ball
   *
   * @param args Command line arguments, ignored
   *****************************************************************/
  public static void main(String [] args)
  {
    Scanner  keyboard;   // Input Stream
    
    String   wantToAsk;  // User input: want to ask a question
    String   questionStr; // User input: the question
    
    int      answerIndex; // The index value returned by random generator
    String   answerStr;  // The program's answer
    
    int      seed;    // User input: the random generator seed
    
    // 1. Create a Scanner object (initialize keyboard)
    keyboard = new Scanner(System.in);
    
    // 2. Output the heading  
    System.out.println("Magic 8 Ball\n\n");
    

    // 3. Get inputs, see detail below
        
    //*****************
    // 4. Check if the user wants to enter a question or not.
    //*****************
    System.out.println("Do you want to ask a question(yes/no)? ");
    wantToAsk = keyboard.nextLine();
    
    if (!wantToAsk.equalsIgnoreCase("yes"))
       System.exit(1);
       
    //*****************
    // 5. Input the user's questions, and check the length of the question.  See checkQuestion method at bottom of lab.
    //*****************
    System.out.println("What is your question?");
    questionStr = keyboard.nextLine();
    checkQuestion(questionStr);
    
    //***************
    // 6. Pick an answer -- See pickRandom method at bottom of lab.
    //***************
    answerIndex = pickRandom();
    answerStr = getAnswer(answerIndex);
    
    //***************
    // 7. Output results
    //***************
    System.out.printf("Question: %s\n", questionStr);

    System.out.printf("  Answer: %s\n", answerStr);
    
  } // end main
  
  /***********************
   * method checkQuestion will check the question entered by the user 
   * and if it is too long, will display the error message and exit the program.
   *
   * @param question The question that was asked
   ***********************/
  public static void checkQuestion(String question)
  {
    if (question.length() > 60)
      System.exit(2);
  } // end checkQuestion
  
  /***********************
   * method pickRandom will pick a random number from 1 - 20  
   * use the Math.random class to choose a random number between 0 and 1
   * then scale that number to 1 - 20. 
   *
   * @return the random number
   ***********************/
  public static int pickRandom()
  {
      return (int)((Math.random() * 20) + 1);
    
  } // end pickRandom
  
  /***********************
   * getAnswer will use the index to select the correct 
   * answer phrase
   *
   * @param index The answer to use
   **********************/
   public static String getAnswer(int index)
   {
      String answer;
      
      switch(index)
      {
         case 1: answer = "Signs point to yes.";
            break;
         case 2: answer = "Yes.";
            break;
         case 3: answer = "Reply hazy, try again.";
            break;
         case 4: answer = "Without a doubt.";
            break;
         case 5: answer = "My sources say no";
            break;
         case 6: answer = "As I see it, yes.";
            break;
         case 7: answer = "You may rely on it";
            break;
         case 8: answer = "Concentrate and ask again.";
            break;
         case 9: answer = "Outlook not so good.";
            break;
         case 10: answer = "It is decidedly so.";
            break;
         case 11: answer = "Better not tell you now.";
            break;
         case 12: answer = "Very doubtful.";
            break;
         case 13: answer = "Yes - definitely.";
            break;
         case 14: answer = "It is certain.";
            break;
         case 15: answer = "Cannot predict now.";
            break;
         case 16: answer = "Most likely";
            break;
         case 17: answer = "Ask again later";
            break;
         case 18: answer = "My reply is no";
            break;
         case 19: answer = "Outlook good.";
            break;
         case 20: answer = "Don't count on it.";
            break;
         default: answer = "Wrong index value " + index;
      }
      
      return answer;
   }
}
