From 9ba61eb10defe2123658ffd911bc6f6b9a56eb2c Mon Sep 17 00:00:00 2001 From: Tony Hut Date: Sat, 14 Oct 2023 13:42:22 +0200 Subject: [PATCH] first commit --- pom.xml | 6 ++++-- src/main/java/core/basesyntax/Main.java | 10 ++++++++++ .../core/basesyntax/PasswordValidationException.java | 6 +++++- src/main/java/core/basesyntax/PasswordValidator.java | 10 ++++++++-- src/main/java/core/basesyntax/UserService.java | 9 ++++++++- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/main/java/core/basesyntax/Main.java diff --git a/pom.xml b/pom.xml index f7e3fc1c9..1f864306c 100644 --- a/pom.xml +++ b/pom.xml @@ -18,12 +18,14 @@ + junit junit - 4.12 + 4.13.2 test + @@ -41,7 +43,7 @@ ${maven.checkstyle.plugin.configLocation} - UTF-8 + true true false diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 000000000..494f4153f --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + User user = new User("hello", null, null); + + UserService userService = new UserService(); + userService.registerUser(user); + } +} 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..bf9999d1b 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) { - //write your code here + public void validate(String password, String repeatPassword) + throws PasswordValidationException { + + if (password == null && repeatPassword == null + || password.length() < 10 && 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..50f3955a2 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -2,7 +2,14 @@ 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) {