-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentication.java
More file actions
58 lines (54 loc) · 2.2 KB
/
Copy pathAuthentication.java
File metadata and controls
58 lines (54 loc) · 2.2 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
import java.io.*;
import java.util.HashMap;
class Authentication {
String userDataFile = "userData.txt";
HashMap<String, String> extractAllUserList() {
HashMap<String, String> allUserList = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(userDataFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] components = line.split(" ");
allUserList.put(components[0], components[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
return allUserList;
}
boolean handleRegistration(String username, String password) {
if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
HashMap<String, String> allUserList = extractAllUserList();
if (allUserList.containsKey(username)) {
System.out.println("User is Already Exist");
return false;
} else {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(userDataFile, true))) {
writer.write(username + " " + password + System.lineSeparator()); // Adding a newline
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Registraton Successfull");
return true;
}
} else {
System.out.println("Invalid User");
return false;
}
}
boolean handleLogin (String username, String password){
HashMap<String, String> allUserList = extractAllUserList();
if (allUserList.containsKey(username)) {
String storedPassword = allUserList.get(username);
if (storedPassword.equals(password)) {
System.out.println("Login Successfull");
return true;
} else {
System.out.println("Password is not Matched");
return false;
}
} else {
System.out.println("User is Not Registered");
return false;
}
}
}