-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationService.java
More file actions
39 lines (32 loc) · 1.07 KB
/
AuthenticationService.java
File metadata and controls
39 lines (32 loc) · 1.07 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
import java.util.*;
public class AuthenticationService {
private List<User> users;
public AuthenticationService() {
users = new ArrayList<>();
}
public boolean isValidEmail(String email) {
return email != null && email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
}
public boolean registerUser(User user) {
if (!isValidEmail(user.getEmail())) {
System.out.println("Invalid email: " + user.getEmail());
return false;
}
for (User u : users) {
if (u.getUsername().equals(user.getUsername())) {
System.out.println("Username already exists: " + user.getUsername());
return false;
}
}
users.add(user);
return true;
}
public User loginUser(String username, String password) {
for (User u : users) {
if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
return u;
}
}
return null;
}
}