Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i verify if i'm adding atributes to a list that are equal?


import entidades.*;

public class Main {

    public static void main(String[] args) {
        Profissional prof = new Profissional(null, null);
        List<Profissional> profissional = new ArrayList<Profissional>();
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while(loop == true) {
            String comando = sc.next().toUpperCase();

            if (comando.contentEquals("RP")) {
                String nomePro = sc.nextLine();
                String categoriaPro = sc.nextLine();
                prof.NomeVerificacao(profissional, nomePro, categoriaPro);
            }
            if(comando.contentEquals("SAIR")) {
                break;
            }
        }

        for(Profissional pro : profissional) {
            System.out.println(pro);

This is my Main, it's running fine but i don´t think it is adding the atributes to the list and not verifying either.

i want to add the atributes to a list so i can create different objets but they can not have at least the name equal.

public class Profissional {
    private String nome;
    private String categoria;

    public Profissional(String nome, String categoria) {
        this.nome = nome;
        this.categoria = categoria;
    }


        public void NomeVerificacao(List<Profissional> profissional ,String nome, String categoria) {
        if(profissional.isEmpty() == true) {
            profissional.add(new Profissional(nome, categoria));
        }else {
            for(Profissional pro : profissional) {
                if(pro.nome.contentEquals(nome)) {
                    System.out.println("Já Exite esse nome");

                }else {
                    profissional.add(new Profissional(nome, categoria));    
                }
            }
        }
    }

    @Override
    public String toString() {
        return "nome=" + nome + ", categoria=" + categoria;
    }


}

this is the Profissional Class.

I'm almost there i think but the output keeps saying that the name exists even though it is the first name i'm inserting.

like image 889
Filipe Mota Avatar asked Nov 19 '25 12:11

Filipe Mota


1 Answers

I ran your code on my machine and made 3 changes into it, and it's working for me now,

1)

String nomePro = sc.next(); String categoriaPro = sc.next();

2) In professional class just changed this function a bit:

public void NomeVerificacao(List<Profissional> profissional, String nome, String categoria) {
    if (profissional.isEmpty() == true) {
        profissional.add(new Profissional(nome, categoria));
    } else {
        int i = 0;
        for (; i < profissional.size(); i++) {
            if (profissional.get(i).nome.equals(nome)) {
                System.out.println("Já Exite esse nome");
                break;
            }
        }
        if (i == profissional.size()) {
            profissional.add(new Profissional(nome, categoria));
        }
    }
}

3) At the end of the class Main, wrote sc.close(); to close the scanner.

i/p and o/p : 
1) RP
   red
   color
2) RP
   orange
   color
3) RP
   orange
   paint
   Já Exite esse nome

4) SAIR
   nome=red, categoria=color
   nome=orange, categoria=color

As you can see in above i/p and o/p, nome=red and nome=orange with categoria=color are added in the list but when we tried to add the same nome=orange again but with different category as paint it didn't add it and printed the message "Já Exite esse nome". and after entering SAIR, the toString(); printed the list content at the end. So the message will be printed only if we try to add the object with the same name again int list (not the first or any other times).

Further optimizations are possible but for now, it will work!

like image 91
amey Avatar answered Nov 22 '25 01:11

amey