From 14431df2885250455de4c323b8425d8a5424b908 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 17 Oct 2023 19:05:59 +0300 Subject: [PATCH] completed jv-checked-exception --- .../basesyntax/PasswordValidationException.java | 7 ++++++- .../java/core/basesyntax/PasswordValidator.java | 14 ++++++++++++-- src/main/java/core/basesyntax/UserService.java | 9 ++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..7086a18d5 100644 --- a/src/main/java/core/basesyntax/PasswordValidationException.java +++ b/src/main/java/core/basesyntax/PasswordValidationException.java @@ -1,3 +1,8 @@ package core.basesyntax; -//write your code here +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..7bd42ec54 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,17 @@ package core.basesyntax; public class PasswordValidator { - public void validate(String password, String repeatPassword) { - //write your code here + private static final int MIN_PASSWORD_LENGTH = 9; + + public void validate(String password, String repeatPassword) + throws PasswordValidationException { + if (password == null || repeatPassword == null + || password.equals(repeatPassword) == false + || password.length() <= MIN_PASSWORD_LENGTH + || repeatPassword.length() <= MIN_PASSWORD_LENGTH + || password.length() != repeatPassword.length()) { + 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..7b8b8af17 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -1,8 +1,15 @@ package core.basesyntax; 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.print("Your passwords are incorrect. Try again."); + } } public void saveUser(User user) {