-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultichoice.java
More file actions
45 lines (41 loc) · 1.49 KB
/
Copy pathMultichoice.java
File metadata and controls
45 lines (41 loc) · 1.49 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
import java.util.HashSet;
import java.util.List;
import java.util.StringJoiner;
public class Multichoice extends Question {
private boolean single;
private List<String> options;
private String solution;
public Multichoice(int difficulty, String questionText, boolean single, List<String> options, String solution) {
super(difficulty, questionText);
this.single = single;
this.options = options;
this.solution = solution;
StringJoiner joiner = new StringJoiner("\n");
for (int index = 0; index < options.size(); index++) {
joiner.add(String.format("\t%d) %s",
index + 1,
options.get(index))
);
}
setDefaultAnswer(joiner.toString());
}
@Override
public String getAnswer() {
HashSet<String> answers = new HashSet<>(List.of(solution.split(",")));
StringJoiner joiner = new StringJoiner("\n");
for (int index = 0; index < options.size(); index++) {
if (answers.contains(String.valueOf(index + 1))) {
joiner.add(String.format(" -> %d) %s",
index + 1,
options.get(index))
);
} else {
joiner.add(String.format("\t%d) %s",
index + 1,
options.get(index))
);
}
}
return joiner.toString();
}
}