You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.8 KiB
97 lines
2.8 KiB
import java.util.*;
|
|
|
|
public class QuizController {
|
|
private QuestionGenerator questionGenerator;
|
|
private List<Question> currentQuiz;
|
|
private int currentQuestionIndex;
|
|
private int score;
|
|
private Map<Integer, Integer> userAnswers;
|
|
|
|
public static class Question {
|
|
private int id;
|
|
private String content;
|
|
private String[] options;
|
|
private int correctAnswer;
|
|
private String difficulty;
|
|
|
|
public Question(int id, String content, String[] options, int correctAnswer, String difficulty) {
|
|
this.id = id;
|
|
this.content = content;
|
|
this.options = options;
|
|
this.correctAnswer = correctAnswer;
|
|
this.difficulty = difficulty;
|
|
}
|
|
|
|
public int getId() { return id; }
|
|
public String getContent() { return content; }
|
|
public String[] getOptions() { return options; }
|
|
public int getCorrectAnswer() { return correctAnswer; }
|
|
public String getDifficulty() { return difficulty; }
|
|
|
|
public boolean checkAnswer(int userAnswer) {
|
|
return userAnswer == correctAnswer;
|
|
}
|
|
}
|
|
|
|
public QuizController() {
|
|
this.questionGenerator = new QuestionGenerator();
|
|
this.userAnswers = new HashMap<>();
|
|
}
|
|
|
|
public void startNewQuiz(String difficulty, int questionCount) {
|
|
this.currentQuiz = questionGenerator.generateQuestionSet(questionCount, difficulty);
|
|
this.currentQuestionIndex = 0;
|
|
this.score = 0;
|
|
this.userAnswers.clear();
|
|
}
|
|
|
|
public Question getCurrentQuestion() {
|
|
if (currentQuiz == null || currentQuestionIndex >= currentQuiz.size()) {
|
|
return null;
|
|
}
|
|
return currentQuiz.get(currentQuestionIndex);
|
|
}
|
|
|
|
public boolean submitAnswer(int answer) {
|
|
if (currentQuiz == null || currentQuestionIndex >= currentQuiz.size()) {
|
|
return false;
|
|
}
|
|
|
|
Question current = currentQuiz.get(currentQuestionIndex);
|
|
userAnswers.put(currentQuestionIndex, answer);
|
|
|
|
if (current.checkAnswer(answer)) {
|
|
score++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean nextQuestion() {
|
|
if (currentQuiz == null || currentQuestionIndex >= currentQuiz.size() - 1) {
|
|
return false;
|
|
}
|
|
currentQuestionIndex++;
|
|
return true;
|
|
}
|
|
|
|
public boolean hasNextQuestion() {
|
|
return currentQuiz != null && currentQuestionIndex < currentQuiz.size() - 1;
|
|
}
|
|
|
|
public int getCurrentQuestionNumber() {
|
|
return currentQuestionIndex + 1;
|
|
}
|
|
|
|
public int getTotalQuestions() {
|
|
return currentQuiz != null ? currentQuiz.size() : 0;
|
|
}
|
|
|
|
public int getScore() {
|
|
return score;
|
|
}
|
|
|
|
public double getPercentage() {
|
|
return currentQuiz != null ? (double) score / currentQuiz.size() * 100 : 0;
|
|
}
|
|
} |