package com.mathlearning.controller; import com.mathlearning.model.Question; import com.mathlearning.model.ConcreteQuestionGenerator; import com.mathlearning.model.QuestionGenerator; import java.util.List; public class QuestionController { private QuestionGenerator questionGenerator; private List currentQuestions; private int currentQuestionIndex; private int score; public QuestionController() { this.questionGenerator = new ConcreteQuestionGenerator(); this.currentQuestionIndex = 0; this.score = 0; } // 原有的生成问题方法 public List generateQuestions(String level, int count) { return questionGenerator.generateQuestions(level, count); } // 原有的显示问题方法 public void displayQuestions(String level, int count) { List questions = generateQuestions(level, count); System.out.println(level + "题目:"); for (int i = 0; i < questions.size(); i++) { Question q = questions.get(i); System.out.println((i + 1) + ". " + q.getQuestionText()); String[] options = q.getOptions(); for (int j = 0; j < options.length; j++) { System.out.println(" " + (char) ('A' + j) + ". " + options[j]); } System.out.println(" 正确答案: " + (char) ('A' + q.getCorrectIndex())); System.out.println(); } } // 新增加的测验管理方法 // 开始新测验 public void startNewQuiz(String level, int questionCount) { this.currentQuestions = questionGenerator.generateQuestions(level, questionCount); this.currentQuestionIndex = 0; this.score = 0; } // 获取当前问题列表 - 新增的方法 public List getCurrentQuestions() { return currentQuestions; } // 获取当前问题 public Question getCurrentQuestion() { if (currentQuestions == null || currentQuestions.isEmpty()) { return null; } return currentQuestions.get(currentQuestionIndex); } // 获取当前问题索引 public int getCurrentQuestionIndex() { return currentQuestionIndex; } // 获取问题总数 public int getTotalQuestions() { return currentQuestions != null ? currentQuestions.size() : 0; } // 提交答案并检查是否正确 public boolean submitAnswer(int selectedOption) { if (currentQuestions == null || currentQuestionIndex >= currentQuestions.size()) { return false; } Question currentQuestion = currentQuestions.get(currentQuestionIndex); boolean isCorrect = (selectedOption == currentQuestion.getCorrectIndex()); if (isCorrect) { score++; } return isCorrect; } // 移动到下一个问题 public boolean nextQuestion() { if (currentQuestions == null || currentQuestionIndex >= currentQuestions.size() - 1) { return false; } currentQuestionIndex++; return true; } // 移动到上一个问题 public boolean previousQuestion() { if (currentQuestions == null || currentQuestionIndex <= 0) { return false; } currentQuestionIndex--; return true; } // 获取当前分数 public int getScore() { return score; } // 检查是否还有更多问题 public boolean hasMoreQuestions() { return currentQuestions != null && currentQuestionIndex < currentQuestions.size() - 1; } // 检查测验是否完成 public boolean isQuizCompleted() { return currentQuestions != null && currentQuestionIndex >= currentQuestions.size() - 1; } // 获取所有问题(用于显示结果等) public List getAllQuestions() { return currentQuestions; } // 重置测验状态 public void resetQuiz() { this.currentQuestions = null; this.currentQuestionIndex = 0; this.score = 0; } // 直接设置当前问题索引(如果需要) public void setCurrentQuestionIndex(int index) { if (currentQuestions != null && index >= 0 && index < currentQuestions.size()) { this.currentQuestionIndex = index; } } }