From 7bb22fccd987c77fa4b4d5c7107eab93f82e5aa0 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Tue, 13 May 2025 09:44:31 +0200 Subject: [PATCH] implemented a part of the test --- .../at/ionas999/questioncatalog/Utils.java | 16 ++- .../controller/MainController.java | 7 ++ .../controller/TestController.java | 116 ++++++++++++++++++ .../services/QuestionCatalogService.java | 17 +++ .../at/ionas999/questioncatalog/main.fxml | 1 + .../at/ionas999/questioncatalog/test.fxml | 45 +++++++ 6 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/main/java/at/ionas999/questioncatalog/controller/TestController.java create mode 100644 src/main/resources/at/ionas999/questioncatalog/test.fxml diff --git a/src/main/java/at/ionas999/questioncatalog/Utils.java b/src/main/java/at/ionas999/questioncatalog/Utils.java index 42d3b03..8a79a6a 100644 --- a/src/main/java/at/ionas999/questioncatalog/Utils.java +++ b/src/main/java/at/ionas999/questioncatalog/Utils.java @@ -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 { @@ -20,9 +25,10 @@ public class Utils { stage.show(); } - public static Stage getStageFromActionEven(ActionEvent actionEvent){ + 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 List getRandom(List items, int count) { + // Shuffle the list + Collections.shuffle(items); + // Return the first 'count' items + return items.subList(0, Math.min(count, items.size())); + } } diff --git a/src/main/java/at/ionas999/questioncatalog/controller/MainController.java b/src/main/java/at/ionas999/questioncatalog/controller/MainController.java index 455fc4d..045a5bf 100644 --- a/src/main/java/at/ionas999/questioncatalog/controller/MainController.java +++ b/src/main/java/at/ionas999/questioncatalog/controller/MainController.java @@ -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); + } } diff --git a/src/main/java/at/ionas999/questioncatalog/controller/TestController.java b/src/main/java/at/ionas999/questioncatalog/controller/TestController.java new file mode 100644 index 0000000..5628117 --- /dev/null +++ b/src/main/java/at/ionas999/questioncatalog/controller/TestController.java @@ -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 catalogs; + private ObservableList questions; + private ArrayList 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 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()); + } +} diff --git a/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java b/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java index 3cd46d8..4d0b00c 100644 --- a/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java +++ b/src/main/java/at/ionas999/questioncatalog/services/QuestionCatalogService.java @@ -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 GetCatalogsWithoutQuestions() throws SQLException { String stmtString = "select id, name from QUESTIONCATALOG"; diff --git a/src/main/resources/at/ionas999/questioncatalog/main.fxml b/src/main/resources/at/ionas999/questioncatalog/main.fxml index d2eea2d..1487b55 100644 --- a/src/main/resources/at/ionas999/questioncatalog/main.fxml +++ b/src/main/resources/at/ionas999/questioncatalog/main.fxml @@ -13,5 +13,6 @@