generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 945
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Povch Anastasia
committed
Dec 2, 2024
1 parent
1217f7a
commit abb1ec0
Showing
4 changed files
with
46 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 15 additions & 10 deletions
25
src/main/java/mate/academy/service/AuthenticationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
@@ -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; | ||
} | ||
} |