forked from CidWB/COMP3607_Jeopardy_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvEventLogger.java
More file actions
52 lines (39 loc) · 1.76 KB
/
CsvEventLogger.java
File metadata and controls
52 lines (39 loc) · 1.76 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
import java.io.*;
public class CsvEventLogger {
private GameEventLog log;
private String filePath;
public CsvEventLogger(String filepath){
this.filePath = filepath;
initializeCsvFile();
}
private void initializeCsvFile() {
File file = new File(filePath);
if (!file.exists()) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
writer.println("case_id,player_id,activity,timestamp,category,question_value,content,answer,result,score_after_play");
} catch (IOException e) {
System.err.println("Error creating CSV file: " + e.getMessage());
}
}
}
public void updateLog(GameEventLog log){
this.log = log;
updateCsv();
}
private void updateCsv(){
try(PrintWriter writer = new PrintWriter(new FileWriter(filePath, true))) {
StringBuilder sb = new StringBuilder();
sb.append(log.getCaseId()).append(",");
sb.append(log.getTimestamp()).append(",");
sb.append(log.getActivity()).append(",");
sb.append(log.getPlayerId() != null ? log.getPlayerId() : "").append(",");
sb.append(log.getCategory() != null ? log.getCategory() : "").append(",");
sb.append(log.getQuestionValue() > 0 ? log.getQuestionValue() : "").append(",");
sb.append(log.getContent() != null ? "\"" + log.getContent().replace("\"", "\"\"") + "\"" : "").append(",");
sb.append(log.getAnswer() != null ? "\"" + log.getAnswer().replace("\"", "\"\"") + "\"" : "");
writer.println(sb.toString());
} catch (IOException e) {
System.err.println("Error updating CSV file: " + e.getMessage());
}
}
}