From 1d27ebe98088e7cdf5be793181902d1597f33a07 Mon Sep 17 00:00:00 2001 From: Geks Date: Wed, 18 Dec 2024 14:43:05 +0200 Subject: [PATCH] had implemented registerUser() and validate() methods,added exceptions to handle wrong password input: now if user will enter different passwords to ensure right input - validator will give the CHECKED exception without service interruption but with information what to do. --- .../core/basesyntax/PasswordValidationException.java | 5 +++++ src/main/java/core/basesyntax/PasswordValidator.java | 8 +++++++- src/main/java/core/basesyntax/UserService.java | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..5ad01b58d 100644 --- a/src/main/java/core/basesyntax/PasswordValidationException.java +++ b/src/main/java/core/basesyntax/PasswordValidationException.java @@ -1,3 +1,8 @@ package core.basesyntax; +public class PasswordValidationException extends Exception { + public PasswordValidationException(String message) { + super(message); + } +} //write your code here diff --git a/src/main/java/core/basesyntax/PasswordValidator.java b/src/main/java/core/basesyntax/PasswordValidator.java index d4655c9f5..9fbbbcaa9 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,13 @@ package core.basesyntax; public class PasswordValidator { - public void validate(String password, String repeatPassword) { + public void validate(String password, String repeatPassword) + throws PasswordValidationException { //write your code here + if (password == null + || !password.equals(repeatPassword) + || password.length() < 10) { + 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..e82c3c871 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -1,8 +1,17 @@ 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()); + } catch (PasswordValidationException e) { + System.out.println("Your passwords are incorrect. Try again."); + return; + } + this.saveUser(user); } public void saveUser(User user) {