How do I make the lock can not be opened anymore? Java POO

0
  

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

}
    
asked by computer96 30.10.2018 в 23:02
source

2 answers

1

1) In the constructor, the variable quantity of FixedFaults thatLaBlock must start at 0 (and try to give it a shorter name hahahaha).

2) The conditional that evaluates if that variable reached the maximum, should be given in the method that tries to open the lock, which by the way should be a void, and not return a boolean. That is, this:

public void abrir(int clave){
       if(cantidadDeFallosConsecutivosQueLaBloquean<5){//primero se verifica que el numero de intentos no se haya excedido
               if(clave==claveDeApertura){//Si no se excedio, se evalua si la clave es correcta
                 System.out.println("Contraseña correcta, cerradura desbloqueada");
                }else{
                  System.out.println("Contraseña incorrecta, intente nuevamente");
                 }
       }else{
           System.out.println("Se ha alcanzado el numero maximo de intentos, cerradura bloqueada permanentemente");
         }
       cantidadDeFallosConsecutivosQueLaBloquean++; //Finalmente, aumentamos la variable que cuenta los intentos      
    
answered by 31.10.2018 в 00:50
0

You can use a static property of type integer in the Lock class. Initialize to 0 and add one each failed attempt to this property. Check its value before allowing to open the lock , once the key has been validated. Reached the maximum number should prevent it from opening.

    
answered by 30.10.2018 в 23:07