From c31f20ccf936d4a3d92a6c93700037ffa83f9577 Mon Sep 17 00:00:00 2001 From: Serhii Holenkov Date: Fri, 20 Dec 2024 17:32:31 +0200 Subject: [PATCH] Implemented UserService solution with checked exception PasswordValidationException --- .../core/basesyntax/PasswordValidationException.java | 6 +++++- src/main/java/core/basesyntax/PasswordValidator.java | 11 +++++++++-- src/main/java/core/basesyntax/UserService.java | 8 +++++++- 3 files changed, 21 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..63a779b51 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) { + if (!password.equals(repeatPassword) || password.length() < 10) { + throw new PasswordValidationException("Wrong passwords"); + } + } else { + throw new PasswordValidationException("Null input data"); + } } } diff --git a/src/main/java/core/basesyntax/UserService.java b/src/main/java/core/basesyntax/UserService.java index 52f08d130..eaaa1dc17 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 + PasswordValidator passwordValidator = new PasswordValidator(); + 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) {