-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsvFormatStrategy.java
More file actions
49 lines (39 loc) · 1.84 KB
/
CsvFormatStrategy.java
File metadata and controls
49 lines (39 loc) · 1.84 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class CsvFormatStrategy implements FileFormatStrategy {
@Override
public ArrayList<Question> loadQuestions(String filePath) {
ArrayList<Question> questions = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
boolean firstLine = true;
while ((line = br.readLine()) != null) {
if (firstLine) {
firstLine = false;
continue; // Skip header
}
String[] parts = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); // Handle quoted commas
if (parts.length >= 4) {
String category = parts[0].trim().replace("\"", "");
int value = Integer.parseInt(parts[1].trim());
String content = parts[2].trim().replace("\"", "");
String correctAnswer = parts[3].trim().replace("\"", "");
Question question = new Question(category, value, content, null, correctAnswer);
// Add options if present
for (int i = 4; i < parts.length; i++) {
String option = parts[i].trim().replace("\"", "");
if (!option.isEmpty()) {
question.addOption(option);
}
}
questions.add(question);
}
}
} catch (IOException e) {
System.err.println("Error loading CSV file: " + e.getMessage());
}
return questions;
}
}