Doubts about logic, ifs with multiple checks

1

I have a code where I need to check if two entries are true.

I'm doing this:

if (!estudante.getOutTotal.equals("0") && !estudante.getOutEmAtendimento.equals.("0")) {
    // Faz algo...
    itens.add(estudante);
}

If the totals of Outbound and Outbound are values "0" I do not want to add to my list now in this situation it could happen that OutOfAssembly is "0" but outTotal is "50" ... in this case it would be necessary to add in the list but this logic does not do this.

I did this:

if (!estudante.getOutTotal.equals("0") && !estudante.getOutEmAtendimento.equals.("0")) {
    // Faz algo...
    itens.add(estudante);
} else if (!estudante.getOutTotal.equals("0") || !estudante.getOutEmAtendimento.equals.("0")) {
    // Faz algo...
    itens.add(estudante);
}

So not to delete all of them I check whether or one or the other is not zero if I do not add to the list.

Is that right? Is there any better way to do it?

At first I made a basic improvement and it worked as I expected.

Follows:

if (!estudante1.equals("0") && !estudante2.equals("0")) {
  itens.add(estudante1);
  itens.add(estudante2);
  System.out.println("1 if");

} else if (!estudante1.equals("0") || !estudante2.equals("0")) {
  itens.add(estudante1);
  System.out.println("2 if");
  itens.add(estudante2);

}
    
asked by anonymous 29.03.2017 / 04:32

1 answer

1

My opinion. Succinctly, that would suffice:

if (!"0".equals(estudante1.getOutTotal())){
  itens.add(estudante1);
}
if (!"0".equals(estudante2.getOutTotal())){
  itens.add(estudante2);
}

And if only one of the conditions was enough to be added in both listings:

if (!"0".equals(estudante1.getOutTotal())){
  itens.add(estudante1);
  itens.add(estudante2);
}
if (!"0".equals(estudante2.getOutTotal())){
  itens.add(estudante1);
  itens.add(estudante2);
}
    
29.03.2017 / 05:35