From bac3b2947a1e69583b6d79a474d2c1d3dc8bffee Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 15 Dec 2024 17:10:39 +0000 Subject: [PATCH 1/6] update --- .../academy/service/AuthenticationService.java | 9 +++++++-- .../java/mate/academy/service/UserService.java | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..e44cc1f6b 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,16 +1,21 @@ package mate.academy.service; -public class AuthenticationService { +public class AuthenticationService extends 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 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) { + if (checkEmailAndPassword(password, email)) { + return true; + } return false; + } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 1652d7d68..6035b1edc 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -3,18 +3,33 @@ 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") }; /** * 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 tempUser : users) { + if (!tempUser.getEmail().equals(email)) { + break; + } + } return null; } + + public boolean checkEmailAndPassword(String password, String email) { + for (User tempUser : users) { + if (tempUser.getPassword().equals(password) && tempUser.getEmail().equals(email)) { + return true; + } + } + return false; + } } From d78093bf0990d46ec6cd9096eadf03179f251beb Mon Sep 17 00:00:00 2001 From: Petro Date: Wed, 18 Dec 2024 01:27:30 +0000 Subject: [PATCH 2/6] correcting mistakes --- .../mate/academy/service/AuthenticationService.java | 8 +++++--- src/main/java/mate/academy/service/UserService.java | 12 ++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index e44cc1f6b..5d4e8732f 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,6 +1,6 @@ package mate.academy.service; -public class AuthenticationService extends UserService { +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. @@ -11,9 +11,11 @@ public class AuthenticationService extends UserService { * @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 service = new UserService(); + public boolean login(String email, String password) { - if (checkEmailAndPassword(password, email)) { - return true; + if (service.findByEmail(email) != null) { + return service.checkingPassword(password) != null; } return false; diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 6035b1edc..70c019c69 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -17,19 +17,19 @@ public class UserService { */ public User findByEmail(String email) { for (User tempUser : users) { - if (!tempUser.getEmail().equals(email)) { - break; + if (tempUser.getEmail().equals(email)) { + return tempUser; } } return null; } - public boolean checkEmailAndPassword(String password, String email) { + public User checkingPassword(String password) { for (User tempUser : users) { - if (tempUser.getPassword().equals(password) && tempUser.getEmail().equals(email)) { - return true; + if (tempUser.getPassword().equals(password)) { + return tempUser; } } - return false; + return null; } } From 88409f0848fd081425f493f65126f47d4c02fd14 Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 22 Dec 2024 00:52:06 +0000 Subject: [PATCH 3/6] correcting mistakes 2.0 --- .../mate/academy/service/AuthenticationService.java | 5 +++-- src/main/java/mate/academy/service/UserService.java | 12 +----------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 5d4e8732f..d56433a78 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,5 +1,7 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { /** * Imagine that some user wants to login to your site. @@ -15,9 +17,8 @@ public class AuthenticationService { public boolean login(String email, String password) { if (service.findByEmail(email) != null) { - return service.checkingPassword(password) != null; + return service.findByEmail(email).getPassword().equals(password); } return false; - } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 70c019c69..40b6ae2f8 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -3,14 +3,13 @@ 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") }; /** * 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 @@ -23,13 +22,4 @@ public User findByEmail(String email) { } return null; } - - public User checkingPassword(String password) { - for (User tempUser : users) { - if (tempUser.getPassword().equals(password)) { - return tempUser; - } - } - return null; - } } From 64506e41906f33cddcac5539b9154e8145c4478e Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 22 Dec 2024 01:07:32 +0000 Subject: [PATCH 4/6] correcting mistakes 3.0 --- src/main/java/mate/academy/service/AuthenticationService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index d56433a78..815c00544 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,7 +1,5 @@ package mate.academy.service; -import mate.academy.model.User; - public class AuthenticationService { /** * Imagine that some user wants to login to your site. From e7bc91a2441c1bc756a1a84b875eea4e6d1999ec Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 22 Dec 2024 12:06:43 +0000 Subject: [PATCH 5/6] final solution --- .../java/mate/academy/service/AuthenticationService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 815c00544..78ab67991 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -14,9 +14,6 @@ public class AuthenticationService { private final UserService service = new UserService(); public boolean login(String email, String password) { - if (service.findByEmail(email) != null) { - return service.findByEmail(email).getPassword().equals(password); - } - return false; + return service.findByEmail(email) != null && service.findByEmail(email).getPassword().equals(password); } } From c721fdedb3260c1895214f0ee3f82dc0893334c3 Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 22 Dec 2024 12:11:35 +0000 Subject: [PATCH 6/6] final solution (fix mvn mistakes) --- src/main/java/mate/academy/service/AuthenticationService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 78ab67991..590ee7bfb 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -14,6 +14,7 @@ public class AuthenticationService { private final UserService service = new UserService(); public boolean login(String email, String password) { - return service.findByEmail(email) != null && service.findByEmail(email).getPassword().equals(password); + return service.findByEmail(email) != null + && service.findByEmail(email).getPassword().equals(password); } }