How to leave default value in Static Arraylist in Java?

Asked

Viewed 72 times

1

I am making a program with login screen and have a question: I want settar an "administrator" account in a ArrayList static and do not know how to proceed (or if this would be something plausible). I don’t want to change the administrator account and therefore can be a value inserted in a "constructor". The problem I came across is this: O ArrayList, because it is static, it is used via called class, and not object, so there is no way to have a constructor.

package control;

import internal.Login;
import java.util.List;
import java.util.ArrayList;

public class LoginControlador {
    public static String ADMIN_USER = "administrador"; // QUERO EVITAR ISSO
    public static String ADMIN_PASS = "senha"; // E ISSO
    private static List <Login> users = new ArrayList<>(); // QUERO MANTER UM VALOR PADRÃO AQUI DENTRO

    public static void cadastrarUsuario(String username, String password){
        Login novoUsuario = new Login(username, password);
        users.add(novoUsuario);
    }

    public static boolean fazerLogin(String username, String password){
        for (Login user : users){
            if (user.getUsername().equals(username) && user.getPassword().equals(password))
                return true;
        }
        return false;
    }
}

Some solution?

1 answer

1


  • I’m going into this, I didn’t know about this "builder". This code is for professional exercise only, as I am starting studies on the organization of classes and rules for safety and encapsulation now. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.