From 3a6e430a7b7dc7bad17229dc1c93d7ac2c4da0a1 Mon Sep 17 00:00:00 2001 From: Serhii Pervii Date: Tue, 23 Jul 2024 18:50:36 +0300 Subject: [PATCH 1/3] Fix AuthenticationService and UserService implementation - Implement findByEmail method in UserService to search for a user by email from the static array of users. - Correct the login method in AuthenticationService to use UserService for email lookup and validate the provided password. - Remove duplicate login method in AuthenticationService. - Ensure correct logic for user authentication based on email and password. This fix ensures the correct functioning of the login process, and the main method now passes all test cases as expected. --- .../java/mate/academy/service/AuthenticationService.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..b41163a53 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,6 +1,10 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + private final UserService userService = new UserService(); + /** * Imagine that some user wants to login to your site. * You should check if user credentials (login and password) are valid or not. @@ -11,6 +15,7 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { - return false; + User user = userService.findByEmail(email); + return user != null && user.getPassword().equals(password); } } From 2f014679d081e934bac0fb4c712ade26fb8b8013 Mon Sep 17 00:00:00 2001 From: Serhii Pervii Date: Tue, 23 Jul 2024 19:11:31 +0300 Subject: [PATCH 2/3] Fix AuthenticationService and UserService implementation - Implement findByEmail method in UserService to search for a user by email from the static array of users. - Correct the login method in AuthenticationService to use UserService for email lookup and validate the provided password. - Remove duplicate login method in AuthenticationService. - Ensure correct logic for user authentication based on email and password. This fix ensures the correct functioning of the login process, and the main method now passes all test cases as expected. --- .../java/mate/academy/service/AuthenticationService.java | 7 ++++++- src/main/java/mate/academy/service/UserService.java | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..b41163a53 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,6 +1,10 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + private final UserService userService = new UserService(); + /** * Imagine that some user wants to login to your site. * You should check if user credentials (login and password) are valid or not. @@ -11,6 +15,7 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { - return false; + User user = userService.findByEmail(email); + return user != null && user.getPassword().equals(password); } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 1652d7d68..d87f245e9 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -3,7 +3,7 @@ import mate.academy.model.User; public class UserService { - private static final User[] users = new User[] { + private static final User[] users = new User[]{ new User("bob@i.ua", "1234"), new User("alice@i.ua", "1234") }; @@ -15,6 +15,11 @@ public class UserService { * Return null if there is no suitable user */ public User findByEmail(String email) { + for (User user : users) { + if (user.getEmail().equals(email)) { + return user; + } + } return null; } } From 3142704045ec6e4378026f0222f959e3590075fd Mon Sep 17 00:00:00 2001 From: Serhii Pervii Date: Wed, 9 Oct 2024 17:12:27 +0300 Subject: [PATCH 3/3] Merge branch 'hm-2-solution' of github.com:SIPervii/jv-oop-lesson-0 into hm-2-solution # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit. # Delete comment --- .../mate/academy/service/AuthenticationService.java | 10 ---------- src/main/java/mate/academy/service/UserService.java | 7 ------- 2 files changed, 17 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index d2a43bd4c..8524b552d 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -5,16 +5,6 @@ public class AuthenticationService { private final UserService userService = new UserService(); - /** - * 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 UserService 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. - */ public boolean login(String email, String password) { User user = userService.findByEmail(email); return user != null && user.getPassword().equals(password); diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 463c4f9a9..98c20549b 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -8,13 +8,6 @@ public class UserService { new User("alice@i.ua", "1234") }; - /** - * Find user by email. All users are stored in private static final User[] users - * - * @param email - the input parameter - * @return - user if his email is equal to passed email. - * Return null if there is no suitable user - */ public User findByEmail(String email) { for (User user : users) { if (user.getEmail().equals(email)) {