Answer the question
In order to leave comments, you need to log in
Checking access to a resource?
1. Let's create a User class in which we will store information about the user's login, password and age: class User, login, password, email, age;
2. Let's create a UserNotFoundException exception class based on the Exception base class. We will use this exception if the user entered an incorrect username or password:
public class UserNotFoundException extends Exception{
public UserNotFoundException(String message){
super(message);
}
}
public static User[] getUsers() {
User user1 = new User("john", "[email protected]", "pass", 18);
User user2 = new User("james", "[email protected]", "qwerty1234", 22);
return new User[]{user1, user2};
}
public static User getUserByLoginAndPassword(String login, String password) throws UserNotFoundException {
User[] users = getUserList();
for (User user : users) {
}
throw new UserNotFoundException("User not found");
}
public static void validateUser(User user) throws AccessDeniedException{
}
public static void main(String[] args) throws UserNotFoundException, AccessDeniedException{
Scanner scanner = new Scanner(System.in);
System.out.println("Введите логин: ");
String login = scanner.nextLine();
System.out.println("Введите пароль");
String password = scanner.nextLine();
// Проверим логин и пароль
// Вызвать методы валидации пользователя
System.out.println("Доступ представлен");
}
Answer the question
In order to leave comments, you need to log in
Hello,
I will not write code, because you have to learn. But I wrote the principle below
Call the getUser () method and pass the username and password to it. The method returns User
To the validateUser() method we pass our user obtained from the first method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question