-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVWriter.java
153 lines (143 loc) · 4.69 KB
/
CSVWriter.java
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.io.*;
import java.util.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
/**
* The `CSVWriter` class provides methods to write and manipulate CSV files.
*
* @author (Dara O'Malley)
*/
public class CSVWriter {
/*
* CSVFile layout example
*
* student1Id,result1,result2,result3
* student2Id,result1,result2,result3
* student3Id,result1,result2,result3
* student4Id,result1,result2,result3
*
*/
/**
* Inserts a grade for a student in a CSV file.
*
* @param id The ID of the student.
* @param grade The grade to be inserted.
* @param moduleName The name of the module.
* @throws IOException If an input or output exception occurred.
*/
public static void insertGrade(String id, double grade, String moduleName) throws IOException {
File csvFile = new File("data/" + moduleName);
List<String> lines = Files.readAllLines(csvFile.toPath(), StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
String[] results = lines.get(i).split(",");
if (results[0].equals(id)) {
lines.set(i, lines.get(i) + "," + grade);
break;
}
}
Files.write(csvFile.toPath(), lines, StandardCharsets.UTF_8);
}
/*
* CSVFile layout example
*
* student1Id,result1,result2,result3
* student2Id,result1,result2,result3
* student3Id,result1,result2,result3
* student4Id,result1,result2,result3
*
*/
/**
* Removes a grade for a student in a CSV file.
*
* @param id The ID of the student.
* @param index The index of the grade to be removed.
* @param moduleName The name of the module.
* @throws IOException If an input or output exception occurred.
*/
public static void removeGrade(String id, int index, String moduleName) throws IOException {
Path path = Paths.get("data/" + moduleName);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
String[] results = lines.get(i).split(",");
if (results[0].equals(id)) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < results.length; j++) {
if (j != index) {
sb.append(results[j]);
if (j != results.length - 1) {
sb.append(",");
}
}
}
lines.set(i, sb.toString());
break;
}
}
Files.write(path, lines, StandardCharsets.UTF_8);
}
/*
* CSVFile layout example
*
* student1Id,message1,message2,message3
* student2Id,message1,message2,message3
* student3Id,message1,message2,message3
* student4Id,message1,message2,message3
*
*/
/**
* Writes a message for a student in a CSV file.
*
* @param id The ID of the student.
* @param message The message to be written.
* @throws IOException If an input or output exception occurred.
*/
public static void writeStudentMessage(String id, String message) throws IOException {
Path path = Paths.get("data/messages.csv");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
int i;
for (i = 0; i < lines.size(); i++) {
String[] messages = lines.get(i).split(",");
if (messages[0].equals(id)) {
lines.set(i, lines.get(i) + "," + message);
break;
}
}
if (i == lines.size()) {
lines.add(id + "," + message);
}
Files.write(path, lines, StandardCharsets.UTF_8);
}
/*
* CSVFile layout example
*
* student1Id,result1,result2,result3
* student2Id,result1,result2,result3
* student3Id,result1,result2,result3
* student4Id,result1,result2,result3
*
*/
/**
* Adds a student to a CSV file.
*
* @param fileName The name of the file.
* @param id The ID of the student.
*/
public static void addStudent(String fileName, String id) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("data/" + fileName, true));
bw.write(id);
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
}
}
}
}
}