package ArraysBidim;

import java.util.Scanner;

public class Ejercicios_P2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// Cuantos corredores hay.
		// Cada corredor tiene 2 datos de carrera (minutos, segundos)
		// Cada corredor tiene nombre, apellidos, dorsal, pais.

		System.out.println("¿Cuántos corredores participan en la carrera?");
		int numCorredores = sc.nextInt();
		sc.nextLine(); // Cuando se pasa de INT A String hay que vaciar el buffer.

		String[][] datosCorredores = new String[numCorredores][4];
		for (int i = 0; i < datosCorredores.length; i++) {
			System.out.println(" Introduce el nombre del corredor " + i);
			datosCorredores[i][0] = sc.nextLine();

			System.out.println(" Introduce el apellido del corredor " + i);
			datosCorredores[i][1] = sc.nextLine();

			System.out.println(" Introuce el dorsal del corredor " + i);
			datosCorredores[i][2] = sc.nextLine();

			System.out.println(" Introduce el país del corredor " + i);
			datosCorredores[i][3] = sc.nextLine();

		}

		double[][] tiempos = new double[numCorredores][2]; // [Fila] [Columna]
		for (int i = 0; i < tiempos.length; i++) {
			System.out.println("Tiempo corredor Nº " + i);
			for (int j = 0; j < tiempos[i].length; j++) {
				if (j == 0) {
					System.out.println("Introduce el minuto: ");
				} else {
					System.out.println("Introduce el segundo: ");
				}
				tiempos[i][j] = sc.nextDouble();
			}
		}

		for (int i = 0; i < datosCorredores.length; i++) {
			System.out.println("Corredor nº " + (i + 1));

			System.out.println("→ Nombre: " + datosCorredores[i][0]);
			System.out.println("--> Apellido: " + datosCorredores[i][1]);
			System.out.println("--> Dorsal: " + datosCorredores[i][2]);
			System.out.println("--> País: " + datosCorredores[i][3]);

			System.out.println("Tiempo minutos: " + tiempos[i][0]);
			System.out.println("Tiempo segundos: " + tiempos[i][1]);

		}

		// Me podrías dar los datos del corredor que ha obtenido el mejor tiempo.

		// Voy a recorrer todos los tiempos y ver cual es el menor

		double segundosMinimos = 1000000; // Número para que el primer lo sobreescriba
		int indiceElegidoMejorTiempo = 0;

		for (int i = 0; i < tiempos.length; i++) {
			double segundosTotales = tiempos[i][0] * 60 + tiempos[i][1];

			if (segundosTotales < segundosMinimos) {
				segundosMinimos = segundosTotales;
				indiceElegidoMejorTiempo = i;
			}

		}

		System.out.println("PERSONA CON EL MEJOR TIEMPO");
		System.out.println("--> Nombre: " + datosCorredores[indiceElegidoMejorTiempo][0]);
		System.out.println("--> Apellido: " + datosCorredores[indiceElegidoMejorTiempo][1]);
		System.out.println("--> Dorsal: " + datosCorredores[indiceElegidoMejorTiempo][2]);
		System.out.println("--> País: " + datosCorredores[indiceElegidoMejorTiempo][3]);

		// El PEOR TIEMPO.

		double tiempoMaximo = 0;
		int indiceElegidoPeorTiempo = 0;
		for (int i = 0; i < tiempos.length; i++) {
			double tiempoTotal = tiempos[i][0] * 60 + tiempos[i][1];
			if (tiempoTotal > tiempoMaximo) {
				tiempoMaximo=tiempoTotal;
				indiceElegidoPeorTiempo=i;
			}
		}
		
		System.out.println("PERSONA CON EL PEOR TIEMPO");
		System.out.println("--> Nombre: " + datosCorredores[indiceElegidoPeorTiempo][0]);
		System.out.println("--> Apellido: " + datosCorredores[indiceElegidoPeorTiempo][1]);
		System.out.println("--> Dorsal: " + datosCorredores[indiceElegidoPeorTiempo][2]);
		System.out.println("--> País: " + datosCorredores[indiceElegidoPeorTiempo][3]);

	}
}
