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.
partner_program/src/main/java/com/example/myapp/controller/ExamController.java

161 lines
5.0 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.example.myapp.controller;
import com.example.myapp.Main;
import com.example.myapp.model.Exam;
import com.example.myapp.model.Question;
import com.example.myapp.service.ExamService;
import javafx.fxml.FXML;
import javafx.scene.control.*;
public class ExamController {
@FXML private Label questionNumberLabel;
@FXML private Label questionTextLabel;
@FXML private RadioButton optionARadio;
@FXML private RadioButton optionBRadio;
@FXML private RadioButton optionCRadio;
@FXML private RadioButton optionDRadio;
@FXML private Label optionALabel;
@FXML private Label optionBLabel;
@FXML private Label optionCLabel;
@FXML private Label optionDLabel;
@FXML private Button previousBtn;
@FXML private Button nextBtn;
@FXML private Button submitBtn;
@FXML private ProgressIndicator progressIndicator;
private String email;
private ExamService examService = ExamService.getInstance();
private int currentQuestionIndex = 0;
private ToggleGroup optionsToggleGroup;
// 添加初始化方法,使用 @FXML 注解确保在FXML加载后立即执行
@FXML
public void initialize() {
// 在这里初始化 ToggleGroup确保在FXML加载完成后立即执行
this.optionsToggleGroup = new ToggleGroup();
optionARadio.setToggleGroup(optionsToggleGroup);
optionBRadio.setToggleGroup(optionsToggleGroup);
optionCRadio.setToggleGroup(optionsToggleGroup);
optionDRadio.setToggleGroup(optionsToggleGroup);
// 设置默认禁用状态
previousBtn.setDisable(true);
submitBtn.setDisable(true);
}
public void initExam(String email) {
this.email = email;
// 确保有考试数据
Exam exam = examService.getCurrentExam(email);
if (exam == null || exam.getQuestions().isEmpty()) {
showAlert("错误", "没有找到考试数据,请重新选择学段");
try {
Main.showGradeSelection(email);
} catch (Exception e) {
e.printStackTrace();
}
return;
}
loadQuestion(0);
}
@FXML
public void onPreviousQuestion() {
saveCurrentAnswer();
if (currentQuestionIndex > 0) {
loadQuestion(currentQuestionIndex - 1);
}
}
@FXML
public void onNextQuestion() {
saveCurrentAnswer();
Exam exam = examService.getCurrentExam(email);
if (currentQuestionIndex < exam.getQuestions().size() - 1) {
loadQuestion(currentQuestionIndex + 1);
}
}
@FXML
public void onSubmitExam() {
saveCurrentAnswer();
// 计算分数
int score = examService.calculateScore(email);
// 显示分数界面
try {
Main.showScorePage(email, score);
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadQuestion(int index) {
Exam exam = examService.getCurrentExam(email);
if (exam == null || index < 0 || index >= exam.getQuestions().size()) {
return;
}
currentQuestionIndex = index;
Question question = exam.getQuestions().get(index);
// 更新界面
questionNumberLabel.setText("第 " + (index + 1) + " 题 / 共 " + exam.getQuestions().size() + " 题");
questionTextLabel.setText(question.getQuestionText());
optionALabel.setText("A. " + question.getOptionA());
optionBLabel.setText("B. " + question.getOptionB());
optionCLabel.setText("C. " + question.getOptionC());
optionDLabel.setText("D. " + question.getOptionD());
// 清除选择
optionsToggleGroup.selectToggle(null);
// 恢复用户之前的选择
if (question.getUserAnswer() != null) {
switch (question.getUserAnswer()) {
case "A": optionARadio.setSelected(true); break;
case "B": optionBRadio.setSelected(true); break;
case "C": optionCRadio.setSelected(true); break;
case "D": optionDRadio.setSelected(true); break;
}
}
// 更新按钮状态
previousBtn.setDisable(index == 0);
nextBtn.setDisable(index == exam.getQuestions().size() - 1);
submitBtn.setDisable(index != exam.getQuestions().size() - 1);
// 更新进度
progressIndicator.setProgress((double) (index + 1) / exam.getQuestions().size());
}
private void saveCurrentAnswer() {
// 添加空值检查
if (optionsToggleGroup == null) {
return;
}
RadioButton selectedRadio = (RadioButton) optionsToggleGroup.getSelectedToggle();
if (selectedRadio != null) {
String answer = "";
if (selectedRadio == optionARadio) answer = "A";
else if (selectedRadio == optionBRadio) answer = "B";
else if (selectedRadio == optionCRadio) answer = "C";
else if (selectedRadio == optionDRadio) answer = "D";
examService.submitAnswer(email, currentQuestionIndex, answer);
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}