
/**
 * This program administers a math quiz containing 10 simple addition, subtraction, multiplication,
 * and division problems.  The problems are built into the program, so that the same quiz is
 * adminstered every time the program runs.  After all the questions have been asked, the user
 * will have a second chance to answer any questions that were not answwered correctly on the
 * first try.  The user gets 10 points for each question answered correctly on the first try and
 * 5 points for each question answered correctly on the second try.
 */
public class MathQuizCL {
	
	static QuizQuestion[] questions = new QuizQuestion[10];  // the questions on the quiz
	static int[] userAnswers = new int[10];                  // the user's answers to the questions
	static int score = 0;                                    // the user's score.
	
	public static void main(String[] args) {

		System.out.println("You are about to take a math quiz.  There are ten questions.  After you have");
		System.out.println("answered each question, you will have a second chance at any questions that you");
		System.out.println("don't answer correctly the first time.  Questions answered correctly on the");
		System.out.println("second try count for half credit.  Let's begin.\n\n");
		
		createQuestions();

		firstRound();
	
		if (score == 100) {
			System.out.println("\n\nCongratulations.  You got all the questions correct!");
			System.out.println("Your score is 100%.\n\n");
		}
		else {
			System.out.println("\n\nYou're score so far is " + score + "%.  You have another shot at the");
			System.out.println("questions you missed...\n\n");
			secondRound();
			System.out.println("\n\nYou're final score is " + score + "%.\n\n");
		}

	}
	

	/**
	 * Creates 10 question objects and stores them in the questions array.  This method
	 * always generates the same set of questions.
	 */
	static void createQuestions() {
		questions[0] = new QuizQuestion(13, '+', 25);
		questions[1] = new QuizQuestion(183, '-', 51);
		questions[2] = new QuizQuestion(9, '*', 7);
		questions[3] = new QuizQuestion(81, '/', 3);
		questions[4] = new QuizQuestion(103, '+', 78);
		questions[5] = new QuizQuestion(97, '+', 0);
		questions[6] = new QuizQuestion(43, '-', 17);
		questions[7] = new QuizQuestion(1789, '*', 10);
		questions[8] = new QuizQuestion(27, '*', 6);
		questions[9] = new QuizQuestion(500, '/', 25);
	}
	

	/**
	 * Ask the user each of the questions in the questions array, and record the user's answers
	 * in the userAnswers array.  The user is informed whether the answer is correct or not.
	 * If the user answers correctly, 10 points are added to the score.
	 */
	static void firstRound() {
		for (int i = 0; i < 10; i++) {
			System.out.println("\n\nQUESTION NUMBER " + (i+1) + ":\n");
			System.out.println("\n   " + questions[i].getQuestion());
			System.out.print("\n   Your answer:  ? ");
			userAnswers[i] = TextIO.getlnInt();
			if (userAnswers[i] == questions[i].getAnswer()) {
				System.out.println("\nThat is correct.  You get 10 points for this question.");
				score += 10;
			}
			else {
				System.out.println("\nSorry, that is not correct.");
			}
		}
	}
	
	/**
	 * Asks the user any question for this the answer in the first round was incorrect.
	 * If the user answers correctly, 5 points are added to the score.
	 */
	static void secondRound() {
		for (int i = 0; i < 10; i++) {
			if (userAnswers[i] != questions[i].getAnswer()) {
				System.out.println("\n\nQUESTION NUMBER " + (i+1) + ":\n");
				System.out.println("\n   " + questions[i].getQuestion());
				System.out.println("\n   Last time, you gave the incorrect answer " + userAnswers[i] + ".");
				System.out.print("\n   Your answer:  ? ");
				userAnswers[i] = TextIO.getlnInt();
				if (userAnswers[i] == questions[i].getAnswer()) {
					System.out.println("\nThat is correct.  You have earned 5 points");
					score += 5;
				}
				else {
					System.out.println("\nSorry, that is not correct.  You get 0 points for this question.");
					System.out.println("The correct answer was " + questions[i].getAnswer() + ".");
				}
			}
		}
	}


}

