implemented logic with correct and incorrect
This commit is contained in:
parent
43c8792d70
commit
02f583f074
@ -13,16 +13,41 @@ import java.io.IOException;
|
||||
|
||||
public class App extends Application {
|
||||
// Der gesamte fxml code wurde mithilfe des Github Copilotes generiert
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("main.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
stage.setTitle("Question Catalog");
|
||||
stage.setScene(scene);
|
||||
stage.setMaximized(true);
|
||||
stage.show();
|
||||
}
|
||||
// die logic mit correct und incorrect wurde auch mithilfe des copilotes generiert
|
||||
|
||||
/* prompt
|
||||
|
||||
|
||||
Please implement the following features:
|
||||
|
||||
1. **Tracking Question Statistics:**
|
||||
|
||||
* Extend the `Question` entity in the database to include two new properties: `correctCount` and `incorrectCount`.
|
||||
* Ensure these properties are initialized in the `CREATE TABLE` or equivalent database creation statements.
|
||||
|
||||
2. **Updating Statistics:**
|
||||
|
||||
* When a user clicks the "Correct" or "Incorrect" button in the answer interface, increment the respective property (`correctCount` or `incorrectCount`) for that question in the database.
|
||||
|
||||
3. **Sorting Questions:**
|
||||
|
||||
* Modify the logic for displaying questions so that:
|
||||
|
||||
* Questions with more **incorrect** answers are shown **earlier**.
|
||||
* Among questions with equal incorrect counts, **newer** questions should appear **before** older ones.
|
||||
* Questions that are answered correctly most of the time should be shown **later**.
|
||||
|
||||
*/
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("main.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
stage.setTitle("Question Catalog");
|
||||
stage.setScene(scene);
|
||||
stage.setMaximized(true);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
|
||||
@ -14,6 +14,7 @@ import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class AnswerTextController {
|
||||
@ -44,6 +45,31 @@ public class AnswerTextController {
|
||||
showAnswerBtn.setDisable(false);
|
||||
try {
|
||||
questions = QuestionService.GetQuestionsFromCatalog(catalog.getId());
|
||||
|
||||
// Sort questions based on the criteria:
|
||||
// 1. Questions with more incorrect answers come first
|
||||
// 2. For equal incorrect counts, newer questions (fewer total answers) come first
|
||||
// 3. Questions answered correctly most of the time come last
|
||||
questions.sort((q1, q2) -> {
|
||||
// First priority: incorrect count (higher comes first)
|
||||
if (q1.getIncorrectCount() != q2.getIncorrectCount()) {
|
||||
return Integer.compare(q2.getIncorrectCount(), q1.getIncorrectCount());
|
||||
}
|
||||
|
||||
// Second priority: total answer count (lower comes first, meaning newer questions first)
|
||||
int totalAnswers1 = q1.getCorrectCount() + q1.getIncorrectCount();
|
||||
int totalAnswers2 = q2.getCorrectCount() + q2.getIncorrectCount();
|
||||
if (totalAnswers1 != totalAnswers2) {
|
||||
return Integer.compare(totalAnswers1, totalAnswers2);
|
||||
}
|
||||
|
||||
// Third priority: correct rate (lower comes first)
|
||||
double correctRate1 = totalAnswers1 == 0 ? 0 : (double) q1.getCorrectCount() / totalAnswers1;
|
||||
double correctRate2 = totalAnswers2 == 0 ? 0 : (double) q2.getCorrectCount() / totalAnswers2;
|
||||
return Double.compare(correctRate1, correctRate2);
|
||||
});
|
||||
|
||||
currentIdx = -1; // Reset index to start from beginning
|
||||
showNextQuestion();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -51,6 +77,12 @@ public class AnswerTextController {
|
||||
}
|
||||
|
||||
private void showNextQuestion() {
|
||||
if (questions == null || questions.isEmpty()) {
|
||||
questionLabel.setText("No questions available");
|
||||
resetUI();
|
||||
return;
|
||||
}
|
||||
|
||||
currentIdx = (currentIdx + 1) % questions.size();
|
||||
currentQuestion = questions.get(currentIdx);
|
||||
questionLabel.setText(currentQuestion.getQuestion());
|
||||
@ -80,12 +112,26 @@ public class AnswerTextController {
|
||||
|
||||
@FXML
|
||||
private void onCorrectBtnClick() {
|
||||
showNextQuestion();
|
||||
try {
|
||||
currentQuestion.incrementCorrectCount();
|
||||
QuestionService.UpdateQuestion(currentQuestion);
|
||||
showNextQuestion();
|
||||
} catch (SQLException e) {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to update question statistics: " + e.getMessage());
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void onIncorrectBtnClick() {
|
||||
showNextQuestion();
|
||||
try {
|
||||
currentQuestion.incrementIncorrectCount();
|
||||
QuestionService.UpdateQuestion(currentQuestion);
|
||||
showNextQuestion();
|
||||
} catch (SQLException e) {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to update question statistics: " + e.getMessage());
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
@ -93,4 +139,4 @@ public class AnswerTextController {
|
||||
Stage stage = Utils.getStageFromActionEven(actionEvent);
|
||||
Utils.switchScenes("main.fxml", stage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,12 +8,25 @@ public class Question {
|
||||
private StringProperty answer = new SimpleStringProperty();
|
||||
private StringProperty question = new SimpleStringProperty();
|
||||
private int questionCatalogId;
|
||||
private int correctCount;
|
||||
private int incorrectCount;
|
||||
|
||||
public Question(int id,String question, String answer, int questionCatalogId) {
|
||||
public Question(int id, String question, String answer, int questionCatalogId) {
|
||||
this.id = id;
|
||||
this.question.set(question);
|
||||
this.answer.set(answer);
|
||||
this.questionCatalogId = questionCatalogId;
|
||||
this.correctCount = 0;
|
||||
this.incorrectCount = 0;
|
||||
}
|
||||
|
||||
public Question(int id, String question, String answer, int questionCatalogId, int correctCount, int incorrectCount) {
|
||||
this.id = id;
|
||||
this.question.set(question);
|
||||
this.answer.set(answer);
|
||||
this.questionCatalogId = questionCatalogId;
|
||||
this.correctCount = correctCount;
|
||||
this.incorrectCount = incorrectCount;
|
||||
}
|
||||
|
||||
public int getQuestionCatalogId()
|
||||
@ -49,10 +62,35 @@ public class Question {
|
||||
this.questionCatalogId = questionCatalogId;
|
||||
}
|
||||
|
||||
public int getCorrectCount() {
|
||||
return correctCount;
|
||||
}
|
||||
|
||||
public void setCorrectCount(int correctCount) {
|
||||
this.correctCount = correctCount;
|
||||
}
|
||||
|
||||
public int getIncorrectCount() {
|
||||
return incorrectCount;
|
||||
}
|
||||
|
||||
public void setIncorrectCount(int incorrectCount) {
|
||||
this.incorrectCount = incorrectCount;
|
||||
}
|
||||
|
||||
public void incrementCorrectCount() {
|
||||
this.correctCount++;
|
||||
}
|
||||
|
||||
public void incrementIncorrectCount() {
|
||||
this.incorrectCount++;
|
||||
}
|
||||
|
||||
public int getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return question.get();
|
||||
|
||||
@ -10,12 +10,14 @@ public class QuestionService {
|
||||
public static boolean AddQuestionToDb(Question question)
|
||||
{
|
||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||
String insertStmt = "insert into QUESTION (QUESTIONCATALOGID, QUESTION, ANSWER) VALUES (?, ?, ?)";
|
||||
String insertStmt = "insert into QUESTION (QUESTIONCATALOGID, QUESTION, ANSWER, CORRECT_COUNT, INCORRECT_COUNT) VALUES (?, ?, ?, ?, ?)";
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
|
||||
statement.setInt(1, question.getQuestionCatalogId());
|
||||
statement.setString(2, question.getQuestion());
|
||||
statement.setString(3, question.getAnswer());
|
||||
statement.setInt(4, question.getCorrectCount());
|
||||
statement.setInt(5, question.getIncorrectCount());
|
||||
statement.execute();
|
||||
}
|
||||
}
|
||||
@ -27,7 +29,7 @@ public class QuestionService {
|
||||
}
|
||||
|
||||
public static ArrayList<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
|
||||
String stmtString = "select id, questionCatalogId, question, answer from Question where questionCatalogId = ?";
|
||||
String stmtString = "select id, questionCatalogId, question, answer, CORRECT_COUNT, INCORRECT_COUNT from Question where questionCatalogId = ?";
|
||||
|
||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||
|
||||
@ -37,11 +39,24 @@ public class QuestionService {
|
||||
ArrayList<Question> questions = new ArrayList<Question>();
|
||||
|
||||
while (rs.next()){
|
||||
int correctCount = 0;
|
||||
int incorrectCount = 0;
|
||||
|
||||
// Handle case where columns might not exist in older DB versions
|
||||
try {
|
||||
correctCount = rs.getInt(5);
|
||||
incorrectCount = rs.getInt(6);
|
||||
} catch (SQLException e) {
|
||||
// Use default values if columns don't exist
|
||||
}
|
||||
|
||||
Question question = new Question(
|
||||
rs.getInt(1),
|
||||
rs.getString(3),
|
||||
rs.getString(4),
|
||||
rs.getInt(2)
|
||||
rs.getInt(2),
|
||||
correctCount,
|
||||
incorrectCount
|
||||
);
|
||||
questions.add(question);
|
||||
}
|
||||
@ -66,23 +81,26 @@ public class QuestionService {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void UpdateQuestion(Question question) throws SQLException {
|
||||
try(Connection connection = ServiceBase.GetJDBCConnection()){
|
||||
String insertStmt = "update QUESTION set " +
|
||||
"QUESTION = ?," +
|
||||
"ANSWER=?," +
|
||||
"QUESTIONCATALOGID=?" +
|
||||
"ANSWER = ?," +
|
||||
"QUESTIONCATALOGID = ?," +
|
||||
"CORRECT_COUNT = ?," +
|
||||
"INCORRECT_COUNT = ? " +
|
||||
"where ID = ?";
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
|
||||
statement.setInt(3, question.getQuestionCatalogId());
|
||||
statement.setInt(4, question.getId());
|
||||
statement.setString(1, question.getQuestion());
|
||||
statement.setString(2, question.getAnswer());
|
||||
statement.setInt(3, question.getQuestionCatalogId());
|
||||
statement.setInt(4, question.getCorrectCount());
|
||||
statement.setInt(5, question.getIncorrectCount());
|
||||
statement.setInt(6, question.getId());
|
||||
statement.execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user