From fb6dce8d17af99acf6406b899eadd8a9427896d0 Mon Sep 17 00:00:00 2001 From: Leonid Bondarenko Date: Sat, 14 Oct 2023 11:34:32 +0300 Subject: [PATCH] 14/10/23 --- .../core/basesyntax/PasswordValidationException.java | 6 +++++- src/main/java/core/basesyntax/PasswordValidator.java | 8 ++++++-- src/main/java/core/basesyntax/UserService.java | 10 +++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..70c1d49fd 100644 --- a/src/main/java/core/basesyntax/PasswordValidationException.java +++ b/src/main/java/core/basesyntax/PasswordValidationException.java @@ -1,3 +1,7 @@ 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..32e6b189e 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,11 @@ 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 || password.length() < 10 || repeatPassword == null + || repeatPassword.length() < 10 || !password.equals(repeatPassword)) { + throw new PasswordValidationException("Wrong passwords"); + } } } diff --git a/src/main/java/core/basesyntax/UserService.java b/src/main/java/core/basesyntax/UserService.java index 52f08d130..48fa353a3 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -1,8 +1,16 @@ package core.basesyntax; public class UserService { + private PasswordValidator passwordValidator = new PasswordValidator(); + public void registerUser(User user) { - //write your code here + try { + passwordValidator.validate(user.getPassword(), user.getRepeatPassword()); + saveUser(user); + + } catch (PasswordValidationException e) { + System.out.println("Your passwords are incorrect. Try again."); + } } public void saveUser(User user) {