Scan an ArrayList of objects and check an attribute of an object passed as a parameter with one of the ArrayList [closed]

closed . This question needs details or to be clearer and is not currently accepting answers.

want to improve this question? Add details and make it clearer what problem is being solved by editing this post .

Closed 1 year ago .

improve this question

I am a beginner in Java and my teacher has 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 looks like this (it is abstract since 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 looked 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 verification.

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

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

My question is, why the way I did is not working?

Author: hkotsubo, 2019-10-19

1 answers

Your loop goes through the list of users and checks if their email is the same as the email of the user you want to register. Since the list starts empty, it doesn't have any users, so it doesn't even enter for, and so it will never register any users.

In fact, it makes no sense to register a user only if there is already another user with an equal email. Usually it is the opposite: if the email of the user to be registered already exists, then I do not register. Probably the 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 scroll through the entire list, but see that as soon as a user with the same email is found, I can already Return true (the return true breaks the loop). If I scroll through all users and do not find another with the same email, then I return false (because I guaranteed that there is no other user with the same email).

Finally, I add the user in the list only if it does not already exist.

 2
Author: hkotsubo, 2019-10-19 11:35:24