Skip to content

Commit

Permalink
done task
Browse files Browse the repository at this point in the history
  • Loading branch information
xli1iax committed Sep 5, 2024
1 parent 223a4a7 commit f90a9fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package core.basesyntax;

//write your code here
public class PasswordValidationException extends Exception {
public PasswordValidationException() {
}

public PasswordValidationException(String message) {
super(message);
}
}
10 changes: 9 additions & 1 deletion src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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: 7 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package core.basesyntax;

public class UserService {
public class UserService extends PasswordValidator {
public void registerUser(User user) {
//write your code here
try {
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
4 changes: 2 additions & 2 deletions src/test/java/core/basesyntax/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void afterTest() {
}

@Test
public void registerUser_correctInputData() {
public void registerUser_correctInputData() throws PasswordValidationException {
User user = new User("email@email", "Password#123", "Password#123");
userService.registerUser(user);
String actualMessage = outContent.toString().trim();
Expand All @@ -44,7 +44,7 @@ public void registerUser_correctInputData() {
}

@Test
public void registerUser_incorrectInputData() {
public void registerUser_incorrectInputData() throws PasswordValidationException {
User user = new User("email@email", "123", "123");
userService.registerUser(user);
String actualMessage = outContent.toString().trim();
Expand Down

0 comments on commit f90a9fe

Please sign in to comment.