ATM simulator in Android Studio

Asked

Viewed 67 times

2

I’m setting up a basic ATM simulator in Android Studio, where the user must enter the password, which is already pre-defined, in the case is "Java". If the password is correct, the message "Correct Password" appears, if it is wrong, the message "Incorrect Password" appears on the screen. If the user misses the password 3 times, a "Blocked Card" message will appear, and that’s basically it. But with my code, when the password is correct, the message appears right, already if the password is wrong it just shows "Blocked Card", without going through the message "Incorrect Password", I wanted to know if I did something wrong. Follows the code:

public class MainActivity extends AppCompatActivity {

EditText digito;
TextView res;
Button entrar;
int cont = 0;
String senha = "Java";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    digito = (EditText) findViewById(R.id.editText1);
    entrar = (Button) findViewById(R.id.btnEntrar);
    res = (TextView) findViewById(R.id.res);
}

public void Verificacao(View view) {
    for (int i = 0; i < 4; i++) {
        if (digito.getText().toString().equals(senha)) {
            res.setText("Senha Correta");
            break;

        }else if (i == 3) {
            res.setText("Cartão Bloqueado");
            break;

        } else {
            res.setText("Senha Incorreta");

        }
    }
}

}

1 answer

2


What’s happening is that when you click on the button you call the method Verificacao. When this method is called, it checks 4 times (it is inside a for) for i = 0, 1, 2 and 3.

To achieve correct behavior, you must count in a variable external to the method:

int tentativa = 0;

public void Verificacao(View view) {
    if (digito.getText().toString().equals(senha)) {
        res.setText("Senha Correta");
    } else if (tentativa == 3) {
        res.setText("Cartão Bloqueado");
    } else {
        tentativa++; // Na primeira chamada, tentativa = 1
        res.setText("Senha Incorreta");
    }
}

This way, if the user misses the third time, the message "Blocked Card" will appear. This happens because I started starting from 1 instead of 0, as in your for, and why am I adding only when it misses and the card is not locked.

Tip: It is not good practice to start a method with a capital letter, I recommend renaming it to verificacao or even to verificarSenha, which makes it more explicit what it checks.

Observing: I assumed that your role Verificacao is being called once when the user clicks the button. It is not possible to tell if this really happens or not based on the code you shared (I imagine you have put in XML). If you wish to hitch a onClick the button in the creation of Activity (in the Java code):

entrar.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        verificacao(v)
    } 
});
  • 1

    I referenced the method itself in the onClick XML property. And I just tested your idea and it worked, only the behavior didn’t turn out like I wanted, so I put the++ attempts inside the Else and not inside the if, and it worked correctly. And your hint about the method in uppercase letter I will start to have this habit.

  • In fact, the tentativas++ I was in the wrong place. I already edited the answer. If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

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