fix coding style

This commit is contained in:
SowinskiBraeden committed 2025-12-01 13:40:20 -08:00
1 parent 0dd1771f83
commit 43dc19f063
2 files changed
+79 -56

No files matched your search

@@ -18,7 +18,7 @@ public class Question
{ {
private static final int MIN_ANSWERS = 1; private static final int MIN_ANSWERS = 1;
private final String question; private final String question;
private final String[] answers; private final String[] answers;
/** /**
@@ -61,7 +61,7 @@ public class Question
} }
this.question = question; this.question = question;
this.answers = answers; this.answers = answers;
} }
/** /**
+77 -54
View File
@@ -20,7 +20,6 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* A timed quiz application that loads questions from an external file * A timed quiz application that loads questions from an external file
@@ -46,7 +45,7 @@ public class Quiz extends Application
private static final String DELIMITER = "\\|"; private static final String 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_SECS = 90;
private static final Path QUIZ_FILE_PATH = Paths.get("comp2522", "data", "quiz.txt"); private static final Path QUIZ_FILE_PATH = Paths.get("data", "quiz.txt");
private final List<Question> questions = new ArrayList<>(); private final List<Question> questions = new ArrayList<>();
private List<Question> quizQuestions = new ArrayList<>(); private List<Question> quizQuestions = new ArrayList<>();
@@ -60,14 +59,13 @@ public class Quiz extends Application
private Timeline timeline; private Timeline timeline;
private VBox root; private Label questionLabel;
private Label questionLabel; private Label feedbackLabel;
private Label feedbackLabel; private Label statusLabel;
private Label statusLabel;
private TextField answerField; private TextField answerField;
private Button submitButton; private Button submitButton;
private Button startButton; private Button startButton;
private Button playAgainButton; private Button playAgainButton;
/** /**
* Reads and loads quiz questions from a file. * Reads and loads quiz questions from a file.
@@ -81,20 +79,26 @@ public class Quiz extends Application
return; return;
} }
try (BufferedReader reader = try
new BufferedReader(new FileReader(path.toAbsolutePath().toFile())))
{ {
final BufferedReader reader;
reader = new BufferedReader(new FileReader(path.toAbsolutePath().toFile()));
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
{ {
final String[] parts = line.split(DELIMITER); final String[] parts;
parts = line.split(DELIMITER);
if (parts.length < MIN_LENGTH) if (parts.length < MIN_LENGTH)
{ {
continue; continue;
} }
final String questionText = parts[QUESTION_SPLIT]; final String questionText;
final String[] answers = parts[ANSWER_SPLIT].split(","); final String[] answers;
questionText = parts[QUESTION_SPLIT];
answers = parts[ANSWER_SPLIT].split(",");
questions.add(new Question( questions.add(new Question(
questionText, questionText,
@@ -120,18 +124,19 @@ public class Quiz extends Application
@Override @Override
public void start(final Stage stage) public void start(final Stage stage)
{ {
final VBox root;
root = new VBox(); root = new VBox();
root.setPrefSize(WIDTH, HEIGHT); root.setPrefSize(WIDTH, HEIGHT);
questionLabel = new Label("Click \"Start quiz\" to begin."); questionLabel = new Label("Click \"Start quiz\" to begin.");
feedbackLabel = new Label(); feedbackLabel = new Label();
statusLabel = new Label(); statusLabel = new Label();
answerField = new TextField(); answerField = new TextField();
answerField.setPromptText("Type your answer here"); answerField.setPromptText("Type your answer here");
submitButton = new Button("Submit"); submitButton = new Button("Submit");
startButton = new Button("Start quiz"); startButton = new Button("Start quiz");
playAgainButton = new Button("Play again"); playAgainButton = new Button("Play again");
answerField.setDisable(true); answerField.setDisable(true);
@@ -140,19 +145,20 @@ public class Quiz extends Application
playAgainButton.setVisible(false); playAgainButton.setVisible(false);
root.getChildren().addAll( root.getChildren().addAll(
questionLabel, questionLabel,
answerField, answerField,
submitButton, submitButton,
feedbackLabel, feedbackLabel,
statusLabel, statusLabel,
startButton, startButton,
playAgainButton playAgainButton
); );
Scene scene = new Scene(root); final Scene scene;
scene = new Scene(root);
scene.getStylesheets().add( scene.getStylesheets().add(
getClass().getResource("style.css").toExternalForm() getClass().getResource("style.css").toExternalForm()
); );
readQuestions(QUIZ_FILE_PATH); readQuestions(QUIZ_FILE_PATH);
@@ -166,7 +172,28 @@ public class Quiz extends Application
return; return;
} }
final Runnable submitAction = () -> { final Runnable submitAction = getRunnable();
submitButton.setOnAction(event -> submitAction.run());
answerField.setOnAction(event -> submitAction.run());
startButton.setOnAction(event -> startGame());
playAgainButton.setOnAction(event -> startGame());
stage.setScene(scene);
stage.setTitle("Quiz");
stage.show();
}
/**
* getRunnable creates a Runnable
* lambda that is used to enter an answer on button click.
* @return Runnable to manage answer
*/
private Runnable getRunnable()
{
final Runnable submitAction;
submitAction = () -> {
if (current == null) if (current == null)
{ {
return; return;
@@ -181,13 +208,15 @@ public class Quiz extends Application
return; return;
} }
final String cleaned = userAnswer.trim(); final String cleaned;
cleaned = userAnswer.trim();
if (current.validateAnswer(cleaned)) if (current.validateAnswer(cleaned))
{ {
correctCount++; correctCount++;
} }
else { else
{
wrongCount++; wrongCount++;
} }
@@ -195,14 +224,7 @@ public class Quiz extends Application
showNextQuestion(); showNextQuestion();
}; };
submitButton.setOnAction(event -> submitAction.run()); return submitAction;
answerField.setOnAction(event -> submitAction.run());
startButton.setOnAction(event -> startGame());
playAgainButton.setOnAction(event -> startGame());
stage.setScene(scene);
stage.setTitle("Quiz");
stage.show();
} }
/** /**
@@ -211,9 +233,9 @@ public class Quiz extends Application
*/ */
private void startGame() private void startGame()
{ {
correctCount = COUNTER_STARTER; correctCount = COUNTER_STARTER;
wrongCount = COUNTER_STARTER; wrongCount = COUNTER_STARTER;
remainingSeconds = START_TIME_SECS; remainingSeconds = START_TIME_SECS;
currentQuestionIndex = COUNTER_STARTER; currentQuestionIndex = COUNTER_STARTER;
feedbackLabel.setText(""); feedbackLabel.setText("");
@@ -233,15 +255,15 @@ public class Quiz extends Application
} }
timeline = new Timeline( timeline = new Timeline(
new KeyFrame(Duration.seconds(ONE_SECOND), event -> { new KeyFrame(Duration.seconds(ONE_SECOND), event -> {
remainingSeconds--; remainingSeconds--;
updateStatusLabel(); updateStatusLabel();
if (remainingSeconds <= COUNTER_STARTER) if (remainingSeconds <= COUNTER_STARTER)
{ {
endGame("Time's up!"); endGame("Time's up!");
} }
}) })
); );
timeline.setCycleCount(Timeline.INDEFINITE); timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play(); timeline.play();
@@ -286,8 +308,8 @@ public class Quiz extends Application
return; return;
} }
final int displayIndex = final int displayIndex;
Math.min(currentQuestionIndex + ONE_SECOND, quizQuestions.size()); displayIndex = Math.min(currentQuestionIndex + ONE_SECOND, quizQuestions.size());
statusLabel.setText( statusLabel.setText(
displayIndex + "/" + quizQuestions.size() displayIndex + "/" + quizQuestions.size()
@@ -311,10 +333,11 @@ public class Quiz extends Application
answerField.setDisable(true); answerField.setDisable(true);
current = null; current = null;
final String finalScore = final String finalScore;
reason + "\n\nFinal score:"
+ "\nCorrect: " + correctCount finalScore = reason + "\n\nFinal score:"
+ "\nWrong: " + wrongCount; + "\nCorrect: " + correctCount
+ "\nWrong: " + wrongCount;
feedbackLabel.setText(finalScore); feedbackLabel.setText(finalScore);