Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/main/java/UserService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import java.util.HashMap;
import java.util.Map;

public class UserService {

private Map<String, Integer> failedAttempts = new HashMap();
private static final int MAX_FAILED_ATTEMPTS = 4;

public String authenticate(String username, String password) {
if ("admin".equals(username) && "password123".equals(password)) {
if (isUserBlocked(username)) {
return "User is blocked due to too many failed login attempts.";
}

if ("admin".equals(username) && "password1234".equals(password)) {
resetFailedAttempts(username); // Reset attempts on successful login
return "Login successful";
} else {
return "Login failed";
incrementFailedAttempts(username);
if (failedAttempts.get(username) >= MAX_FAILED_ATTEMPTS) {
return "User is blocked due to too many failed login attempts.";
} else {
return "Login failed. You have " + (MAX_FAILED_ATTEMPTS - failedAttempts.get(username)) + " attempts left.";
}
}
}
}

private void incrementFailedAttempts(String username) {
failedAttempts.put(username, failedAttempts.getOrDefault(username, 0) + 1);
}

private void resetFailedAttempts(String username) {
failedAttempts.put(username, 0);
}

private boolean isUserBlocked(String username) {
return failedAttempts.getOrDefault(username, 0) >= MAX_FAILED_ATTEMPTS;
}
}