Implement the Lock class. Then verify:
a. When you enter a correct password, open
b. When you enter an incorrect password, it will not open
c. If you enter the key incorrectly the maximum amount of attempts, you can not open the lock anymore
The problem is that I do not know what to do so that point c) works for me, I can not use for in this exercise, just if. I put the variable amount of Fixed Faults that LaBlock as a counter, so it accumulates in the open method, and then in the method was BLOCKED I put an if and if that variable is equal to 5 it does not open anymore, but it does not work.
public class Cerradura {
//Atributos
private int claveDeApertura = 1234;
private int cantidadDeFallosConsecutivosQueLaBloquean;
private boolean estado = false;
//Constructor
public Cerradura (int claveDeApertura ) {
this.claveDeApertura = claveDeApertura;
this.cantidadDeFallosConsecutivosQueLaBloquean = cantidadDeFallosConsecutivosQueLaBloquean;
}
//Metodos
public boolean abrir(int clave) {
cantidadDeFallosConsecutivosQueLaBloquean++;
if(claveDeApertura == clave) {
estado=true;
System.out.println("Se abre la cerradura.");
}else {
System.out.println("No se puede abrir la cerradura , contraseña incorrecta.");
}
return estado ;
}
public void cerrar () {
System.out.println("Cerradura cerrada.");
}
public boolean fueBloqueda() {
if(cantidadDeFallosConsecutivosQueLaBloquean ==5) {
System.out.println("La cerradura fue bloqueada , ha utilizado la cantida maxima de intentos.");
}
return estado;
}
}
---------------------------
public class PruebaCerradura {
public static void main(String[] args) {
Cerradura c1 = new Cerradura(1234);
c1.abrir(1210);
c1.cerrar();
c1.abrir(1548);
c1.abrir(1548);
c1.abrir(1548);
c1.abrir(1548);
c1.abrir(1548);
}
}