|
|
package com.student.mathquiz.service;
|
|
|
|
|
|
import com.student.mathquiz.constant.UserType;
|
|
|
import com.student.mathquiz.model.Question;
|
|
|
import com.student.mathquiz.persistence.FileSaver;
|
|
|
import com.student.mathquiz.persistence.QuestionChecker;
|
|
|
import com.student.mathquiz.question.QuestionGenerator;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
|
public class QuizServiceImpl implements IQuizService {
|
|
|
|
|
|
private List<com.student.mathquiz.question.Question> generatedQuestions;
|
|
|
private List<Question> quizQuestions;
|
|
|
private List<Integer> userAnswers;
|
|
|
private int currentQuestionIndex;
|
|
|
|
|
|
// 在 QuizServiceImpl.java 中
|
|
|
|
|
|
@Override
|
|
|
public void generateQuiz(String level, int count, String userEmail) {
|
|
|
System.out.println(" --> 【QuizService】: 进入 generateQuiz 方法。");
|
|
|
UserType userType = UserType.fromDisplayName(level);
|
|
|
if (userType == null) {
|
|
|
System.err.println(" --> 【QuizService】: 错误!无效的难度级别: " + level);
|
|
|
throw new IllegalArgumentException("无效的难度级别: " + level);
|
|
|
}
|
|
|
|
|
|
QuestionGenerator generator = QuestionGenerator.getGeneratorByUserType(userType);
|
|
|
|
|
|
Set<String> questionContentSet = new HashSet<>();
|
|
|
generatedQuestions = new ArrayList<>();
|
|
|
|
|
|
// 我们把 maxTries 提到外面,方便观察
|
|
|
int maxTries = count * 20;
|
|
|
int currentTry = 0;
|
|
|
|
|
|
System.out.println(" --> 【QuizService】: 准备进入循环生成题目,最大尝试次数: " + maxTries);
|
|
|
|
|
|
while (generatedQuestions.size() < count && currentTry < maxTries) {
|
|
|
currentTry++; // 每次循环都计数
|
|
|
|
|
|
// 每循环20次打印一次日志,防止刷屏,又能看到进度
|
|
|
if (currentTry % 20 == 0) {
|
|
|
System.out.println(" --> 【QuizService】: 循环中... 已尝试 " + currentTry + " 次, 已生成 " + generatedQuestions.size() + " 道题。");
|
|
|
}
|
|
|
|
|
|
com.student.mathquiz.question.Question newQuestion = generator.generateQuestion();//
|
|
|
String content = newQuestion.getContent();
|
|
|
|
|
|
if (!questionContentSet.contains(content) && !QuestionChecker.isDuplicate(userEmail, content)) {
|
|
|
questionContentSet.add(content);
|
|
|
generatedQuestions.add(newQuestion);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
System.out.println(" --> 【QuizService】: 循环结束!最终尝试 " + currentTry + " 次, 成功生成 " + generatedQuestions.size() + " 道题。");
|
|
|
|
|
|
if (generatedQuestions.size() < count) {
|
|
|
System.err.println(" --> 【QuizService】: 警告: 题库不足或重复率过高,只生成了 " + generatedQuestions.size() + " 道题。");
|
|
|
}
|
|
|
|
|
|
// ... 后面的代码不变 ...
|
|
|
this.quizQuestions = new ArrayList<>();
|
|
|
for (com.student.mathquiz.question.Question q : generatedQuestions) {
|
|
|
this.quizQuestions.add(new Question(q.getContent(), q.getOptions(), q.getCorrectAnswerIndex()));
|
|
|
}
|
|
|
|
|
|
FileSaver.saveQuestions(userEmail, generatedQuestions);
|
|
|
this.userAnswers = new ArrayList<>();
|
|
|
this.currentQuestionIndex = 0;
|
|
|
System.out.println(" --> 【QuizService】: generateQuiz 方法执行完毕!");
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Question getCurrentQuestion() {
|
|
|
if (quizQuestions == null || currentQuestionIndex >= quizQuestions.size()) return null;
|
|
|
return quizQuestions.get(currentQuestionIndex);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isLastQuestion() {
|
|
|
return currentQuestionIndex == quizQuestions.size() - 1;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void submitAnswer(int selectedOptionIndex) {
|
|
|
userAnswers.add(selectedOptionIndex);
|
|
|
if (!isLastQuestion()) currentQuestionIndex++;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int calculateScore() {
|
|
|
int correctCount = 0;
|
|
|
for (int i = 0; i < quizQuestions.size(); i++) {
|
|
|
if (i < userAnswers.size() && quizQuestions.get(i).getCorrectAnswerIndex() == userAnswers.get(i)) {
|
|
|
correctCount++;
|
|
|
}
|
|
|
}
|
|
|
if (quizQuestions.isEmpty()) return 0;
|
|
|
return (int) ((double) correctCount / quizQuestions.size() * 100);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getTotalQuestionCount() { return quizQuestions != null ? quizQuestions.size() : 0; }
|
|
|
@Override
|
|
|
public int getCurrentQuestionIndex() { return currentQuestionIndex; }
|
|
|
}
|