-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersHandling.java
More file actions
68 lines (53 loc) · 2.38 KB
/
Copy pathUsersHandling.java
File metadata and controls
68 lines (53 loc) · 2.38 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
import java.io.*;
import java.util.HashMap;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class UsersHandling {
private HashMap<String, Student> users;
public UsersHandling(HashMap<String, Student> users) {
this.users = users;
}
public void loadUsersFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader("users.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" : ");
if (parts.length >= 3) {
String username = parts[0];
String storedHashedPassword = parts[1]; // This is the hashed password from the file
String id = parts[2];
String firstName = (parts.length > 3) ? parts[3] : "default";
String lastName = (parts.length > 4) ? parts[4] : "default";
// No need to hash the loaded password again
Student student = new Student(username, storedHashedPassword, id, firstName, lastName);
users.put(username, student);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveUsersToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("users.txt"));
for (String username : users.keySet()) {
Student student = users.get(username);
String line;
// Get the stored hashed password directly
String storedHashedPassword = student.getPassword();
if (student.getLastname() != null && student.getFirstname() != null) {
line = username + " : " + storedHashedPassword + " : " + student.getId() + " : " + student.getFirstname()
+ " : " + student.getLastname() + "\n";
} else {
line = username + " : " + storedHashedPassword + " : " + student.getId() + "\n";
}
writer.write(line);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}