From 98fd43781853153e026a23c6d1890946e3130896 Mon Sep 17 00:00:00 2001 From: Roman Malyarenko Date: Sun, 20 Oct 2024 13:59:35 +0300 Subject: [PATCH 1/3] Implemented a login method in AuthenticationService and findByEmail method in UserService --- src/main/java/mate/academy/Main.java | 1 + src/main/java/mate/academy/model/User.java | 5 +++-- .../mate/academy/service/AuthenticationService.java | 13 +++++++++++-- src/main/java/mate/academy/service/UserService.java | 9 ++++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 9c1dcdec..3a2e0d88 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -3,6 +3,7 @@ import mate.academy.service.AuthenticationService; public class Main { + private static final AuthenticationService authenticationService = new AuthenticationService(); public static void main(String[] args) { diff --git a/src/main/java/mate/academy/model/User.java b/src/main/java/mate/academy/model/User.java index 22dc6ff8..c45761c6 100644 --- a/src/main/java/mate/academy/model/User.java +++ b/src/main/java/mate/academy/model/User.java @@ -1,8 +1,9 @@ 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; diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c97..f3d3f103 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,8 +1,11 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + /** - * Imagine that some user wants to login to your site. + * 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 @@ -10,7 +13,13 @@ public class AuthenticationService { * @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) { - return false; + User user = UserService.findByEmail(email); + + if (user == null) { + return false; + } + return 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 1652d7d6..97cd24f6 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -3,6 +3,7 @@ import mate.academy.model.User; public class UserService { + private static final User[] users = new User[] { new User("bob@i.ua", "1234"), new User("alice@i.ua", "1234") @@ -14,7 +15,13 @@ public class UserService { * @return - user if his email is equal to passed email. * Return null if there is no suitable user */ - public User findByEmail(String email) { + + public static User findByEmail(String email) { + for (User person : users) { + if (person.getEmail().equals(email)) { + return person; + } + } return null; } } From 1dd6f06c033453bd6a8442c29c06efe6e2f29261 Mon Sep 17 00:00:00 2001 From: Roman Malyarenko Date: Sun, 20 Oct 2024 23:57:25 +0300 Subject: [PATCH 2/3] added fixes after review --- src/main/java/mate/academy/Main.java | 1 - src/main/java/mate/academy/model/User.java | 5 ++--- .../mate/academy/service/AuthenticationService.java | 13 +++---------- src/main/java/mate/academy/service/UserService.java | 9 ++++----- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 3a2e0d88..9c1dcdec 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -3,7 +3,6 @@ import mate.academy.service.AuthenticationService; public class Main { - private static final AuthenticationService authenticationService = new AuthenticationService(); public static void main(String[] args) { diff --git a/src/main/java/mate/academy/model/User.java b/src/main/java/mate/academy/model/User.java index c45761c6..22dc6ff8 100644 --- a/src/main/java/mate/academy/model/User.java +++ b/src/main/java/mate/academy/model/User.java @@ -1,9 +1,8 @@ package mate.academy.model; public class User { - - private final String email; - private final String password; + private String email; + private String password; public User(String email, String password) { this.email = email; diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index f3d3f103..16d176d3 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,9 +1,6 @@ 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. @@ -13,13 +10,9 @@ public class AuthenticationService { * @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); - - if (user == null) { - return false; - } - return user.getPassword().equals(password); + UserService userService = new UserService(); + return userService.findByEmail(email) != null ? userService.findByEmail(email) + .getPassword().equals(password) : false; } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 97cd24f6..d19b7afe 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -15,11 +15,10 @@ public class UserService { * @return - user if his email is equal to passed email. * Return null if there is no suitable user */ - - public static User findByEmail(String email) { - for (User person : users) { - if (person.getEmail().equals(email)) { - return person; + public User findByEmail(String email) { + for (User user : users) { + if (user.getEmail().equals(email)) { + return user; } } return null; From 369b57bacd9422231adaa7f615b2895b32a77b21 Mon Sep 17 00:00:00 2001 From: Roman Malyarenko Date: Mon, 21 Oct 2024 12:16:47 +0300 Subject: [PATCH 3/3] added fixes after review again --- .../java/mate/academy/service/AuthenticationService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 16d176d3..bd27cd2f 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,8 +1,10 @@ package mate.academy.service; public class AuthenticationService { + private UserService userService = new UserService(); + /** - * Imagine that some user wants to //login to your site. + * 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 @@ -11,7 +13,6 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { - UserService userService = new UserService(); return userService.findByEmail(email) != null ? userService.findByEmail(email) .getPassword().equals(password) : false; }