There is, it’s called return
.
You can encapsulate your logic in a function:
public Categoria getCategoria(int saldo) {
if (saldo < 50000) {
return SILVER;
} else if (saldo < 200000) {
return GOLD;
} else {
return PLATINUM;
}
}
This assumes that the values of Pokémon versions have already been declared somewhere else. To integrate with the rest of the code:
if (valor > getSaldo()) {
System.out.println("Valor informado maior que o saldo");
} else {
setSaldo(saldo - valor);
}
Categoria pokemon = getCategoria(getSaldo());
setCategoria(pokemon);
Now about not falling more than one category at a time, for that you will need to increase the complexity of your code a little. I don’t know how you’re organizing them, but here’s a suggestion:
Categoria categoriaAtual = getCategoria(getSaldo());
Categoria rebaixamento = getProximoRebaixamento(categoriaAtual); // implemente isto.
if (valor > getSaldo()) {
System.out.println("Valor informado maior que o saldo");
} else {
setSaldo(saldo - valor);
}
Categoria novaCategoria = getCategoria(getSaldo());
if (categoriaAtual != rebaixamento) {
setCategoria(rebaixamento);
}
What you want to achieve with this "
break
"?– Jefferson Quesado
So, I need to set a status
code public void retirada(double valor) {
 if (valor > getSaldo()) {
 System.out.println("Valor informado maior que o saldo");
 } else {
 setSaldo(saldo - valor);
 if (getSaldo() < 50000) {
 setCategoria(SILVER);
 } else if (getSaldo() < 200000) {
 setCategoria(GOLD);
 } else {
 setCategoria(PLATINUM);
 }
 }
 }code
– dcm50
I could not read the code in your comment, it was very messy. Update the question with this new information
– Jefferson Quesado
You can use the command itself
break
, just need to understand if it’s really necessary.– rLinhares
edited the question :)
– dcm50
I don’t understand your doubt, where do you want to put a stop? The way it is, the code should work the way it explained.
– user28595