A
A
Amir Kenesbay2021-01-31 23:24:16
Java
Amir Kenesbay, 2021-01-31 23:24:16

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);
    }
}


3. In the same way, we will create an exception class AccessDeniedException
4. Let's create the Main class, in which we will create the getUsers method, this method should return a list of pre-created users:
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};
}


5. Let's create the getUserByLoginAndPassword() method in the Main class;
public static User getUserByLoginAndPassword(String login, String password) throws UserNotFoundException {
        User[] users = getUserList();
        for (User user : users) {

        }
        throw new UserNotFoundException("User not found");

}


6. Let's create another validateUser method for the Main class to check the user's age. If the age is less than 18, then the method should throw an AccessDeniedException
public static void validateUser(User user) throws AccessDeniedException{
    }


7. Add the last method in the Main class to run the program
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

1 answer(s)
O
Orkhan, 2021-02-01
Hasanly @azerphoenix

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 question

Ask a Question

731 491 924 answers to any question