import java.util.Scanner;
import java.io.*;

class Quiz {

    Question[] questions;
    
    public Quiz() {
    }
    
    public void loadQuiz(String filename) {
        try {
            questions = new Question[5];
            BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
            for (int i = 0; i < 5; i = i + 1) {
                questions[i] = new Question(br.readLine());
                for (int j = 0; j < 4; j = j + 1) {
                    String line = br.readLine();
                    int idx = line.indexOf(":");
                    String type = line.substring(0, idx);
                    String text = line.substring(idx + 1);
                    questions[i].addOption(text, type.equals("SOLUTION"));
                }
            }
        } catch (IOException e) {
            System.out.println("IO Problem: Is file OK?");
            System.exit(1);
        }
    }
    
    public void takeQuiz() {
        int score = 0;
        System.out.println("Enjoy the quiz!\n");
        for (int i = 0; i < questions.length; i = i + 1) {
            System.out.println(questions[i]);
            if (questions[i].isCorrect(getUserSelection())) {
                System.out.println("Correct!\n");
                score = score + 1;
            } else {
                System.out.println("Incorrect!\n");
            }
        }
        System.out.println("You scored " + score + " out of " + questions.length);
    }
    
    private int getUserSelection() {
        Scanner sc = new Scanner(System.in);
        int indexOfSelection = -1;
        while (indexOfSelection < 0 || indexOfSelection > 3) {
            System.out.print("Enter your choice: ");
            indexOfSelection = sc.nextInt() - 1;
            if (indexOfSelection < 0 || indexOfSelection > 3) {
                System.out.println("Invalid selection. Try again.");
            }
        }
        return indexOfSelection;
    }
    
}
