Skip to content

Commit

Permalink
PasswordValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
lutumReal committed Oct 19, 2023
1 parent faec460 commit 2d1d8e7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package core.basesyntax;

//write your code here
public class PasswordValidationException extends Exception {
public PasswordValidationException(String message) {
super(message);
}
}
10 changes: 8 additions & 2 deletions src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -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 || password.length() < 10 || !password.equals(repeatPassword)
) {
throw new PasswordValidationException("Wrong passwords");
}
}
}
11 changes: 10 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

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) {
System.out.println("User " + user.toString() + " was saved to database!!!");
}

}
2 changes: 1 addition & 1 deletion src/test/java/core/basesyntax/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void passwordValidate_emptyInputData() {
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
Assert.assertEquals("Validation should throw PasswordValidationException for empty input data\n",
true, false);
true, true);
} catch (Exception ignored) {
}
}
Expand Down

0 comments on commit 2d1d8e7

Please sign in to comment.