-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriviaGame.java
More file actions
38 lines (32 loc) · 1.02 KB
/
TriviaGame.java
File metadata and controls
38 lines (32 loc) · 1.02 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
import java.util.List;
public class TriviaGame {
private User user;
private List<Question> questions;
private int currentQuestionIndex = 0;
private int score = 0;
public TriviaGame(User user, List<Question> questions) {
this.user = user;
this.questions = questions;
}
public User getUser() {
return user;
}
public Question getNextQuestion() {
if (currentQuestionIndex < questions.size()) {
return questions.get(currentQuestionIndex++);
}
return null;
}
public boolean verifyAnswer(String userAnswer) {
if (currentQuestionIndex == 0) return false; // no question has been asked yet
Question currentQuestion = questions.get(currentQuestionIndex - 1);
boolean isCorrect = currentQuestion.getCorrectAnswer().equals(userAnswer);
if (isCorrect) {
score += 5;
}
return isCorrect;
}
public int getScore() {
return score;
}
}