Skip to content

Commit

Permalink
The code for checking the validity of email and password has been imp…
Browse files Browse the repository at this point in the history
…roved
  • Loading branch information
har1as committed Feb 22, 2025
1 parent 88df7f4 commit 7afd7d7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static void main(String[] args) {
test("[email protected]", "1234", false);
test("[email protected]", "qwerty", false);
}

private static void test(String email, String password, boolean expected) {
boolean actual = authenticationService.login(email, password);
if (expected == actual) {

if (expected && actual) {
System.out.println("Test passed for email: " + email + " and password " + password);
} else {
System.out.print("Expected to receive " + expected + ", but was " + actual + ". ");
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/mate/academy/service/AuthenticationService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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.
Expand All @@ -11,6 +14,17 @@ public class AuthenticationService {
* Return false in any other cases.
*/
public boolean login(String email, String password) {
return false;
User user = userService.findByEmail(email);

if (user != null) {


if (!user.getPassword().equals(password)) {
return false;
} else {
return true;
}
}
return user != null && user.getPassword().equals(password);
}
}
8 changes: 8 additions & 0 deletions src/main/java/mate/academy/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import mate.academy.model.User;

import java.util.Scanner;

public class UserService {
private static final User[] users = new User[] {
new User("[email protected]", "1234"),
Expand All @@ -14,7 +16,13 @@ public class UserService {
* @return - user if his email is equal to passed email.
* 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;
}
}

0 comments on commit 7afd7d7

Please sign in to comment.