diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..ee30ac13e 100644 --- a/src/main/java/core/basesyntax/PasswordValidationException.java +++ b/src/main/java/core/basesyntax/PasswordValidationException.java @@ -1,3 +1,9 @@ package core.basesyntax; -//write your code here +public class PasswordValidationException extends Exception { + public PasswordValidationException(String message) { + super(message); + } +} + + diff --git a/src/main/java/core/basesyntax/PasswordValidator.java b/src/main/java/core/basesyntax/PasswordValidator.java index d4655c9f5..65279f6f6 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,14 @@ package core.basesyntax; public class PasswordValidator { - public void validate(String password, String repeatPassword) { - //write your code here + public void validate(String password, String repeatPassword) + throws PasswordValidationException { + if (password == null || repeatPassword == null) { + throw new PasswordValidationException("Wrong passwords"); + } + if (!(password.equals(repeatPassword)) || (password.length() < 10) + || (repeatPassword.length() < 10)) { + throw new PasswordValidationException("Wrong passwords"); + } } } diff --git a/src/main/java/core/basesyntax/User.java b/src/main/java/core/basesyntax/User.java index e702946a8..f26b6d0cb 100644 --- a/src/main/java/core/basesyntax/User.java +++ b/src/main/java/core/basesyntax/User.java @@ -28,6 +28,7 @@ public void setPassword(String password) { } public String getRepeatPassword() { + return repeatPassword; } diff --git a/src/main/java/core/basesyntax/UserService.java b/src/main/java/core/basesyntax/UserService.java index 52f08d130..7635bf801 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -2,7 +2,13 @@ public class UserService { public void registerUser(User user) { - //write your code here + try { + PasswordValidator passwordValidator = new PasswordValidator(); + passwordValidator.validate(user.getPassword(), user.getRepeatPassword()); + this.saveUser(user); + } catch (PasswordValidationException e) { + System.out.println("Your passwords are incorrect. Try again."); + } } public void saveUser(User user) {