I'm trying to make sure that when the positions of the array [] are completed, do not let me insert more records

0

Every time I call the method, I reset the counter to zero but I have to declare the variable. In the class that contains this method, declare the attribute:

private Vehiculo[] listaVehiculos;

and I built the object

 public Parking() {//C
    listaVehiculos= new Vehiculo[8];
   }

.

private void insertarCocheArray(Scanner in) {
    int plaza,opcion,contador,libres = 0;   //A
    String nombre;
    boolean lleno= false;
    contador=0;
    while (contador != 8) {


        if (contador==listaVehiculos.length) {

            lleno=true;
        }
        if (lleno) {
            System.out.println("lleno");
        }else {


            System.out.println("Tenemos un garage con "+listaVehiculos.length+" plazas.");//aun estan todas desocupadas


            do {
                System.out.println("Elige en que plaza vas a guardar el coche 1/8 "); 
                plaza=in.nextInt();
            } while (plaza<1 || plaza >8);


            if (listaVehiculos[plaza-1]==null) {
                System.out.println("La plaza "+plaza+" esta disponible");
                System.out.println("Introduce el modelo");
                nombre=in.next();
                listaVehiculos[plaza-1]=new Vehiculo(nombre);
                contador++;

                System.out.println("Seha guardado el "+listaVehiculos[plaza-1]+" en la plaza "+plaza);

            }else {
                System.out.println("La plaza "+plaza+" no esta disponible por que la ocupa el "+listaVehiculos[plaza-1]);
            }

        }

    }
}
    
asked by David Palanco 23.04.2018 в 01:12
source

0 answers