Skip to content

Commit

Permalink
add checked exception for validation passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Dec 28, 2024
1 parent 223a4a7 commit a50f80c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package core.basesyntax;

//write your code here
class PasswordValidationException extends Exception {
public PasswordValidationException(String message) {
super(message);
}
}
13 changes: 12 additions & 1 deletion src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package core.basesyntax;

public class PasswordValidator {
public void validate(String password, String repeatPassword) {
public void validate(String password, String repeatPassword)
throws PasswordValidationException {
PasswordValidationException exception = new PasswordValidationException("Wrong passwords");
//write your code here
if (password == null || repeatPassword == null) {
throw exception;
}
if (password.length() < 10 && repeatPassword.length() < 10) {
throw exception;
}
if (!password.equals(repeatPassword)) {
throw exception;
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -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;
}
saveUser(user);
}

public void saveUser(User user) {
Expand Down

0 comments on commit a50f80c

Please sign in to comment.