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

checked exception #1295

Open
wants to merge 2 commits 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
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);
}
}
15 changes: 13 additions & 2 deletions 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) {
//write your code here
public void validate(String password, String repeatPassword)
throws PasswordValidationException {
if (password == null || repeatPassword == null) {
throw new PasswordValidationException("Passwords cannot be null");
}

if (password.isEmpty() || repeatPassword.isEmpty()) {
throw new PasswordValidationException("Passwords cannot be empty");
}

if (password.length() < 10 || !password.equals(repeatPassword)) {
throw new PasswordValidationException("Wrong passwords");
}
}
}
30 changes: 7 additions & 23 deletions src/main/java/core/basesyntax/User.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
package core.basesyntax;

public class User {
private String email;
private String password;
private String repeatPassword;
private final String email;
private final String password;
private final String repeatPassword;

public User(String email, String password, String repeatPassword) {
this.email = email;
this.password = password;
this.repeatPassword = repeatPassword;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRepeatPassword() {
return repeatPassword;
}

public void setRepeatPassword(String repeatPassword) {
this.repeatPassword = repeatPassword;
}

@Override
public String toString() {
return "User{"
+ "email='" + email + '\''
+ ", password='" + password + '\''
+ ", repeatPassword='" + repeatPassword + '\''
+ '}';
+ "email='" + email + '\''
+ ", password='" + password + '\''
+ ", repeatPassword='" + repeatPassword + '\''
+ '}';
}
}
9 changes: 8 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package core.basesyntax;

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

public void registerUser(User user) {
//write your code here
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
1 change: 1 addition & 0 deletions src/test/java/core/basesyntax/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
Expand Down
Loading