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.
two_project/src/main/java/com/mathgenerator/controller/QuizController.java

146 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.mathgenerator.controller;
import com.mathgenerator.model.ChoiceQuestion;
import com.mathgenerator.model.Level;
import com.mathgenerator.model.User;
import com.mathgenerator.service.PaperService;
import com.mathgenerator.service.strategy.MixedDifficultyStrategy;
import com.mathgenerator.storage.FileManager;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class QuizController {
// --- 后端服务 ---
private final PaperService paperService;
// --- 答题状态 ---
private User currentUser;
private List<ChoiceQuestion> questions;
private List<Integer> userAnswers = new ArrayList<>();
private int currentQuestionIndex = 0;
// --- FXML 控件 ---
@FXML private Label questionNumberLabel;
@FXML private ProgressBar progressBar;
@FXML private Label questionTextLabel;
@FXML private ToggleGroup optionsGroup;
@FXML private RadioButton option1, option2, option3, option4;
@FXML private Button submitButton;
@FXML private Label statusLabel;
/**
* 构造函数,初始化后端服务
*/
public QuizController() {
// 这是依赖注入的一种简化形式,在真实项目中会使用框架管理
FileManager fileManager = new FileManager();
MixedDifficultyStrategy strategy = new MixedDifficultyStrategy();
this.paperService = new PaperService(fileManager, strategy);
}
/**
* 接收从主菜单传递过来的数据,并开始答题
*/
public void initData(User user, Level level, int questionCount) {
this.currentUser = user;
// 调用后端服务生成题目
this.questions = paperService.createPaper(user, questionCount, level);
displayCurrentQuestion();
}
/**
* 显示当前的题目和选项 (已更新增加ABCD前缀)
*/
private void displayCurrentQuestion() {
ChoiceQuestion currentQuestion = questions.get(currentQuestionIndex);
questionNumberLabel.setText(String.format("第 %d / %d 题", currentQuestionIndex + 1, questions.size()));
progressBar.setProgress((double) (currentQuestionIndex + 1) / questions.size());
questionTextLabel.setText(currentQuestion.questionText());
List<RadioButton> radioButtons = List.of(option1, option2, option3, option4);
String[] prefixes = {"A. ", "B. ", "C. ", "D. "}; // 定义选项前缀
for (int i = 0; i < radioButtons.size(); i++) {
// 将前缀和选项文本结合起来
radioButtons.get(i).setText(prefixes[i] + currentQuestion.options().get(i));
}
optionsGroup.selectToggle(null); // 清除上一次的选择
statusLabel.setText(""); // 清除状态提示
if (currentQuestionIndex == questions.size() - 1) {
submitButton.setText("完成答题");
}
}
/**
* 处理提交按钮的点击事件
*/
@FXML
private void handleSubmitButtonAction(ActionEvent event) {
RadioButton selectedRadioButton = (RadioButton) optionsGroup.getSelectedToggle();
if (selectedRadioButton == null) {
statusLabel.setText("请选择一个答案!");
return;
}
// 记录用户答案的索引
List<RadioButton> radioButtons = List.of(option1, option2, option3, option4);
userAnswers.add(radioButtons.indexOf(selectedRadioButton));
// 移动到下一题或结束答题
currentQuestionIndex++;
if (currentQuestionIndex < questions.size()) {
displayCurrentQuestion();
} else {
// 答题结束,计算分数并跳转到分数界面
calculateScoreAndShowResults();
}
}
/**
* 计算分数并准备跳转到结果页面
*/
private void calculateScoreAndShowResults() {
int correctCount = 0;
for (int i = 0; i < questions.size(); i++) {
if (userAnswers.get(i) == questions.get(i).correctOptionIndex()) {
correctCount++;
}
}
double score = (double) correctCount / questions.size() * 100;
// 禁用当前页面的按钮
submitButton.setDisable(true);
statusLabel.setText("答题已完成,正在为您计算分数...");
// 加载分数界面并传递数据
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/mathgenerator/view/ScoreView.fxml"));
Parent root = loader.load();
ScoreController controller = loader.getController();
controller.initData(currentUser, score); // 将用户和分数传递过去
Stage stage = (Stage) submitButton.getScene().getWindow();
stage.setScene(new Scene(root));
stage.setTitle("答题结果");
} catch (IOException e) {
e.printStackTrace();
}
}
}