From e6acaa6d846845fa17f750aaee7f35ed2ec7cffe Mon Sep 17 00:00:00 2001 From: Kamil Foatov Date: Mon, 10 Feb 2025 03:01:47 +0300 Subject: [PATCH] PasswordLockoutRequirement implementation --- PasswordLockoutRequirement.java | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 PasswordLockoutRequirement.java diff --git a/PasswordLockoutRequirement.java b/PasswordLockoutRequirement.java new file mode 100644 index 0000000..f54e423 --- /dev/null +++ b/PasswordLockoutRequirement.java @@ -0,0 +1,35 @@ +import java.util.HashMap; +import java.util.Map; + +public class PasswordLockoutRequirement extends Requirement { + private static final int MAX_FAILED_ATTEMPTS = 5; + private final String username; + private final Map failedAttempts = new HashMap<>(); + + public PasswordLockoutRequirement(String username) { + this.username = username; + } + + public void recordFailedAttempt() { + int currentAttempts = failedAttempts.getOrDefault(username, 0); + failedAttempts.put(username, currentAttempts + 1); + } + + public void resetFailedAttempts() { + failedAttempts.remove(username); + } + + @Override + public CheckStatus check() { + int attempts = failedAttempts.getOrDefault(username, 0); + + if (attempts >= MAX_FAILED_ATTEMPTS) { + System.out.println("The account is locked due to too many failed login attempts."); + return CheckStatus.FAIL; + } else { + int attemptsLeft = MAX_FAILED_ATTEMPTS - attempts; + System.out.println("The account is open. After " + attemptsLeft + " failed attempts it will become locked"); + return CheckStatus.PASS; + } + } +} \ No newline at end of file