我有一个程序,允许用户参加考试。考试将定时进行。时间轴库将在initializes类中使用,以便在用户在此处输入登录信息时启动。我想知道如何用我的伴侣在下面创建的endtest()函数结束测试。他将事件作为参数。我尝试使用null,但是没有用。我是否只需要创建一个事件并提供虚假数据?即使数据是伪造的也行吗? Link to the file iteself
public class QuestionsController implements Initializable {
QuestionsDataFactory test = new JSONQuestionDataFactory();
List<QuestionsData> data = test.getQuestionsData();
List<String> possibleAnswers;
int count = 0;
ToggleGroup group = new ToggleGroup();
Toggle selectedAnswer[] = new Toggle[data.size()];
private Integer timeSeconds = 18;
@FXML
private Label timerLabel;
@FXML
private Label savedAnswer;
@FXML
private Button logOut;
@FXML
private Button nextBtn;
@FXML
private Button prevBtn;
@FXML
private Label questionText;
@FXML
private RadioButton answerOne;
@FXML
private RadioButton answerTwo;
@FXML
private RadioButton answerThree;
@FXML
private RadioButton answerFour;
@FXML
private void loadLogin(ActionEvent event) throws IOException {
Parent QuestionPane = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene tableViewScene = new Scene(QuestionPane);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
private void loadNextQuestion(ActionEvent event) throws IOException {
if (count < data.size() - 1) {
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
group.getSelectedToggle().setSelected(false);
}
if (selectedAnswer[count + 1] != null) {
group.selectToggle(selectedAnswer[count + 1]);
}
}
if (count == data.size() - 1) {
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
} else {
count++;
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
}
@FXML
private void loadPreviousQuestion(ActionEvent event) throws IOException {
if (count > 0) {
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
group.getSelectedToggle().setSelected(false);
}
if (selectedAnswer[count - 1] != null) {
group.selectToggle(selectedAnswer[count - 1]);
}
}
if (count == 0) {
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
} else {
count--;
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
}
}
@FXML
private void endTest(ActionEvent event) throws IOException {
ArrayList<User> users = User.deserializeUsers();
int[] answers = new int[data.size()];
double correct = 0;
int userID = FXMLDocumentController.currentUserID;
NumberFormat formatter = new DecimalFormat("#0.00");
User currentUser = users.get(userID);
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
}
for (int i = 0; i < data.size(); i++) {
if (selectedAnswer[i] != null) {
if (selectedAnswer[i].getUserData().equals(0)) {
answers[i] = 0;
}
if (selectedAnswer[i].getUserData().equals(1)) {
answers[i] = 1;
}
if (selectedAnswer[i].getUserData().equals(2)) {
answers[i] = 2;
}
if (selectedAnswer[i].getUserData().equals(3)) {
answers[i] = 3;
}
} else {
answers[i] = 4;
}
if (answers[i] == data.get(i).getCorrectAnswerIdx()) {
correct++;
}
}
double score = correct / (double) data.size() * 100;
currentUser.setScores(score);
currentUser.setAnswers(answers);
currentUser.setTests(data);
User.serializeUsers(users);
System.out.println("Number of Tests taken: " + currentUser.getNumTests());
for (int i = 0; i < currentUser.getNumTests(); i++) {
System.out.println("Score for test " + (i + 1) + ": " + formatter.format(currentUser.getScores(i)) + "%");
}
Parent QuestionPane = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene tableViewScene = new Scene(QuestionPane);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
private void saveAnswer(ActionEvent event) throws IOException {
if (group.getSelectedToggle() != null) {
selectedAnswer[count] = group.getSelectedToggle();
savedAnswer.setVisible(true);
PauseTransition pause = new PauseTransition();
pause.setDuration(Duration.seconds(1.4));
pause.setOnFinished(e -> {
savedAnswer.setVisible(false);
});
pause.play();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
answerOne.setUserData(0);
answerTwo.setUserData(1);
answerThree.setUserData(2);
answerFour.setUserData(3);
answerOne.setToggleGroup(group);
answerTwo.setToggleGroup(group);
answerThree.setToggleGroup(group);
answerFour.setToggleGroup(group);
questionText.setText(data.get(count).getQuestionText());
possibleAnswers = data.get(count).getPossibleAnswers();
answerOne.setText(possibleAnswers.get(0));
answerTwo.setText(possibleAnswers.get(1));
answerThree.setText(possibleAnswers.get(2));
answerFour.setText(possibleAnswers.get(3));
// update timerLabel
timerLabel.setText(timeSeconds.toString());
Timeline timer = new Timeline();
timer.setCycleCount(Timeline.INDEFINITE);
timer.getKeyFrames().add(
new KeyFrame(Duration.seconds(1),
new EventHandler() {
// KeyFrame event handler
@Override
public void handle(Event event) {
timeSeconds--;
// update timerLabel
timerLabel.setText(timeSeconds.toString());
if (timeSeconds <= 0) {
timer.stop();
try {
endTest(null);
} catch (IOException ex) {
Logger.getLogger(QuestionsController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}));
timer.playFromStart();
}
private void Timer() {
}
}