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

First commit #1275

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) throws PasswordValidationException {
User user = new User();
user.setEmail("[email protected]");
user.setPassword("12345678910");
user.setRepeatPassword("12345678910");
UserService service = new UserService();
service.registerUser(user);
}
}
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);
}
}
11 changes: 9 additions & 2 deletions 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) {
//write your code here
public void validate(String password, String repeatPassword)
throws PasswordValidationException {
UserService service2 = new UserService();
User user = new User();
if (!password.equals(repeatPassword) || password.length() <= 10) {
throw new PasswordValidationException("Your passwords are incorrect. Try again.");
} else {
service2.saveUser(user);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/core/basesyntax/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public User(String email, String password, String repeatPassword) {
this.repeatPassword = repeatPassword;
}

public User() {
}

public String getEmail() {
return email;
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package core.basesyntax;

public class UserService {
public void registerUser(User user) {
//write your code here
public void registerUser(User user) throws PasswordValidationException {
PasswordValidator passwordValidator = new PasswordValidator();
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
}

public void saveUser(User user) {
System.out.println("User " + user.toString() + " was saved to database!!!");
User user1 = new User(user.getEmail(), user.getPassword(), user.getRepeatPassword());
System.out.println("User " + user1.toString() + " was saved to database!!!");
}
}
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
Loading