Skip to content

Commit bdefd77

Browse files
factory class
1 parent 093768b commit bdefd77

File tree

3 files changed

+76
-42
lines changed

3 files changed

+76
-42
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package quiz_use_case.GUI;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Factory that produces GUI question cards.
7+
* Frameworks & Drivers
8+
* @author Anthony
9+
*/
10+
public class QuestionCardFactory {
11+
/**
12+
* Creates a question card.
13+
* @param type the type of question
14+
* @param num the question number
15+
* @param outputText the output text
16+
* @return the question card
17+
*/
18+
public QuestionCard createQuestionCard(String type, int num, ArrayList<String> outputText) {
19+
switch(type) {
20+
case "MC":
21+
return new MultipleChoiceQuestionCard(num, outputText);
22+
case "TE":
23+
return new TextEntryQuestionCard(num, outputText);
24+
case "TF":
25+
return new TrueFalseQuestionCard(num, outputText);
26+
default:
27+
throw new IllegalArgumentException("Invalid type.");
28+
}
29+
}
30+
31+
/**
32+
* Creates an answered question card.
33+
* @param type the type of question
34+
* @param num the question number
35+
* @param outputText the output text
36+
* @param userAnswer the user answer
37+
* @param actualAnswer the actual answer
38+
* @return the answered question card
39+
*/
40+
public QuestionCard createAnsweredQuestionCard(String type, int num, ArrayList<String> outputText,
41+
String userAnswer, String actualAnswer) {
42+
switch(type) {
43+
case "MC":
44+
return new MultipleChoiceQuestionCard(num, outputText, userAnswer, actualAnswer);
45+
case "TE":
46+
return new TextEntryQuestionCard(num, outputText, userAnswer, actualAnswer);
47+
case "TF":
48+
return new TrueFalseQuestionCard(num, outputText, userAnswer, actualAnswer);
49+
default:
50+
throw new IllegalArgumentException("Invalid type.");
51+
}
52+
}
53+
}

src/main/java/quiz_use_case/screens/QuizResultsScreen.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
*/
1616
public class QuizResultsScreen extends Screen {
1717
private final QuizController controller;
18-
1918
private final int flashcardSetID;
2019

21-
private final ArrayList<QuestionCard> questionCards;
22-
23-
// actions
2420
private enum Actions {
2521
RESTART
2622
}
@@ -36,7 +32,7 @@ public QuizResultsScreen(QuizController controller, QuizResponseModel response,
3632

3733
this.controller = controller;
3834
this.flashcardSetID = flashcardSetID;
39-
this.questionCards = new ArrayList<>();
35+
QuestionCardFactory questionCardFactory = new QuestionCardFactory();
4036

4137
ArrayList<String> types = response.getTypes();
4238
ArrayList<ArrayList<String>> outputText = response.getOutputText();
@@ -73,25 +69,10 @@ public QuizResultsScreen(QuizController controller, QuizResponseModel response,
7369
c.fill = GridBagConstraints.BOTH;
7470

7571
for (int i = 0; i < types.size(); i++) {
76-
String type = types.get(i);
77-
QuestionCard q;
7872
c.gridy = i;
79-
if (type.equals("MC")) {
80-
q = new MultipleChoiceQuestionCard(i+1, outputText.get(i),
81-
userAnswers.get(i),actualAnswers.get(i));
82-
this.questionCards.add(q);
83-
centerPanel.add(q, c);
84-
} else if (type.equals("TE")) {
85-
q = new TextEntryQuestionCard(i+1, outputText.get(i),
86-
userAnswers.get(i), actualAnswers.get(i));
87-
this.questionCards.add(q);
88-
centerPanel.add(q, c);
89-
} else if (type.equals("TF")) {
90-
q = new TrueFalseQuestionCard(i+1, outputText.get(i),
91-
userAnswers.get(i), actualAnswers.get(i));
92-
this.questionCards.add(q);
93-
centerPanel.add(q, c);
94-
}
73+
QuestionCard q = questionCardFactory.createAnsweredQuestionCard(types.get(i), i+1, outputText.get(i),
74+
userAnswers.get(i), actualAnswers.get(i));
75+
centerPanel.add(q, c);
9576
}
9677

9778
// SCROLL BAR
@@ -114,6 +95,10 @@ public QuizResultsScreen(QuizController controller, QuizResponseModel response,
11495
this.setupScreen();
11596
}
11697

98+
/**
99+
* Restarts back to quiz settings screen on command.
100+
* @param e the action event
101+
*/
117102
@Override
118103
public void actionPerformed(ActionEvent e) {
119104
String command = e.getActionCommand();

src/main/java/quiz_use_case/screens/QuizScreen.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@
1515
*/
1616
public class QuizScreen extends Screen {
1717
private final QuizController controller;
18-
1918
private final int flashcardSetID;
20-
2119
private final ArrayList<QuestionCard> questionCards;
2220

23-
// actions
2421
private enum Actions {
2522
RESTART, SUBMIT
2623
}
2724

25+
/**
26+
* Constructs a quiz screen.
27+
* @param controller the quiz controller
28+
* @param response the quiz settings response model
29+
* @param flashcardSetID the flashcard set ID
30+
*/
2831
public QuizScreen(QuizController controller, QuizSettingsResponseModel response, int flashcardSetID) {
2932
super("Quiz", false);
3033

3134
this.controller = controller;
3235
this.flashcardSetID = flashcardSetID;
33-
3436
this.questionCards = new ArrayList<>();
37+
QuestionCardFactory questionCardFactory = new QuestionCardFactory();
3538

3639
ArrayList<String> types = response.getTypes();
3740
ArrayList<ArrayList<String>> outputText = response.getOutputText();
@@ -48,22 +51,10 @@ public QuizScreen(QuizController controller, QuizSettingsResponseModel response,
4851
c.fill = GridBagConstraints.BOTH;
4952

5053
for (int i = 0; i < types.size(); i++) {
51-
String type = types.get(i);
52-
QuestionCard q;
5354
c.gridy = i;
54-
if (type.equals("MC")) {
55-
q = new MultipleChoiceQuestionCard(i+1, outputText.get(i));
56-
this.questionCards.add(q);
57-
centerPanel.add(q, c);
58-
} else if (type.equals("TE")) {
59-
q = new TextEntryQuestionCard(i+1, outputText.get(i));
60-
this.questionCards.add(q);
61-
centerPanel.add(q, c);
62-
} else if (type.equals("TF")) {
63-
q = new TrueFalseQuestionCard(i+1, outputText.get(i));
64-
this.questionCards.add(q);
65-
centerPanel.add(q, c);
66-
}
55+
QuestionCard q = questionCardFactory.createQuestionCard(types.get(i), i+1, outputText.get(i));
56+
this.questionCards.add(q);
57+
centerPanel.add(q, c);
6758
}
6859

6960
// SCROLL BAR
@@ -89,6 +80,11 @@ public QuizScreen(QuizController controller, QuizSettingsResponseModel response,
8980
this.setupScreen();
9081
}
9182

83+
/**
84+
* RESTART - the program returns to the quiz settings screen.
85+
* SUBMIT - the program collects all user answers and proceeds to the quiz results screen.
86+
* @param e the action event
87+
*/
9288
@Override
9389
public void actionPerformed(ActionEvent e) {
9490
String command = e.getActionCommand();

0 commit comments

Comments
 (0)