Scan an Arraylist of objects and check an attribute of an object passed as parameter with an Arraylist

Asked

Viewed 293 times

3

I am a beginner in Java and my teacher passed a list of exercises to practice, in a question I need to create a system that registers a user only if his email has not been registered before.

A part of the User class is like this (it is abstract because there are several types of user):

public abstract class Usuario {
    private String email;
    private String nomeCompleto;
    private LocalDate dataNascimento;    

    public Usuario(String email, String nomeCompleto, LocalDate dataNascimento) {
        this.email = email;
        this.nomeCompleto = nomeCompleto;
        this.dataNascimento = dataNascimento;
    }

    public String getEmail() {
        return email;
    }

I created a repository to "register" users in an Arraylist, which was like this:

public class RepositorioUsuario {
    private ArrayList<Usuario> usuarios = new ArrayList<>();

    public void cadastrarUsuario(Usuario u) {
        for (Usuario a : this.usuarios) {
            if (u.getEmail().equals(a.getEmail())) {
                usuarios.add(u);
            }
        }
    }

The part that doesn’t work at all is this, specifically the verification.

for (Usuario a : this.usuarios) {
            if (u.getEmail().equals(a.getEmail())) {
                usuarios.add(u);
            }
        }

If I take out the for and if, the user is added smoothly (I created a method to list them).

My question is, because the way I did it’s not working?

1 answer

2


Your loop Scroll through the user list and see if their email matches the email of the user you want to register. As the list starts empty, there is no user, so it doesn’t even enter the for, and so will never register any user.

In fact, it does not make sense to register a user only if there is already another user with a similar email. It’s usually the opposite: if the user’s email to be registered already exists, then I don’t register. Probably what you want is something like this:

public class RepositorioUsuario {

    private ArrayList<Usuario> usuarios = new ArrayList<>();

    public void cadastrarUsuario(Usuario novoUsuario) {
        if (existe(novoUsuario)) {
            System.out.println("Já existe um usuário com o email " + novoUsuario.getEmail() + " cadastrado");
        } else {
            usuarios.add(novoUsuario);
        }
    }

    // verificar se o usuário já existe
    private boolean existe(Usuario novoUsuario) {
        for (Usuario user : this.usuarios) {
            if (user.getEmail().equals(novoUsuario.getEmail())) {
                return true; // já existe usuário com este email
            }
        }
        return false; // se chegou até aqui, é porque o email ainda não existe
    }
}

First you check if there is already a user with the same email. For this you need to go through the entire list, but see that as soon as a user with the same email is found, I can return true (the return true interrupts the loop). If I go through all the users and do not find another one with the same email, then I return false (because I made sure that there is no other user with the same email).

Finally, I add the user to the list only if it does not yet exist.

Browser other questions tagged

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