-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizApp.java
More file actions
179 lines (149 loc) · 5.39 KB
/
QuizApp.java
File metadata and controls
179 lines (149 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
public class QuizApp extends Application
{
public static final int QUESTION_ANSWER_LENGTH = 2;
public static final int MAX_QUESTIONS = 10;
public static final int LAYOUT_SIZE = 15;
public static final int SCENE_WIDTH = 600;
public static final int SCENE_LENGTH = 400;
public static final int STARTING_SCORE = 0;
public static final int STARTING_QUESTION_COUNT = 0;
private final List<String[]> questionsAndAnswers = new ArrayList<>();
private String currentAnswer;
private int questionCount;
private int score;
@Override
public void start(final Stage primaryStage)
{
final Label questionLabel;
final Label statusLabel;
final TextField answerField;
final Button submitButton;
final Button startQuizButton;
final VBox layout;
final Scene scene;
layout = new VBox(LAYOUT_SIZE);
questionLabel = new Label("Press 'Start Quiz' to begin!");
statusLabel = new Label("Status: ");
answerField = new TextField();
submitButton = new Button("Submit");
startQuizButton = new Button("Start Quiz");
initializeQuestionsFromFile();
primaryStage.setTitle("Quiz");
submitButton.setOnAction(e -> handleAnswer(answerField, questionLabel, statusLabel, startQuizButton));
answerField.setOnKeyPressed(e ->
{
if(e.getCode() == KeyCode.ENTER)
{
handleAnswer(answerField, questionLabel, statusLabel, startQuizButton);
}
});
startQuizButton.setOnAction(e ->
{
resetQuiz(questionLabel, statusLabel, answerField);
startQuizButton.setDisable(true);
});
layout.getChildren().addAll(questionLabel, answerField, submitButton, statusLabel, startQuizButton);
scene = new Scene(layout, SCENE_WIDTH, SCENE_LENGTH);
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/styles.css")).toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
private void handleAnswer(final TextField answerField,
final Label questionLabel,
final Label statusLabel,
final Button startQuizButton)
{
if(questionCount < MAX_QUESTIONS)
{
if(answerField.getText().equalsIgnoreCase(currentAnswer))
{
score++;
statusLabel.setText("Status: CORRECT! Your score: " + score);
}
else
{
statusLabel.setText("Status: INCORRECT! The answer was: " + currentAnswer);
}
answerField.clear();
if(++questionCount < MAX_QUESTIONS)
{
nextQuestion(questionLabel);
}
else
{
statusLabel.setText("Quiz Complete! Final Score: " + score + "/" + MAX_QUESTIONS);
questionLabel.setText("Press 'Start Quiz' to play again!");
startQuizButton.setDisable(false);
}
}
}
private void resetQuiz(final Label questionLabel,
final Label statusLabel,
final TextField answerField)
{
questionCount = STARTING_QUESTION_COUNT;
score = STARTING_SCORE;
answerField.clear();
statusLabel.setText("Status: ");
nextQuestion(questionLabel);
}
private final void nextQuestion(final Label questionLabel)
{
String question;
question = getRandomQuestion();
questionLabel.setText(question);
}
private final void initializeQuestionsFromFile()
{
questionsAndAnswers.clear();
try(final BufferedReader br = new BufferedReader(new FileReader("src/quiz.txt")))
{
String line;
while((line = br.readLine()) != null)
{
final String[] qAndA;
qAndA = line.split("\\|");
if(qAndA.length == QUESTION_ANSWER_LENGTH)
{
questionsAndAnswers.add(qAndA);
}
}
}
catch (final IOException e)
{
e.printStackTrace();
}
}
private final String getRandomQuestion()
{
final Random random;
final int index;
random = new Random();
if(questionsAndAnswers.isEmpty())
{
return "No questions available!";
}
index = random.nextInt(questionsAndAnswers.size());
currentAnswer = questionsAndAnswers.get(index)[1];
return questionsAndAnswers.get(index)[STARTING_SCORE];
}
public static void main(final String[] args)
{
launch(args);
}
}