Skip to content

Commit

Permalink
implemented methods registerUser() and validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
Your_Name Your_Surname committed Dec 23, 2024
1 parent 223a4a7 commit 15ea58a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 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);
}
}
7 changes: 5 additions & 2 deletions src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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.equals(repeatPassword) || password.length() < 10) {
throw new PasswordValidationException("Wrong passwords");
}
}
}
13 changes: 12 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package core.basesyntax;

public class UserService {
private PasswordValidator passwordValidator = new PasswordValidator();

public void registerUser(User user) {
//write your code here
boolean isPasswordValid = false;
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
isPasswordValid = true;
} catch (PasswordValidationException e) {
System.out.println("Your passwords are incorrect. Try again.");
}
if (isPasswordValid) {
saveUser(user);
}
}

public void saveUser(User user) {
Expand Down

0 comments on commit 15ea58a

Please sign in to comment.