/
v.nilov
/
Zadanie11
Обзор
Документация
Войти
/
v.nilov
/
Zadanie11
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
PasswordChecker.java
68 строк
2 KB
v.nilov
Zadanie11
18 ноя 2025, 00:01
18 ноя 2025, 00:01
afceddd
Код
Авторство
О чём код?
public class PasswordChecker { private Integer minLength; private Integer maxRepeats; public void setMinLength(int minLength) { if (minLength < 0) { throw new IllegalArgumentException("Минимальная длина не может быть отрицательной: " + minLength); } this.minLength = minLength; } public void setMaxRepeats(int maxRepeats) { if (maxRepeats <= 0) { throw new IllegalArgumentException("Максимальное количество повторений должно быть положительным: " + maxRepeats); } this.maxRepeats = maxRepeats; } public boolean verify(String password) { if (minLength == null) { throw new IllegalStateException("Минимальная длина не установлена. Сначала вызовите setMinLength."); } if (maxRepeats == null) { throw new IllegalStateException("Максимальное количество повторений не установлено. Сначала вызовите setMaxRepeats."); } if (password == null) { return false; } if (password.length() < minLength) { return false; } return !hasExcessiveRepeats(password); } private boolean hasExcessiveRepeats(String password) { if (password.isEmpty()) { return false; } char currentChar = password.charAt(0); int currentCount = 1; for (int i = 1; i < password.length(); i++) { if (password.charAt(i) == currentChar) { currentCount++; if (currentCount > maxRepeats) { return true; } } else { currentChar = password.charAt(i); currentCount = 1; } } return false; } public Integer getMinLength() { return minLength; } public Integer getMaxRepeats() { return maxRepeats; } }