Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented methods registerUser(), validate(). #1300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ We should implement the following methods:
- `registerUser()` in UserService class
- `validate()` in PasswordValidator class

Method `validate()` should compare the `password` field with the `repeatPassword` field and check if their length is more or equal to 10. If not, throw exception `PasswordValidationException` with the message "Wrong passwords" (since it's our custom logic, let's create our own, **CHECKED**, exception because the user can put any password and our service shouldn't be broken. We should handle the not-valid case and return the understandable message).
Method `validate()` should compare the `password` field with the `repeatPassword`
field and check if their length is more or equal to 10. If not, throw exception
`PasswordValidationException` with the message "Wrong passwords" (since it's our
custom logic, let's create our own, **CHECKED**, exception because the user can put
any password and our service shouldn't be broken. We should handle the not-valid case
and return the understandable message).

Method `registerUser()` should call the method `validate()` and handle the exception.
If everything is okay, save the user to the database (use method `saveUser()`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package core.basesyntax;

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

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