-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecords.java
More file actions
141 lines (103 loc) · 4.42 KB
/
Records.java
File metadata and controls
141 lines (103 loc) · 4.42 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
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
package officialInternalAssessment;
import java.io.*;
import java.util.*;
public class Records {
private static final String DATA_FILE = "/Users/binus/Desktop/users.txt";
public static List<Student> students = new LinkedList<>();
public static List<Teacher> teachers = new LinkedList<>();
public static void addUser(User u) {
if (u instanceof Student s) students.add(s);
else if (u instanceof Teacher t) teachers.add(t);
saveUser(u);
}
public static User checkLogin(String username, String password) {
for (User u : students)
if (u.getUsername().equals(username) && u.getPassword().equals(password))
return u;
for (User u : teachers)
if (u.getUsername().equals(username) && u.getPassword().equals(password))
return u;
return null;
}
// Append mode saving (no history)
private static void saveUser(User user) {
try (RandomAccessFile raf = new RandomAccessFile(DATA_FILE, "rw")) {
raf.seek(raf.length());
raf.writeBytes(user.getUsername() + "," + user.getPassword() + "," + user.getRole() + "\n");
} catch (IOException e) { e.printStackTrace(); }
}
// Rewrite file when necessary
private static void rewriteFile() {
try (RandomAccessFile raf = new RandomAccessFile(DATA_FILE, "rw")) {
raf.setLength(0); // clear file
recurseWriteStudents(raf, students, 0);
recurseWriteTeachers(raf, teachers, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
// Recursive methods
// Recursively write students
private static void recurseWriteStudents(RandomAccessFile raf, List<Student> list, int index) throws IOException {
if (index >= list.size()) return;
Student s = list.get(index);
// Write the student header
raf.writeBytes(s.getUsername() + "," + s.getPassword() + ",student\n");
// Recursively write their attempts
writeAttemptsRecursively(raf, s.getHistory(), 0);
// Move to next student
recurseWriteStudents(raf, list, index + 1);
}
// Recursively write teachers
private static void recurseWriteTeachers(RandomAccessFile raf, List<Teacher> list, int index) throws IOException {
if (index >= list.size()) return;
Teacher t = list.get(index);
raf.writeBytes(t.getUsername() + "," + t.getPassword() + ",teacher\n");
recurseWriteTeachers(raf, list, index + 1);
}
// Recursively write attempts
private static void writeAttemptsRecursively(RandomAccessFile raf, List<Student.PracticeAttempt> attempts, int index)
throws IOException {
if (index >= attempts.size()) return;
raf.writeBytes("ATTEMPT:" + attempts.get(index).toString() + "\n");
writeAttemptsRecursively(raf, attempts, index + 1);
}
// Load all data (polymorphic)
public static void loadData() {
students.clear();
teachers.clear();
try (RandomAccessFile raf = new RandomAccessFile(DATA_FILE, "rw")) {
String line;
Student currentStudent = null;
while ((line = raf.readLine()) != null) {
if (line.startsWith("ATTEMPT:")) {
if (currentStudent != null) {
Student.PracticeAttempt pa = Student.PracticeAttempt.fromString(line.substring(8));
currentStudent.getHistory().add(pa);
}
}
else {
String[] p = line.split(",");
if (p.length == 3) {
String name = p[0];
String pass = p[1];
String role = p[2];
if (role.equalsIgnoreCase("student")) {
currentStudent = new Student(name, pass);
students.add(currentStudent);
} else {
teachers.add(new Teacher(name, pass));
currentStudent = null;
}
}
}
}
} catch (IOException ignored) {}
}
public static void recordAttempt(User user, String topic, int score, int time) {
if (user instanceof Student s) {
s.addAttempt(topic, score, time);
rewriteFile();
}
}
}