// Arup Guha
// 2/11/2017
// Alternate Solution to one fraction exercise

import java.util.*;

public class fractiongame2 {

	// Constant that stores all operations.
	final public static String OPLIST = "+-*/";

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		Random r = new Random();

		System.out.println("How many problems do you want?");
		int numProbs = stdin.nextInt();

		// Keeps track of score.
		int score = 0;
		long startTime = System.currentTimeMillis();

		// Go through problems.
		for (int i=0; i<numProbs; i++) {

			// Create the first fraction.
			int n1 = r.nextInt(10);
			int n2 = 1 + r.nextInt(10);
			fraction f1 = new fraction(n1, n2);

			// Create the second fraction.
			n1 = r.nextInt(10);
			n2 = 1 + r.nextInt(10);
			fraction f2 = new fraction(n1, n2);

			// Randomly generate which operation we want.
			int op = r.nextInt(4);

			// Run this question and score it.
			score += askQuestion(f1, f2, op);
		}

		long endTime = System.currentTimeMillis();
		double sec = (endTime-startTime)/1000.0;
		double perQ = sec/numProbs;

		// Output results.
		System.out.println("You got "+score+" questions out of "+numProbs+" for a percentage of "+(100.0*score/numProbs));
		System.out.println("This took you "+sec+" seconds, which is "+perQ+" seconds per question.");
	}

	public static int askQuestion(fraction f1, fraction f2, int op) {

		Scanner stdin = new Scanner(System.in);

		// Ask the question, get the response and calculate the correct answer.
		System.out.println("What is "+f1+" "+OPLIST.charAt(op)+" "+f2+"?");
		String res = stdin.next();
		fraction ans = new fraction(res);
		fraction correct = operation(f1, f2, op);

		// See if they got it!
		if (ans.equals(correct)) {
			System.out.println("Great, you got it!");
			return 1;
		}
		else {
			System.out.println("That's not correct. The answer is "+correct);
			return 0;
		}
	}

	// Returns f1 op f2, where op=0 is +, op=1 is -, op=2 is *, op=3 is /.
	public static fraction operation(fraction f1, fraction f2, int op) {
		if (op == 0) return f1.add(f2);
		if (op == 1) return f1.sub(f2);
		if (op == 2) return f1.mult(f2);
		return f1.div(f2);
	}
}