-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserCredentials.java
62 lines (56 loc) · 1.97 KB
/
UserCredentials.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
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* A program Team Project
*
* <p>Purdue University -- CS18000 -- Spring 2024 -- Team Project-- </p>
*
* @author Purdue CS
* @version Mar 31, 2024
*/
public class UserCredentials {
private static final String FILE_NAME = "user_credentials.txt";
private static Map<String, String> credentials = new HashMap<>();
static {
try {
loadCredentialsFromFile();
} catch (IOException e) {
System.err.println("Error loading user credentials from file: " + e.getMessage());
}
}
public static boolean isValidCredentials(String username, String password) {
String storedPassword = credentials.get(username);
return storedPassword != null && storedPassword.equals(password);
}
public static void addCredentials(String username, String password) {
credentials.put(username, password);
saveCredentialsToFile();
}
static void loadCredentialsFromFile() throws IOException {
File file = new File(FILE_NAME);
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length == 2) {
credentials.put(parts[0], parts[1]);
}
}
reader.close();
}
}
private static void saveCredentialsToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME));
for (Map.Entry<String, String> entry : credentials.entrySet()) {
writer.write(entry.getKey() + ":" + entry.getValue());
writer.newLine();
}
writer.close();
} catch (IOException e) {
System.err.println("Error saving user credentials to file: " + e.getMessage());
}
}
}