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

/**********************************************************************
 * Class purpose: Simulate an 8-ball
 * 
 * @author  Archer Harris updated Nancy Harris
 * Note the simplification of the main method by making each non-trivial step
 * This is a version that is the solution to the Loopy 8 Ball lab.
 * it's own method.
 * @version V4 3/23/2015
 *********************************************************************/

public class EightBallSol2
{
/******************************************************************
 * Method purpose: simulate a Magic 8-ball
 *
 * @param args Command line arguments, ignored
 *****************************************************************/
   public static void main(String [] args)
   {
      Scanner  stdin;      // Input Stream
      String   wantToAsk;  // User input: want to ak a question
      String   questionStr;   // User input: the question
      int      answerIndex;   // The index value returned by random generator
      String   answerStr;  // The program's answer
      Random   randi;      // The random generator
         
      stdin = new Scanner(System.in);
      System.out.printf("Magic 8 Ball\n\n");
   
      System.out.print("Do you want to ask a question(yes/no)? ");
      wantToAsk = stdin.nextLine();
      System.out.println();
      randi = new Random();
   
      while (wantToAsk.equalsIgnoreCase("Yes"))
      {
      //*****************
      // Get the inputs
      //*****************
         System.out.printf("What is your question?\n");
         questionStr = stdin.nextLine();
         questionStr = checkQuestion(questionStr, stdin);
                  
         //***************
         // Pick an answer number
         //***************
            
         answerIndex = pickRandom(randi);
         
         //****************
         // Pick the answer 
         //****************
         answerStr = pickAnswer(answerIndex);
                   
         //***************
         // Output results
         //***************
         System.out.printf("Question: %s\n", questionStr);
         System.out.printf("  Answer: %s\n", answerStr);
         
         System.out.print("Do you want to ask another question? ");
         wantToAsk = stdin.nextLine();
      }
      
   }

/***********************
* 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
* @result true if the question is okay and false if it is not
***********************/
   public static String checkQuestion(String question, Scanner kb)
   {
      boolean good;     
      good = false;
           
      while(!good)  // true means its okay
      {
         if(question.length() > 60)
         {
            System.out.print("Your question is too long. Be more concise.\n");
            System.out.print("What is your question? ");
            question = kb.nextLine();
         }
         else if (question.length() < 3)
         {
            System.out.println("Your question is too short. Please expand.\n");
            System.out.print("What is your question? ");
            question = kb.nextLine();
         }
         else if (!question.endsWith("?"))
         {
            System.out.println("This is not a question. Questions end with '?'");
            System.out.print("What is your question? ");
            question = kb.nextLine();
         }
         else
         {
            good = true;
         }
      }
      return question;
   
   } // 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(Random randi)
   {
      int randomNum;
      
      // generate a number in the 1 - 20 range
      randomNum = (int) (randi.nextInt(20) + 1);
      
      return randomNum;
   
   } // end pickRandom

/***************************
* pickAnswer chooses the answer from a list of possible answers
*
* @param index The index of the problem to choose
* @return A String representing an answer to a question
****************************/
   public static String pickAnswer(int index)
   { 
      String answerStr;
       
      switch (index)
      {
         case 1:
            answerStr = "Signs point to yes.";
            break;
         case 2: 
            answerStr = "Yes.";
            break;
         case 3:
            answerStr = "Reply hazy, try again.";
            break;
         case 4:
            answerStr = "Without a doubt.";
            break;
         case 5:
            answerStr = "My sources say no.";
            break;
         case 6:
            answerStr = "As I see it, yes.";
            break;
         case 7: 
            answerStr = "You may rely on it.";
            break;
         case 8:
            answerStr = "Concentrate and ask again.";
            break;
         case 9:
            answerStr = "Outlook not so good.";
            break;
         case 10:
            answerStr = "It is decidedly so.";
            break;
         case 11:
            answerStr = "Better not tell you now.";
            break;
         case 12:
            answerStr = "Very doubtful.";
            break;
         case 13:
            answerStr = "Yes - definitely.";
            break;
         case 14:
            answerStr = "It is certain.";
            break;
         case 15:
            answerStr = "Cannot predict now.";
            break;
         case 16:
            answerStr = "Most likely.";
            break;
         case 17: 
            answerStr = "Ask again later.";
            break;
         case 18:
            answerStr = "My reply is no.";
            break;
         case 19:
            answerStr = "Outlook good.";
            break;
         case 20:
            answerStr = "Don't count on it.";
            break;
         default:
            answerStr = "HUH?";
            break;
      }
      return answerStr;
   }
}
