diff options
Diffstat (limited to 'src/Quiz.java')
-rw-r--r-- | src/Quiz.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/Quiz.java b/src/Quiz.java new file mode 100644 index 0000000..a630890 --- /dev/null +++ b/src/Quiz.java @@ -0,0 +1,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 + + '}'; + } +} |