show solutions
This commit is contained in:
2 files changed
+83
-24
No files matched your search
@@ -84,6 +84,20 @@ public class Question
|
||||
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.
|
||||
* Matching is case-insensitive.
|
||||
|
||||
@@ -8,6 +8,7 @@ import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.util.Duration;
|
||||
|
||||
@@ -35,21 +36,23 @@ import java.util.List;
|
||||
*/
|
||||
public class Quiz extends Application
|
||||
{
|
||||
private static final int WIDTH = 800;
|
||||
private static final int HEIGHT = 600;
|
||||
private static final int QUESTION_SPLIT = 0;
|
||||
private static final int WIDTH_PIXELS = 800;
|
||||
private static final int HEIGHT_PIXELS = 600;
|
||||
private static final int QUESTION_SPLIT_INDEX = 0;
|
||||
private static final int COUNTER_STARTER = 0;
|
||||
private static final int ANSWER_SPLIT = 1;
|
||||
private static final int ONE_SECOND = 1;
|
||||
private static final int MIN_LENGTH = 2;
|
||||
private static final String DELIMITER = "\\|";
|
||||
private static final int ANSWER_SPLIT_INDEX = 1;
|
||||
private static final int SECOND = 1;
|
||||
private static final int MIN_CHAR_LENGTH = 2;
|
||||
private static final String QUESTION_DELIMITER = "\\|";
|
||||
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 List<Question> questions;
|
||||
|
||||
static {
|
||||
static
|
||||
{
|
||||
questions = new ArrayList<>();
|
||||
QUIZ_FILE_PATH = Paths.get("data", "quiz.txt");
|
||||
}
|
||||
@@ -61,6 +64,7 @@ public class Quiz extends Application
|
||||
private int correctCount;
|
||||
private int wrongCount;
|
||||
private int remainingSeconds;
|
||||
private List<Question> wrongQuestions;
|
||||
|
||||
private Timeline timeline;
|
||||
|
||||
@@ -93,8 +97,8 @@ public class Quiz extends Application
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
final String[] parts;
|
||||
parts = line.split(DELIMITER);
|
||||
if (parts.length < MIN_LENGTH)
|
||||
parts = line.split(QUESTION_DELIMITER);
|
||||
if (parts.length < MIN_CHAR_LENGTH)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -102,20 +106,18 @@ public class Quiz extends Application
|
||||
final String questionText;
|
||||
final String[] answers;
|
||||
|
||||
questionText = parts[QUESTION_SPLIT];
|
||||
answers = parts[ANSWER_SPLIT].split(",");
|
||||
questionText = parts[QUESTION_SPLIT_INDEX];
|
||||
answers = parts[ANSWER_SPLIT_INDEX].split(",");
|
||||
|
||||
questions.add(new Question(
|
||||
questionText,
|
||||
answers
|
||||
));
|
||||
}
|
||||
}
|
||||
catch (final FileNotFoundException e)
|
||||
} catch (final FileNotFoundException e)
|
||||
{
|
||||
System.out.println("Unable to open file: " + path);
|
||||
}
|
||||
catch (final IOException e)
|
||||
} catch (final IOException e)
|
||||
{
|
||||
System.out.println("Error reading file: " + path);
|
||||
}
|
||||
@@ -131,7 +133,7 @@ public class Quiz extends Application
|
||||
{
|
||||
final VBox root;
|
||||
root = new VBox();
|
||||
root.setPrefSize(WIDTH, HEIGHT);
|
||||
root.setPrefSize(WIDTH_PIXELS, HEIGHT_PIXELS);
|
||||
|
||||
questionLabel = new Label("Click \"Start quiz\" to begin.");
|
||||
feedbackLabel = new Label();
|
||||
@@ -193,13 +195,15 @@ public class Quiz extends Application
|
||||
/**
|
||||
* 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 = () -> {
|
||||
submitAction = () ->
|
||||
{
|
||||
if (current == null)
|
||||
{
|
||||
return;
|
||||
@@ -220,10 +224,10 @@ public class Quiz extends Application
|
||||
if (current.validateAnswer(cleaned))
|
||||
{
|
||||
correctCount++;
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
wrongCount++;
|
||||
this.wrongQuestions.add(current);
|
||||
}
|
||||
|
||||
currentQuestionIndex++;
|
||||
@@ -239,14 +243,17 @@ public class Quiz extends Application
|
||||
*/
|
||||
private void startGame()
|
||||
{
|
||||
((VBox) feedbackLabel.getParent()).getChildren().removeIf(node -> node instanceof TextArea);
|
||||
|
||||
correctCount = COUNTER_STARTER;
|
||||
wrongCount = COUNTER_STARTER;
|
||||
remainingSeconds = START_TIME_SECS;
|
||||
remainingSeconds = START_TIME_SECONDS;
|
||||
currentQuestionIndex = COUNTER_STARTER;
|
||||
|
||||
feedbackLabel.setText("");
|
||||
questionLabel.setText("");
|
||||
|
||||
wrongQuestions = new ArrayList<>();
|
||||
quizQuestions = new ArrayList<>(questions);
|
||||
Collections.shuffle(quizQuestions);
|
||||
|
||||
@@ -261,7 +268,8 @@ public class Quiz extends Application
|
||||
}
|
||||
|
||||
timeline = new Timeline(
|
||||
new KeyFrame(Duration.seconds(ONE_SECOND), event -> {
|
||||
new KeyFrame(Duration.seconds(SECOND), event ->
|
||||
{
|
||||
remainingSeconds--;
|
||||
updateStatusLabel();
|
||||
|
||||
@@ -315,7 +323,7 @@ public class Quiz extends Application
|
||||
}
|
||||
|
||||
final int displayIndex;
|
||||
displayIndex = Math.min(currentQuestionIndex + ONE_SECOND, quizQuestions.size());
|
||||
displayIndex = Math.min(currentQuestionIndex + SECOND, quizQuestions.size());
|
||||
|
||||
statusLabel.setText(
|
||||
displayIndex + "/" + quizQuestions.size() +
|
||||
@@ -340,12 +348,49 @@ public class Quiz extends Application
|
||||
current = null;
|
||||
|
||||
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 +
|
||||
"\n\nFinal score:" +
|
||||
"\nCorrect: " + correctCount +
|
||||
"\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);
|
||||
|
||||
questionLabel.setText("Game over.");
|
||||
|
||||
Reference in new issue
Block a user