Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package proyecto_contrareloj;
- import java.util.Arrays;
- import java.util.Scanner;
- /**
- *
- * @author Jako69
- */
- public class Proyecto_Contrareloj {
- public static final int NUMCORREDORES = 20;
- public static int d, t;
- public static int finalizados = 0, peor_tiempo = 0, mejor_tiempo;
- public static void main(String[] args) {
- /* d, t y e son para leer dorsal, tiempo y equipo */
- int opcion;
- String e;
- Scanner teclado = new Scanner(System.in);
- Ciclista corredores[] = new Ciclista[NUMCORREDORES]; // un array de ciclistas
- TiempoDorsal clasificacion[] = new TiempoDorsal[NUMCORREDORES]; // un array de dorsales y tiempos
- /**
- * *** GENERACIÓN CICLISTAS FICTICIOS ****
- */
- // variables para asignar valores
- int _dors;
- String _nomb, _equi, _naci;
- // Arrays de valores ficticios para asignar aleatoriamente
- String[] nombres = {"Ana", "Juana", "Rosa", "Yolanda", "María", "Carmen", "Pepa", "Ramona", "Patricia", "Belén", "Esther", "Flora", "Victoria", "Luisa", "Carla", "Romina"};
- String[] apellidos = {"Pérez", "Martínez", "Rocha", "Alonso", "Perales", "Puig", "De La Fuente", "González", "Iglesias", "Sánchez", "Herrera", "Vaquero", "López", "García"};
- String[] equipos = {"SEGAFREDO", "ASTANA", "SKY", "MOVISTAR", "GROUPAMA", "LOTTO"};
- String[] nacionalidades = {"ES", "PT", "FR", "GB", "IT", "DE", "RU", "FI", "NO"};
- // Rellenamos array de corredores con valores aleatorios
- for (int i = 0; i < corredores.length; i++) {
- _dors = (int) (Math.random() * 99) + 1; // random de 1 a 99 para asignar dorsal (ojo, puede generar dorsales repetidos)
- _nomb = nombres[(int) (Math.random() * nombres.length)] + " " + apellidos[(int) (Math.random() * apellidos.length)];
- _equi = equipos[(int) (Math.random() * equipos.length)];
- _naci = nacionalidades[(int) (Math.random() * nacionalidades.length)];
- corredores[i] = new Ciclista(_dors, _nomb, _equi, _naci); // creamos nuevo objeto ciclista
- clasificacion[i] = new TiempoDorsal();
- }
- do {
- System.out.println("==============================================");
- System.out.println("|| MENÚ ||");
- System.out.println("==============================================");
- System.out.println("|| 1 - Mostrar Corredores Inscritos ||");
- System.out.println("|| 2 - Mostrar Corredores de un Equipo ||");
- System.out.println("|| 3 - Introducir Nuevo Tiempo de Corredor ||");
- System.out.println("|| 4 - Mostrar Clasificación Tiempos ||");
- System.out.println("|| 5 - Mostrar Diferencia de Tiempo ||");
- System.out.println("|| 99 - Salir del Programa ||");
- System.out.println("==============================================");
- opcion = teclado.nextInt();
- switch (opcion) {
- case 1:
- System.out.println(" TABLA DE CORREDORES ");
- System.out.println("=========================");
- mostrarLista(corredores);
- break;
- case 2:
- System.out.println(" CORREDORES POR EQUIPO ");
- System.out.println("=========================");
- System.out.println(" Equipos disponibles:");
- mostrarEquipos(corredores);
- System.out.println("\n Introduzca un equipo:");
- seleccionEquipo(corredores);
- System.out.println("=========================\n");
- break;
- case 3:
- System.out.println(" NUEVO TIEMPO ");
- System.out.println("=========================");
- System.out.println(" Dorsales disponibles:");
- mostrarDorsales(corredores);
- //System.out.println("\n Introduzca un dorsal y su tiempo (en segundos) :");
- introducirTiempo(clasificacion);
- break;
- case 4:
- System.out.println(" CLASIFICACIÓN TIEMPOS ");
- System.out.println("=========================");
- System.out.println("Llegaron " + finalizados + " de " + corredores.length + " corredores");
- System.out.println("");
- System.out.println("| DORSAL | TIEMPO |");
- System.out.println("-------------------------");
- mostrarClasi(clasificacion);
- break;
- case 5:
- System.out.println(" DIFERENCIA CON EL MEJOR ");
- System.out.println("=========================");
- System.out.println(" Dorsales con tiempo:");
- dorsalTiempo(clasificacion);
- System.out.println("\n Introduzca un dorsal:");
- diferenciaTiempo(clasificacion);
- break;
- default:
- System.out.println("** opcion incorrecta ");
- }
- } while (opcion != 99);
- }
- /**
- *
- * @param corredores Mostrar lista de corredores probando un for each
- */
- public static void mostrarLista(Ciclista[] corredores) {
- for (Ciclista c : corredores) {
- c.mostrarDatos();
- }
- }
- /**
- *
- * @param corredores Muestra la lista de los equipos generados
- */
- public static void mostrarEquipos(Ciclista[] corredores) {
- for (int i = 0; i < corredores.length; i++) {
- System.out.print(corredores[i].equipo + " ");
- }
- }
- /**
- *
- * @param corredores Muestra la lista de corredores del equipo seleccionado
- */
- public static void seleccionEquipo(Ciclista[] corredores) {
- Scanner teclado = new Scanner(System.in);
- String e = teclado.next();
- for (int i = 0; i < corredores.length; i++) {
- corredores[i].mostrarDatosPorEquipo(e);
- }
- }
- /**
- *
- * @param corredores Muestra la lista de dorsales generados
- */
- public static void mostrarDorsales(Ciclista[] corredores) {
- for (int i = 0; i < corredores.length; i++) {
- System.out.print(corredores[i].dorsal + " ");
- }
- }
- /**
- *
- * @param clasificacion Almacena el tiempo para el dorsal que introduzcas
- */
- public static void introducirTiempo(TiempoDorsal[] clasificacion) {
- Scanner teclado = new Scanner(System.in);
- System.out.println("Introduce el Dorsal");
- d = teclado.nextInt();
- if (finalizados > 0) {
- for (int i = 0; i < finalizados; i++) {
- if (clasificacion[i].dorsal == d) {
- System.err.println("El dorsal ya está en la base de datos");
- break;
- } else {
- System.out.println("Introduce el tiempoooo joeputa");
- t = teclado.nextInt();
- clasificacion[finalizados].dorsal = d;
- clasificacion[finalizados].tiempo = t;
- finalizados++;
- }
- }
- } else {
- System.out.println("Intoduce el tiempo");
- t = teclado.nextInt();
- clasificacion[finalizados].dorsal = d;
- clasificacion[finalizados].tiempo = t;
- finalizados++;
- }
- // d = teclado.nextInt();
- // t = teclado.nextInt();
- // clasificacion[finalizados].dorsal = d;
- // clasificacion[finalizados].tiempo = t;
- // finalizados++;
- // if (t > peor_tiempo) {
- // peor_tiempo = t;
- // }
- }
- /**
- *
- * @param clasificacion Muestra la clasificación
- */
- public static void mostrarClasi(TiempoDorsal[] clasificacion) {
- for (int i = 0; i < clasificacion.length; i++) {
- if (clasificacion[i].dorsal != 0) {
- System.out.printf("| -%02d- | %-8s |\n", clasificacion[i].dorsal, segToHumanTime(clasificacion[i].tiempo, 0));
- }
- }
- }
- /**
- * Método encontrado en internet para pasar los segundos a formato
- * Hora-Minuto-Segundo
- *
- * @param secs
- * @param format_type
- * @return
- */
- public static String segToHumanTime(int secs, int format_type) {
- String time_str = "";
- int hours, mins;
- hours = (int) secs / 3600;
- secs -= (hours * 3600);
- mins = (int) secs / 60;
- secs -= (mins * 60);
- if (format_type == 0) {
- time_str = String.format("%02d:%02d:%02d", hours, mins, secs);
- } else {
- if (hours > 0) {
- time_str += String.format("%02dh ", hours);
- }
- if (mins > 0) {
- time_str += String.format("%02dm ", mins);
- }
- time_str += String.format("%02ds", secs);
- }
- return time_str;
- }
- public static void dorsalTiempo(TiempoDorsal[] clasificacion) {
- mejor_tiempo = peor_tiempo;
- for (int i = 0; i < clasificacion.length; i++) {
- if (clasificacion[i].dorsal != 0) {
- if (clasificacion[i].tiempo < mejor_tiempo) {
- mejor_tiempo = clasificacion[i].tiempo;
- }
- System.out.print(clasificacion[i].dorsal + " ");
- }
- }
- }
- public static void diferenciaTiempo(TiempoDorsal[] clasificacion) {
- Scanner teclado = new Scanner(System.in);
- d = teclado.nextInt();
- System.out.println();
- for (int i = 0; i < clasificacion.length; i++) {
- if (clasificacion[i].dorsal != 0) {
- if (d == clasificacion[i].dorsal) {
- int dif = clasificacion[i].tiempo - mejor_tiempo;
- if (dif == 0) {
- System.out.println("El corredor -" + d + "- tiene el mejor tiempo!");
- } else {
- System.out.println("El corredor -" + d + "- está a " + segToHumanTime(dif, 1));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement