-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
50 lines (40 loc) · 1.53 KB
/
Question.java
File metadata and controls
50 lines (40 loc) · 1.53 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
import java.util.*;
public class Question{
private String questionText;
private List<String> choices;
private String answer;
public Question(String questionText, List<String> choices, String answer) {
if (questionText == null || questionText.isEmpty()) {
System.out.println("Question text cannot be null or empty!");
} else if (choices == null || choices.isEmpty()) {
System.out.println("Choices cannot be null or empty!");
} else if (answer == null || answer.isEmpty()) {
System.out.println("Answer cannot be null or empty!");
} else if (!choices.contains(answer)) {
System.out.println("Answer is not present among the choices!");
} else {
this.questionText = questionText;
this.choices = choices;
this.answer = answer;
}
}
public String getAnswer() {
return answer;
}
public String getQuestionText() {
return questionText;
}
public boolean checkAnswer(String answer) {
return this.answer.equals(answer);
}
public List<String> getChoices() {
return choices;
}
public void display(){
System.out.println(getQuestionText());
for(int i = 0; i < choices.size(); i++){
int choiceNumber = i + 1;
System.out.println(choiceNumber + ":" + choices.get(i));
}
}
}