fix coding style
This commit is contained in:
1 file changed
+47
-24
@@ -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,7 +59,6 @@ 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;
|
||||||
@@ -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,6 +124,7 @@ 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);
|
||||||
|
|
||||||
@@ -149,7 +154,8 @@ public class Quiz extends Application
|
|||||||
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()
|
||||||
@@ -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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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,8 +333,9 @@ 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:"
|
|
||||||
|
finalScore = reason + "\n\nFinal score:"
|
||||||
+ "\nCorrect: " + correctCount
|
+ "\nCorrect: " + correctCount
|
||||||
+ "\nWrong: " + wrongCount;
|
+ "\nWrong: " + wrongCount;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user