"break" inside a chained if-lse

Asked

Viewed 1,018 times

2

I’m new here and I have a very simple question: is there some kind of "break" so that it can be used within a if-else chained?

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);
}

Example: I have balance of 200000 (Platinum category). If I remove 160000, I should stay in the GOLD category because I cannot "fall" twice

  • 1

    What you want to achieve with this "break"?

  • So, I need to set a status code public void retirada(double valor) {&#xA; if (valor > getSaldo()) {&#xA; System.out.println("Valor informado maior que o saldo");&#xA; } else {&#xA; setSaldo(saldo - valor);&#xA; if (getSaldo() < 50000) {&#xA; setCategoria(SILVER);&#xA; } else if (getSaldo() < 200000) {&#xA; setCategoria(GOLD);&#xA; } else {&#xA; setCategoria(PLATINUM);&#xA; }&#xA; }&#xA; }code

  • I could not read the code in your comment, it was very messy. Update the question with this new information

  • You can use the command itself break, just need to understand if it’s really necessary.

  • edited the question :)

  • 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.

Show 1 more comment

1 answer

3

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);
}
  • Actually, it’s a bank account, and I already have this function that you mentioned, and this method that I outlined, it has to be void. I just wanted to know if there is any kind of break in an if-It started, but, thank you very much, your codes have already given me other ideas :)

Browser other questions tagged

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