Skip to content

Commit

Permalink
Implement password validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitazuka committed Nov 13, 2024
1 parent 223a4a7 commit f900c59
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package core.basesyntax;

//write your code here
public class PasswordValidationException extends Exception {
public PasswordValidationException(String message) {
super(message);
}
}
10 changes: 8 additions & 2 deletions src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package core.basesyntax;

public class PasswordValidator {
public void validate(String password, String repeatPassword) {
//write your code here
private static final int MIN_PASSWORD_LENGTH = 10;

public void validate(String password, String repeatPassword)
throws PasswordValidationException {
if (null == password || password.length() < MIN_PASSWORD_LENGTH
|| !password.equals(repeatPassword)) {
throw new PasswordValidationException("Wrong passwords");
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

public class UserService {
public void registerUser(User user) {
//write your code here
PasswordValidator validator = new PasswordValidator();
try {
validator.validate(user.getPassword(), user.getRepeatPassword());
saveUser(user);
} catch (PasswordValidationException e) {
System.out.println("Your passwords are incorrect. Try again.");
}
}

public void saveUser(User user) {
Expand Down

0 comments on commit f900c59

Please sign in to comment.