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

/**********************************************************************
 * Class purpose: Simulate an 8-ball
 * 
 * @author  Nancy Harris
 * @version 10/13/09
 *********************************************************************/

public class EightBallV3
{
   /******************************************************************
    * 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      seed;				// User input: the random generator seed
      Random   answerPicker;	// The random generator object
		String	answerStr;		// holds answer from picker
   
      stdin = new Scanner(System.in);
      System.out.printf("Magic 8 Ball\n\n");

		// Moved the seed to the top, we only need one
      System.out.printf("Type a large positive integer: ");
		if (stdin.hasNextInt())
			seed = stdin.nextInt();
		else
			seed = 123456789;
		stdin.nextLine();  // consume new line
		
		answerPicker = new Random(seed);
			
		// This will initialize the big loop
		System.out.print("Do you want to ask a question(yes/no)? ");
		wantToAsk = stdin.nextLine();
		System.out.println();
		
		// decision		
		while (wantToAsk.equalsIgnoreCase("Yes"))
		{
			//*****************
	      // Get the 3 inputs
	      //*****************
	      System.out.printf("What is your question?\n");
	      questionStr = stdin.nextLine();
			
			questionStr = checkString(questionStr, stdin);
							
	      System.out.printf("\n");
	
	      //***************
	      // Pick an answer
	      //***************
			answerStr = getAnswer(answerPicker);
			
	      //***************
	      // Output results
	      //***************
	      System.out.printf("Question: %s\n", questionStr);
	      System.out.printf("  Answer: %s\n", answerStr);
			
			//***************
			// ask again
			//***************
			System.out.print("Do you want to ask another question(yes/no)? ");
			wantToAsk = stdin.nextLine();
			System.out.println();
		}
   }
	/*************************************************************************
	 * checkString will check that the question answered is between 1 and 60 characters
	 *
	 * @param question The question to check
	 * @param kb The keyboard
	 * @return The validated questions
	 *************************************************************************/
	public static String checkString(String question, Scanner kb)
	{
		// any one of the three is an error
		while(question.length() > 60 || question.length() == 0)
		{
			// now figure out which one.
			if (question.length() > 60)
				System.out.print("Your question is too long. Be more concise.\n");
			else if (question.length() == 0)
				System.out.print("You must enter a question.  Try again.\n");
			else
				System.out.print("There is an error in your logic");
			
			System.out.printf("What is your question?\n");
      	question = kb.nextLine();
			
		} // end while
			
		return question;
	}
	/************************************************************************
	 * getAnswer will "shake" the 8-ball and obtain an answer
	 *
	 * @param answerPicker The Random object representing the 8 ball answers
	 * @return The answer String
	 ***********************************************************************/
	public static String getAnswer(Random answerPicker)
	{
		int 		answerIndex;
		String 	answerStr;
		
	   answerIndex = answerPicker.nextInt(20) + 1;
			
			// Use answerIndex to condition a switch statement.  Print the 
			// result of the answerIndex assignment to be sure you understand what
			// numbers will be generated by the nextInt() method.
	 	   switch (answerIndex)
			{
				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;
		}
}
