Compare two cars (Arrays) by entering their identifiers by scanner

0

How could I get the car with the highest number of kilometers asking the user to enter two identifiers of the cars?

public class Coches {

    int identificador;
    int kilometros;

    public Coches(){}

    public Coches(int identificador, int kilometros) {
        this.identificador = identificador;
        this.kilometros = kilometros;
    }

    public int getIdentificador(){
        return identificador;
    }

    public int getKilometros(){
        return kilometros;
    }
}

public class Pruebas{

     public static void main(String[] args) {

         Scanner teclado = new Scanner(System.in);

         Coches coche = new Coches();

         // Se almacenan un numero máximo de coches (4).

         Coches array [] = new Coches [4];

         int contador = 0;

         while (contador < array.length) {

             System.out.println("Introduce el identificador del coche : ");

             int identificador;
             int kilometros;

             System.out.print("Identificador :");
             identificador = teclado.nextInt();

             System.out.print("Kilometros :");
             kilometros = teclado.nextInt();

             coche = new Coches(identificador,kilometros);

             array [contador] = coche;
             contador++;

             System.out.print("Coche dado de alta");
             System.out.println();
        }

        System.out.print("Se ha alcanzado el maximo de coches.\n");

        //Busca cual realizo mayor kilometraje:
        int maxKilometros = 0;
        int indiceCoche = 0;

        for(int i = 0; i < array.length; i++){                  
            if(array[i].getKilometros() > maxKilometros){
                maxKilometros = array[i].getKilometros();
                indiceCoche = i;
            }                   
        }

        System.out.print("El coche que ha recorrido más kilometros es:"  + array[indiceCoche].getIdentificador() + " Kilometros: "+ array[indiceCoche].getKilometros() +"\n");
    }
}

I need in the last part to enter two identifiers by keyboard, and once introduced the compare and tell me which is the car with greater autonomy.

// Comparar dos coches introduciendo sus identificadores.

System.out.print("Introduzca el primer identificador :");
System.out.print("Introduzca el segundo identificador :");

How could it be done?

Thank you, Regards.

Thanks to Jorgesys for the answer in the previous thread.

    
asked by InThaHouse 13.01.2018 в 13:40
source

1 answer

1

You only have to read the identifier by keyboard

int idcoche1 = teclado.nextInt(),
    idcoche2 = teclado.nextInt(),
    indiceCoche1 = -1,
    indiceCoche2 = -1;

then you use the same cycle that you already have:

for(int i = 0; i< array.length; i++){                  
    if(array[i].getIdentificador() == idcoche1){
        indiceCoche1 = i;
    }
    if(array[i].getIdentificador() == idcoche2){
        indiceCoche2 = i;
    }               
}

then you compare the cars:

if (array[indiceCoche1].getKilometros() > array[indiceCoche2].getKilometros(indiceCoche2)){
    System.out.print("El coche 1 tiene mas autonomia");
} else {
    System.out.print("El coche 2 tiene mas autonomia");
}

Remember to make the corresponding validations at the user's entrance (this is an example):

int coche = -1;
do {
    try {
        coche = teclado.nextInt();
    } catch (InputMismatchException e) {
        System.out.print("HAs ingresado algo mal");
    }
} while (coche > 1);

You can see more about validation in this link: EL LINK

Disclaimer: I'm sorry if there are any errors, but I do not have any IDEs at hand, therefore none of this is proven.

    
answered by 13.01.2018 / 14:16
source