From 50ccb02f459e108468014ab86d64612a1ebb095c Mon Sep 17 00:00:00 2001 From: Piotr Hic Date: Sun, 22 Dec 2024 08:50:18 +0100 Subject: [PATCH] v1 --- .../java/core/basesyntax/PasswordValidationException.java | 6 +++++- src/main/java/core/basesyntax/PasswordValidator.java | 6 ++++-- src/main/java/core/basesyntax/UserService.java | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..fc7870b6c 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); + } +} \ No newline at end of file diff --git a/src/main/java/core/basesyntax/PasswordValidator.java b/src/main/java/core/basesyntax/PasswordValidator.java index d4655c9f5..0c71f986a 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,9 @@ 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.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..c0283657d 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()); + }catch(Exception exception){ + System.out.println("Your passwords are incorrect. Try again."); + } + saveUser(user); } public void saveUser(User user) {