implemented a part of the test

This commit is contained in:
Jonas Hinterdorfer 2025-05-13 09:44:31 +02:00
parent 6f4d8adf18
commit 7bb22fccd9
6 changed files with 201 additions and 1 deletions

View File

@ -7,8 +7,13 @@ import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import java.util.Collections;
import java.util.List;
import javafx.event.ActionEvent;
import java.io.IOException;
import java.util.Collections;
import java.util.Optional;
public class Utils {
@ -23,6 +28,7 @@ public class Utils {
public static Stage getStageFromActionEven(ActionEvent actionEvent) {
return (Stage) ((javafx.scene.Node) actionEvent.getSource()).getScene().getWindow();
}
public static boolean showConfirmationButton(String content) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
@ -39,4 +45,12 @@ public class Utils {
return result.filter(buttonType -> buttonType == yesButton).isPresent();
}
public static <T> List<T> getRandom(List<T> items, int count) {
// Shuffle the list
Collections.shuffle(items);
// Return the first 'count' items
return items.subList(0, Math.min(count, items.size()));
}
}

View File

@ -4,6 +4,7 @@ import at.ionas999.questioncatalog.Utils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
@ -25,4 +26,10 @@ public class MainController {
Stage stage = Utils.getStageFromActionEven(actionEvent);
Utils.switchScenes("answerText.fxml", stage);
}
@FXML
private void testBtnClick(ActionEvent actionEvent) throws IOException {
Stage stage = Utils.getStageFromActionEven(actionEvent);
Utils.switchScenes("test.fxml", stage);
}
}

View File

@ -0,0 +1,116 @@
package at.ionas999.questioncatalog.controller;
import at.ionas999.questioncatalog.Utils;
import at.ionas999.questioncatalog.model.Question;
import at.ionas999.questioncatalog.model.QuestionCatalog;
import at.ionas999.questioncatalog.services.QuestionCatalogService;
import at.ionas999.questioncatalog.services.QuestionService;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.converter.NumberStringConverter;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
public class TestController {
@FXML
private Button nextQuestionBtn;
@FXML
private Button startBtn;
@FXML
private ComboBox catalogComboBox;
@FXML
private TextField questionCountField;
@FXML
private Slider questionSlider;
@FXML
private Label questionLabel;
@FXML
private TextArea userAnswerArea;
@FXML
private TextArea correctAnswerArea;
@FXML
private Button correctBtn;
@FXML
private Button incorrectBtn;
private BooleanProperty isInCorrectionState = new SimpleBooleanProperty(false);
private BooleanProperty isInConfiguringState = new SimpleBooleanProperty(true);
private ObservableList<QuestionCatalog> catalogs;
private ObservableList<Question> questions;
private ArrayList<String> answers = new ArrayList<>();
private int currentIdx = 0;
@FXML
private void initialize() throws SQLException {
catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
catalogComboBox.setItems(catalogs);
correctAnswerArea.visibleProperty().bind(isInCorrectionState);
correctBtn.visibleProperty().bind(isInCorrectionState);
incorrectBtn.visibleProperty().bind(isInCorrectionState);
userAnswerArea.editableProperty().bind(isInCorrectionState.not());
questionSlider.disableProperty().bind(isInConfiguringState.not());
questionCountField.disableProperty().bind(isInConfiguringState.not());
catalogComboBox.disableProperty().bind(isInConfiguringState.not());
startBtn.textProperty().bind(Bindings.when(isInConfiguringState).then("Start").otherwise("Cancle"));
nextQuestionBtn.disableProperty().bind(isInConfiguringState);
nextQuestionBtn.visibleProperty().bind(isInCorrectionState.not());
Bindings.bindBidirectional(questionCountField.textProperty(), questionSlider.valueProperty(), new NumberStringConverter());
questionSlider.setBlockIncrement(1);
questionSlider.valueProperty().addListener((_, _, newValue) -> {
questionSlider.setValue(newValue.intValue());
});
catalogComboBox.valueProperty().addListener((_,_,newValue) -> {
if(newValue == null)
return;
QuestionCatalog c = (QuestionCatalog) newValue;
try {
this.questionSlider.setMax(QuestionCatalogService.GetAmountOfQuestionsFromCatalog(c.getId()));
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
@FXML
private void onCorrectBtnClick(ActionEvent actionEvent) {
}
@FXML
private void onIncorrectBtnClick(ActionEvent actionEvent) {
}
public void onStartTestBtnClick(ActionEvent actionEvent) throws SQLException {
this.isInConfiguringState.setValue(false);
ArrayList<Question> questions = QuestionService
.GetQuestionsFromCatalog(((QuestionCatalog) catalogComboBox.getValue()).getId());
this.questions = FXCollections.observableList(Utils.getRandom(questions, (int) this.questionSlider.getValue()));
this.questionLabel.setText(this.questions.get(currentIdx).getQuestion());
}
public void showNextQuestion(ActionEvent actionEvent) {
this.answers.add(this.userAnswerArea.getText());
currentIdx++;
if(currentIdx >= this.questions.size())
{
this.isInCorrectionState.setValue(true);
this.questionLabel.setText("Rate your Answers !");
return;
}
this.userAnswerArea.clear();
this.questionLabel.setText(this.questions.get(currentIdx).getQuestion());
}
}

View File

@ -67,6 +67,23 @@ public class QuestionCatalogService {
return true;
}
public static int GetAmountOfQuestionsFromCatalog(int id) throws SQLException {
String stmtString = "SELECT COUNT(*) FROM QUESTION WHERE QUESTIONCATALOGID = ?";
int count = 0;
try (Connection connection = ServiceBase.GetJDBCConnection()) {
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
}
}
return count;
}
public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
String stmtString = "select id, name from QUESTIONCATALOG";

View File

@ -13,5 +13,6 @@
<Button text="Add New" onAction="#addNewBtnClick" prefWidth="150" />
<Button text="View" onAction="#viewBtnClick" prefWidth="149" />
<Button text="Learn" onAction="#learnBtnClick" prefWidth="150" />
<Button text="Test" onAction="#testBtnClick" prefWidth="150" />
</VBox>
</VBox>

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefWidth="600.0" prefHeight="400.0"
fx:controller="at.ionas999.questioncatalog.controller.TestController">
<children>
<VBox spacing="20" alignment="CENTER" AnchorPane.topAnchor="10.0" AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0">
<!-- Start Test Button -->
<Button text="Start Test" fx:id="startBtn" onAction="#onStartTestBtnClick"/>
<!-- Slider and Input Box -->
<HBox spacing="10" alignment="CENTER">
<Label text="Number of Questions:"/>
<TextField fx:id="questionCountField" promptText="Enter amount"/>
<Slider fx:id="questionSlider" min="1" max="10" showTickLabels="true" showTickMarks="true"/>
<ComboBox fx:id="catalogComboBox" promptText="Choose a catalog" />
</HBox>
<!-- Question Display -->
<Label fx:id="questionLabel" text="Question will appear here" style="-fx-font-size: 18;"/>
<!-- User Input Box -->
<TextArea fx:id="userAnswerArea" promptText="Enter your answer here..." wrapText="true" prefHeight="100.0"
prefWidth="300.0"/>
<Button text="Next Question" fx:id="nextQuestionBtn" onAction="#showNextQuestion"/>
<!-- Correct Answer Display -->
<VBox spacing="10" alignment="CENTER">
<Label text="Correct Answer:" style="-fx-font-size: 14;"/>
<TextArea fx:id="correctAnswerArea" editable="false" wrapText="true" prefHeight="100.0"
prefWidth="300.0"/>
</VBox>
<!-- Correct and Incorrect Buttons -->
<HBox spacing="10" alignment="CENTER">
<Button text="Correct" fx:id="correctBtn" onAction="#onCorrectBtnClick"/>
<Button text="Incorrect" fx:id="incorrectBtn" onAction="#onIncorrectBtnClick"/>
</HBox>
</VBox>
</children>
</AnchorPane>