-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionFactory.java
More file actions
97 lines (86 loc) · 3.24 KB
/
Copy pathQuestionFactory.java
File metadata and controls
97 lines (86 loc) · 3.24 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
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.util.LinkedList;
import java.util.List;
public final class QuestionFactory {
public Question createQuestion(String questionType, Element element) {
NodeList listDifficulty = element.getElementsByTagName("difficulty");
int difficulty = Integer.parseInt(listDifficulty.item(0)
.getChildNodes()
.item(0)
.getTextContent()
);
NodeList listQuestionText = element.getElementsByTagName("questiontext");
String questionText = listQuestionText.item(0)
.getChildNodes()
.item(0)
.getTextContent();
if (questionType.equals("multichoice")) {
NodeList singleList = element.getElementsByTagName("single");
boolean single = Boolean.parseBoolean(singleList.item(0)
.getChildNodes()
.item(0)
.getTextContent()
);
//todo
NodeList optionsList = element.getElementsByTagName("options")
.item(0)
.getChildNodes();
List<String> options = new LinkedList<>();
for (int index = 1; index < optionsList.getLength(); index += 2) {
options.add(optionsList.item(index).getTextContent());
}
NodeList solutionList = element.getElementsByTagName("solution");
String solution = solutionList.item(0)
.getChildNodes()
.item(0)
.getTextContent();
return new Multichoice(
difficulty,
questionText,
single,
options,
solution
);
}
if (questionType.equals("truefalse")) {
NodeList answerList = element.getElementsByTagName("answer");
boolean answer = Boolean.parseBoolean(answerList.item(0)
.getChildNodes()
.item(0)
.getTextContent()
);
return new Truefalse(
difficulty,
questionText,
answer
);
}
if (questionType.equals("short")) {
NodeList answersList = element.getElementsByTagName("answers");
String answers = answersList.item(0)
.getChildNodes()
.item(0)
.getTextContent();
return new Short(
difficulty,
questionText,
answers
);
}
if (questionType.equals("essay")) {
NodeList answerList = element.getElementsByTagName("answer");
String answer = answerList.item(0)
.getChildNodes()
.item(0)
.getTextContent();
return new Essay(
difficulty,
questionText,
answer
);
}
System.out.println("No such type!");
return null;
}
}