From 926a3347d3bacdf5f39f15279ca4ab77d5e0db9f Mon Sep 17 00:00:00 2001 From: Petro Shchur Date: Sun, 30 Jun 2024 22:54:02 -0400 Subject: [PATCH 1/4] Add new Method --- src/main/java/mate/academy/Main.java | 4 +++- src/main/java/mate/academy/model/User.java | 8 ++++++++ .../academy/service/AuthenticationService.java | 17 +++++++++++++++-- .../java/mate/academy/service/UserService.java | 5 +++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 9c1dcdecf..aad600c1b 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,9 +1,11 @@ package mate.academy; import mate.academy.service.AuthenticationService; +import mate.academy.service.UserService; public class Main { - private static final AuthenticationService authenticationService = new AuthenticationService(); + private static final AuthenticationService authenticationService = + new AuthenticationService(new UserService()); public static void main(String[] args) { test("bob@i.ua", "1234", true); diff --git a/src/main/java/mate/academy/model/User.java b/src/main/java/mate/academy/model/User.java index 22dc6ff8b..bc5c7080f 100644 --- a/src/main/java/mate/academy/model/User.java +++ b/src/main/java/mate/academy/model/User.java @@ -13,7 +13,15 @@ public String getEmail() { return email; } + public void setEmail(String email) { + this.email = email; + } + public String getPassword() { return password; } + + public void setPassword(String password) { + this.password = password; + } } diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..3dec9bf79 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,16 +1,29 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + private final UserService userService; + + public AuthenticationService(UserService userService) { + this.userService = 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) { - 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 1652d7d68..94210665c 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -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 9f77e4d5f61429ffde0060384a108834747dc7c4 Mon Sep 17 00:00:00 2001 From: Petro Shchur Date: Mon, 1 Jul 2024 08:41:59 -0400 Subject: [PATCH 2/4] Fixed mistake --- src/main/java/mate/academy/model/User.java | 8 -------- .../java/mate/academy/service/AuthenticationService.java | 3 ++- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/mate/academy/model/User.java b/src/main/java/mate/academy/model/User.java index bc5c7080f..22dc6ff8b 100644 --- a/src/main/java/mate/academy/model/User.java +++ b/src/main/java/mate/academy/model/User.java @@ -13,15 +13,7 @@ public String getEmail() { return email; } - public void setEmail(String email) { - this.email = email; - } - public String getPassword() { return password; } - - public void setPassword(String password) { - this.password = password; - } } diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 3dec9bf79..4ec688d0a 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -8,6 +8,7 @@ public class AuthenticationService { public AuthenticationService(UserService userService) { this.userService = userService; } + /** * Imagine that some user wants to login to your site. * You should check if user credentials (login and password) are valid or not. @@ -24,6 +25,6 @@ public boolean login(String email, String password) { if (user == null) { return false; } - return user.getPassword().equals(password); + return user != null && user.getPassword().equals(password); } } From 8f04dbe8346879f9dab655328d2bf529fd77d632 Mon Sep 17 00:00:00 2001 From: Petro Shchur Date: Tue, 9 Jul 2024 09:18:55 -0400 Subject: [PATCH 3/4] Fixed --- src/main/java/mate/academy/service/AuthenticationService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 4ec688d0a..1385681c7 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -13,7 +13,7 @@ public AuthenticationService(UserService 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. - * + * add * @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. From b28b3214266cf2d77b659cce22502aa9a3b44213 Mon Sep 17 00:00:00 2001 From: Petro Shchur Date: Tue, 9 Jul 2024 09:24:14 -0400 Subject: [PATCH 4/4] Fixed --- src/main/java/mate/academy/service/AuthenticationService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 1385681c7..1d4ee19c6 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -23,6 +23,7 @@ public AuthenticationService(UserService userService) { public boolean login(String email, String password) { User user = userService.findByEmail(email); if (user == null) { + return false; } return user != null && user.getPassword().equals(password);