summaryrefslogtreecommitdiff
path: root/src/Quiz.java
blob: a6308909e8d172595ce917dcd5ae937602a82cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.io.*;
import java.util.*;

public class Quiz {
    private String name;
    private final ArrayList<Question> questions = new ArrayList<>();

    public Quiz() {
    }

    public Quiz(String name) {
        this.name = name;
    }

    public static Quiz loadFromFile(String filePath) throws FileNotFoundException, InvalidQuizFormatException {
        File file = new File(filePath);
        Quiz quiz = new Quiz(file.getName().split(".txt")[0]);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        try {
            while (bufferedReader.ready()) {
                ArrayList<String> lines = new ArrayList<>();

                while (true) {
                    String line = bufferedReader.readLine();

                    if (line != null && !line.equals("")) {
                        lines.add(line);
                    } else {
                        break;
                    }
                }

                if (lines.size() == 2) {
                    quiz.addQuestion(new FillIn(lines.get(0), lines.get(1)));
                } else if (lines.size() > 2) {
                    quiz.addQuestion(new Test(lines.get(0), lines.get(1), lines.subList(1, lines.size()).toArray(new String[0])));
                } else {
                    throw new InvalidQuizFormatException("No line found");
                }
            }
        } catch (IOException ignored) {
        }

        return quiz;
    }

    public void start() {
        Scanner scanner = new Scanner(System.in);
        int correctAnswers = 0;

        Collections.shuffle(questions);
        System.out.printf("""
                =======================================================
                
                              WELCOME TO "%s" QUIZ!
                
                -------------------------------------------------------
                """, name);
        for (int i = 0; i < questions.size(); i++) {
            Question question = questions.get(i);

            if (question instanceof Test) {
                String choice;

                ((Test) question).shuffleOptions();
                System.out.printf("""
                    %d. %s
                    ----------------------------
                    """, i + 1, question);
                System.out.print("Enter the correct choice: ");
                choice = scanner.next();
                while (choice.length() != 1 || choice.charAt(0) < 65 || choice.charAt(0) > ((Test) question).getNumOfOptions() + 64) {
                    System.out.print("Invalid choice! Try again (Ex: A, B, ...): ");
                    choice = scanner.next();
                }
                if (((Test) question).isCorrectAnswer(choice.charAt(0))) {
                    System.out.println("Correct!");
                    correctAnswers++;
                } else {
                    System.out.println("Incorrect!");
                }
            } else if (question instanceof FillIn) {
                System.out.printf("""
                    %d. %s
                    ----------------------------
                    """, i + 1, question);
                System.out.print("Type your answer: ");
                if (scanner.next().equalsIgnoreCase(question.getAnswer())) {
                    System.out.println("Correct!");
                    correctAnswers++;
                } else {
                    System.out.println("Incorrect!");
                }
            }
            System.out.println("-------------------------------------------------------");
        }
        System.out.printf("Correct Answers: %d/%d (%.1f%%)", correctAnswers, questions.size(), (double) correctAnswers / questions.size() * 100);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addQuestion(Question question) {
        questions.add(question);
    }

    @Override
    public String toString() {
        return "Quiz{" +
                "name='" + name + '\'' +
                ", questions=" + questions +
                '}';
    }
}