Skip to content

Commit

Permalink
processed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Povch Anastasia committed Dec 2, 2024
1 parent 1217f7a commit abb1ec0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
21 changes: 17 additions & 4 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package mate.academy;

import mate.academy.model.User;
import mate.academy.service.AuthenticationService;
import mate.academy.service.UserService;

import java.util.Arrays;
import java.util.List;

public class Main {
private static final AuthenticationService authenticationService = new AuthenticationService();
private static final AuthenticationService authenticationService;

static {
List<User> users = Arrays.asList(
new User("[email protected]", "1234"),
new User("[email protected]", "1234"),
new User("[email protected]", "4321")
);
UserService userService = new UserService(users);
authenticationService = new AuthenticationService(userService);
}
public static void main(String[] args) {
test("[email protected]", "1234", true);
test("[email protected]", "1234", true);
Expand All @@ -17,8 +30,8 @@ private static void test(String email, String password, boolean expected) {
if (expected == actual) {
System.out.println("Test passed for email: " + email + " and password " + password);
} else {

System.out.println("Test passed for email: " + email + " and password " + password);
System.out.print("Expected to receive " + expected + ", but was " + actual + ". ");
System.out.println("Email: " + email + ", password " + password);
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/mate/academy/model/User.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mate.academy.model;

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

public User(String email, String password) {
this.email = email;
Expand All @@ -14,6 +14,7 @@ public String getEmail() {
}

public String getPassword() {

return password;
}
}
25 changes: 15 additions & 10 deletions src/main/java/mate/academy/service/AuthenticationService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package mate.academy.service;

import mate.academy.model.User;

public class AuthenticationService {
/**
* Imagine that some user wants to login to your site.
* You should check if user credentials (login and password) are valid or not.
* All users are stored in <code>UserService</code> class.
* @param email - user's email
* @param password - user's password
* @return true if user by email exists and passed password is equal to user's password.
* Return false in any other cases.
*/
private final UserService userService;

public AuthenticationService(UserService userService) {
this.userService = userService;
}

public boolean login(String email, String password) {
User user = userService.findByEmail(email);

if (user != null && user.getPassword().equals(password)) {
return true;
}

return false;
}
}
}
15 changes: 11 additions & 4 deletions src/main/java/mate/academy/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package mate.academy.service;

import mate.academy.model.User;
import java.util.List;

public class UserService {
private static final User[] users = new User[] {
new User("[email protected]", "1234"),
new User("[email protected]", "1234")
};
private final List<User> users;

public UserService(List<User> users) {
this.users = users;
}

/**
* Find user by email. All users are stored in <code>private static final User[] users</code>
Expand All @@ -15,6 +17,11 @@ public class UserService {
* Return <code>null</code> if there is no suitable user
*/
public User findByEmail(String email) {
for (User user : users) {
if (user.getEmail().equals(email)) {
return user;
}
}
return null;
}
}

0 comments on commit abb1ec0

Please sign in to comment.