show solutions

This commit is contained in:
SowinskiBraeden committed 2025-12-01 14:50:45 -08:00
1 parent 8363f5c1dc
commit 19f788de64
2 files changed
+83 -24

No files matched your search

@@ -84,6 +84,20 @@ public class Question
return this.answers; return this.answers;
} }
public String getAnswersString()
{
final StringBuilder sb;
sb = new StringBuilder();
for (final String answer : this.answers)
{
sb.append(answer);
sb.append(", ");
}
return sb.toString();
}
/** /**
* Checks whether a user's answer is correct. * Checks whether a user's answer is correct.
* Matching is case-insensitive. * Matching is case-insensitive.
+69 -24
View File
@@ -8,6 +8,7 @@ import javafx.scene.layout.VBox;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.util.Duration; import javafx.util.Duration;
@@ -35,21 +36,23 @@ import java.util.List;
*/ */
public class Quiz extends Application public class Quiz extends Application
{ {
private static final int WIDTH = 800; private static final int WIDTH_PIXELS = 800;
private static final int HEIGHT = 600; private static final int HEIGHT_PIXELS = 600;
private static final int QUESTION_SPLIT = 0; private static final int QUESTION_SPLIT_INDEX = 0;
private static final int COUNTER_STARTER = 0; private static final int COUNTER_STARTER = 0;
private static final int ANSWER_SPLIT = 1; private static final int ANSWER_SPLIT_INDEX = 1;
private static final int ONE_SECOND = 1; private static final int SECOND = 1;
private static final int MIN_LENGTH = 2; private static final int MIN_CHAR_LENGTH = 2;
private static final String DELIMITER = "\\|"; private static final String QUESTION_DELIMITER = "\\|";
private static final int NUM_QUESTIONS = 10; private static final int NUM_QUESTIONS = 10;
private static final int START_TIME_SECS = 90; private static final int START_TIME_SECONDS = 90;
private static final int INDEX_OFFSET = 1;
private static final Path QUIZ_FILE_PATH; private static final Path QUIZ_FILE_PATH;
private static final List<Question> questions; private static final List<Question> questions;
static { static
{
questions = new ArrayList<>(); questions = new ArrayList<>();
QUIZ_FILE_PATH = Paths.get("data", "quiz.txt"); QUIZ_FILE_PATH = Paths.get("data", "quiz.txt");
} }
@@ -61,6 +64,7 @@ public class Quiz extends Application
private int correctCount; private int correctCount;
private int wrongCount; private int wrongCount;
private int remainingSeconds; private int remainingSeconds;
private List<Question> wrongQuestions;
private Timeline timeline; private Timeline timeline;
@@ -93,8 +97,8 @@ public class Quiz extends Application
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
{ {
final String[] parts; final String[] parts;
parts = line.split(DELIMITER); parts = line.split(QUESTION_DELIMITER);
if (parts.length < MIN_LENGTH) if (parts.length < MIN_CHAR_LENGTH)
{ {
continue; continue;
} }
@@ -102,20 +106,18 @@ public class Quiz extends Application
final String questionText; final String questionText;
final String[] answers; final String[] answers;
questionText = parts[QUESTION_SPLIT]; questionText = parts[QUESTION_SPLIT_INDEX];
answers = parts[ANSWER_SPLIT].split(","); answers = parts[ANSWER_SPLIT_INDEX].split(",");
questions.add(new Question( questions.add(new Question(
questionText, questionText,
answers answers
)); ));
} }
} } catch (final FileNotFoundException e)
catch (final FileNotFoundException e)
{ {
System.out.println("Unable to open file: " + path); System.out.println("Unable to open file: " + path);
} } catch (final IOException e)
catch (final IOException e)
{ {
System.out.println("Error reading file: " + path); System.out.println("Error reading file: " + path);
} }
@@ -131,7 +133,7 @@ public class Quiz extends Application
{ {
final VBox root; final VBox root;
root = new VBox(); root = new VBox();
root.setPrefSize(WIDTH, HEIGHT); root.setPrefSize(WIDTH_PIXELS, HEIGHT_PIXELS);
questionLabel = new Label("Click \"Start quiz\" to begin."); questionLabel = new Label("Click \"Start quiz\" to begin.");
feedbackLabel = new Label(); feedbackLabel = new Label();
@@ -193,13 +195,15 @@ public class Quiz extends Application
/** /**
* getRunnable creates a Runnable * getRunnable creates a Runnable
* lambda that is used to enter an answer on button click. * lambda that is used to enter an answer on button click.
*
* @return Runnable to manage answer * @return Runnable to manage answer
*/ */
private Runnable getRunnable() private Runnable getRunnable()
{ {
final Runnable submitAction; final Runnable submitAction;
submitAction = () -> { submitAction = () ->
{
if (current == null) if (current == null)
{ {
return; return;
@@ -220,10 +224,10 @@ public class Quiz extends Application
if (current.validateAnswer(cleaned)) if (current.validateAnswer(cleaned))
{ {
correctCount++; correctCount++;
} } else
else
{ {
wrongCount++; wrongCount++;
this.wrongQuestions.add(current);
} }
currentQuestionIndex++; currentQuestionIndex++;
@@ -239,14 +243,17 @@ public class Quiz extends Application
*/ */
private void startGame() private void startGame()
{ {
((VBox) feedbackLabel.getParent()).getChildren().removeIf(node -> node instanceof TextArea);
correctCount = COUNTER_STARTER; correctCount = COUNTER_STARTER;
wrongCount = COUNTER_STARTER; wrongCount = COUNTER_STARTER;
remainingSeconds = START_TIME_SECS; remainingSeconds = START_TIME_SECONDS;
currentQuestionIndex = COUNTER_STARTER; currentQuestionIndex = COUNTER_STARTER;
feedbackLabel.setText(""); feedbackLabel.setText("");
questionLabel.setText(""); questionLabel.setText("");
wrongQuestions = new ArrayList<>();
quizQuestions = new ArrayList<>(questions); quizQuestions = new ArrayList<>(questions);
Collections.shuffle(quizQuestions); Collections.shuffle(quizQuestions);
@@ -261,7 +268,8 @@ public class Quiz extends Application
} }
timeline = new Timeline( timeline = new Timeline(
new KeyFrame(Duration.seconds(ONE_SECOND), event -> { new KeyFrame(Duration.seconds(SECOND), event ->
{
remainingSeconds--; remainingSeconds--;
updateStatusLabel(); updateStatusLabel();
@@ -315,7 +323,7 @@ public class Quiz extends Application
} }
final int displayIndex; final int displayIndex;
displayIndex = Math.min(currentQuestionIndex + ONE_SECOND, quizQuestions.size()); displayIndex = Math.min(currentQuestionIndex + SECOND, quizQuestions.size());
statusLabel.setText( statusLabel.setText(
displayIndex + "/" + quizQuestions.size() + displayIndex + "/" + quizQuestions.size() +
@@ -340,12 +348,49 @@ public class Quiz extends Application
current = null; current = null;
final String finalScore; final String finalScore;
final TextArea solutions;
if (wrongCount > COUNTER_STARTER)
{
final StringBuilder allSolutions;
allSolutions = new StringBuilder();
for (final Question question : wrongQuestions)
{
final StringBuilder solution;
solution = new StringBuilder();
solution.append("Question: ");
solution.append(question.getQuestion());
solution.append("\n");
solution.append("Answer: ");
solution.append(question.getAnswersString());
solution.append("\n\n");
allSolutions.append(solution.toString());
}
solutions = new TextArea(allSolutions.toString());
solutions.setEditable(false);
}
else
{
solutions = null;
}
finalScore = reason + finalScore = reason +
"\n\nFinal score:" + "\n\nFinal score:" +
"\nCorrect: " + correctCount + "\nCorrect: " + correctCount +
"\nWrong: " + wrongCount; "\nWrong: " + wrongCount;
if (solutions != null)
{
final VBox root;
root = (VBox) feedbackLabel.getParent();
root.getChildren().add(root.getChildren().indexOf(feedbackLabel) + INDEX_OFFSET, solutions);
}
feedbackLabel.setText(finalScore); feedbackLabel.setText(finalScore);
questionLabel.setText("Game over."); questionLabel.setText("Game over.");