Advertisement
Guest User

Contrareloj

a guest
Feb 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. package proyecto_contrareloj;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  *
  8.  * @author Jako69
  9.  */
  10. public class Proyecto_Contrareloj {
  11.  
  12.     public static final int NUMCORREDORES = 20;
  13.     public static int d, t;
  14.     public static int finalizados = 0, peor_tiempo = 0, mejor_tiempo;
  15.  
  16.     public static void main(String[] args) {
  17.         /* d, t y e son para leer dorsal, tiempo y equipo */
  18.         int opcion;
  19.         String e;
  20.         Scanner teclado = new Scanner(System.in);
  21.         Ciclista corredores[] = new Ciclista[NUMCORREDORES];   // un array de ciclistas
  22.         TiempoDorsal clasificacion[] = new TiempoDorsal[NUMCORREDORES];   // un array de dorsales y tiempos
  23.  
  24.         /**
  25.          * *** GENERACIÓN CICLISTAS FICTICIOS ****
  26.          */
  27.         // variables para asignar valores
  28.         int _dors;
  29.         String _nomb, _equi, _naci;
  30.         // Arrays de valores ficticios para asignar aleatoriamente
  31.         String[] nombres = {"Ana", "Juana", "Rosa", "Yolanda", "María", "Carmen", "Pepa", "Ramona", "Patricia", "Belén", "Esther", "Flora", "Victoria", "Luisa", "Carla", "Romina"};
  32.         String[] apellidos = {"Pérez", "Martínez", "Rocha", "Alonso", "Perales", "Puig", "De La Fuente", "González", "Iglesias", "Sánchez", "Herrera", "Vaquero", "López", "García"};
  33.         String[] equipos = {"SEGAFREDO", "ASTANA", "SKY", "MOVISTAR", "GROUPAMA", "LOTTO"};
  34.         String[] nacionalidades = {"ES", "PT", "FR", "GB", "IT", "DE", "RU", "FI", "NO"};
  35.  
  36.         // Rellenamos array de corredores con valores aleatorios
  37.         for (int i = 0; i < corredores.length; i++) {
  38.             _dors = (int) (Math.random() * 99) + 1; // random de 1 a 99 para asignar dorsal (ojo, puede generar dorsales repetidos)
  39.             _nomb = nombres[(int) (Math.random() * nombres.length)] + " " + apellidos[(int) (Math.random() * apellidos.length)];
  40.             _equi = equipos[(int) (Math.random() * equipos.length)];
  41.             _naci = nacionalidades[(int) (Math.random() * nacionalidades.length)];
  42.  
  43.             corredores[i] = new Ciclista(_dors, _nomb, _equi, _naci); // creamos nuevo objeto ciclista
  44.             clasificacion[i] = new TiempoDorsal();
  45.         }
  46.         do {
  47.             System.out.println("==============================================");
  48.             System.out.println("||                   MENÚ                   ||");
  49.             System.out.println("==============================================");
  50.             System.out.println("|| 1  - Mostrar Corredores Inscritos        ||");
  51.             System.out.println("|| 2  - Mostrar Corredores de un Equipo     ||");
  52.             System.out.println("|| 3  - Introducir Nuevo Tiempo de Corredor ||");
  53.             System.out.println("|| 4  - Mostrar Clasificación Tiempos       ||");
  54.             System.out.println("|| 5  - Mostrar Diferencia de Tiempo        ||");
  55.             System.out.println("|| 99 - Salir del Programa                  ||");
  56.             System.out.println("==============================================");
  57.             opcion = teclado.nextInt();
  58.  
  59.             switch (opcion) {
  60.                 case 1:
  61.                     System.out.println("   TABLA DE CORREDORES   ");
  62.                     System.out.println("=========================");
  63.                     mostrarLista(corredores);
  64.                     break;
  65.                 case 2:
  66.                     System.out.println("  CORREDORES POR EQUIPO  ");
  67.                     System.out.println("=========================");
  68.                     System.out.println(" Equipos disponibles:");
  69.                     mostrarEquipos(corredores);
  70.                     System.out.println("\n Introduzca un equipo:");
  71.                     seleccionEquipo(corredores);
  72.                     System.out.println("=========================\n");
  73.                     break;
  74.                 case 3:
  75.                     System.out.println("       NUEVO TIEMPO      ");
  76.                     System.out.println("=========================");
  77.                     System.out.println(" Dorsales disponibles:");
  78.                     mostrarDorsales(corredores);
  79.                     //System.out.println("\n Introduzca un dorsal y su tiempo (en segundos) :");
  80.                     introducirTiempo(clasificacion);
  81.  
  82.                     break;
  83.                 case 4:
  84.                     System.out.println("  CLASIFICACIÓN TIEMPOS  ");
  85.                     System.out.println("=========================");
  86.                     System.out.println("Llegaron " + finalizados + " de " + corredores.length + " corredores");
  87.                     System.out.println("");
  88.                     System.out.println("|  DORSAL  |   TIEMPO   |");
  89.                     System.out.println("-------------------------");
  90.                     mostrarClasi(clasificacion);
  91.                     break;
  92.                 case 5:
  93.                     System.out.println(" DIFERENCIA CON EL MEJOR ");
  94.                     System.out.println("=========================");
  95.                     System.out.println(" Dorsales con tiempo:");
  96.                     dorsalTiempo(clasificacion);
  97.                     System.out.println("\n Introduzca un dorsal:");
  98.                     diferenciaTiempo(clasificacion);
  99.                     break;
  100.                 default:
  101.                     System.out.println("** opcion incorrecta  ");
  102.             }
  103.         } while (opcion != 99);
  104.  
  105.     }
  106.  
  107.     /**
  108.      *
  109.      * @param corredores Mostrar lista de corredores probando un for each
  110.      */
  111.     public static void mostrarLista(Ciclista[] corredores) {
  112.         for (Ciclista c : corredores) {
  113.             c.mostrarDatos();
  114.         }
  115.     }
  116.  
  117.     /**
  118.      *
  119.      * @param corredores Muestra la lista de los equipos generados
  120.      */
  121.     public static void mostrarEquipos(Ciclista[] corredores) {
  122.         for (int i = 0; i < corredores.length; i++) {
  123.             System.out.print(corredores[i].equipo + " ");
  124.         }
  125.     }
  126.  
  127.     /**
  128.      *
  129.      * @param corredores Muestra la lista de corredores del equipo seleccionado
  130.      */
  131.     public static void seleccionEquipo(Ciclista[] corredores) {
  132.         Scanner teclado = new Scanner(System.in);
  133.         String e = teclado.next();
  134.         for (int i = 0; i < corredores.length; i++) {
  135.             corredores[i].mostrarDatosPorEquipo(e);
  136.         }
  137.     }
  138.  
  139.     /**
  140.      *
  141.      * @param corredores Muestra la lista de dorsales generados
  142.      */
  143.     public static void mostrarDorsales(Ciclista[] corredores) {
  144.         for (int i = 0; i < corredores.length; i++) {
  145.             System.out.print(corredores[i].dorsal + " ");
  146.         }
  147.     }
  148.  
  149.     /**
  150.      *
  151.      * @param clasificacion Almacena el tiempo para el dorsal que introduzcas
  152.      */
  153.     public static void introducirTiempo(TiempoDorsal[] clasificacion) {
  154.         Scanner teclado = new Scanner(System.in);
  155.         System.out.println("Introduce el Dorsal");
  156.         d = teclado.nextInt();
  157.         if (finalizados > 0) {
  158.             for (int i = 0; i < finalizados; i++) {
  159.                 if (clasificacion[i].dorsal == d) {
  160.                     System.err.println("El dorsal ya está en la base de datos");
  161.                     break;
  162.                 } else {
  163.                     System.out.println("Introduce el tiempoooo joeputa");
  164.                     t = teclado.nextInt();
  165.                     clasificacion[finalizados].dorsal = d;
  166.                     clasificacion[finalizados].tiempo = t;
  167.                     finalizados++;
  168.                 }
  169.             }
  170.         } else {
  171.             System.out.println("Intoduce el tiempo");
  172.             t = teclado.nextInt();
  173.             clasificacion[finalizados].dorsal = d;
  174.             clasificacion[finalizados].tiempo = t;
  175.             finalizados++;
  176.         }
  177.  
  178. //        d = teclado.nextInt();      
  179. //        t = teclado.nextInt();
  180. //        clasificacion[finalizados].dorsal = d;
  181. //        clasificacion[finalizados].tiempo = t;
  182. //        finalizados++;
  183. //        if (t > peor_tiempo) {
  184. //            peor_tiempo = t;  
  185. //        }
  186.     }
  187.  
  188.     /**
  189.      *
  190.      * @param clasificacion Muestra la clasificación
  191.      */
  192.     public static void mostrarClasi(TiempoDorsal[] clasificacion) {
  193.         for (int i = 0; i < clasificacion.length; i++) {
  194.             if (clasificacion[i].dorsal != 0) {
  195.                 System.out.printf("|   -%02d-   |  %-8s  |\n", clasificacion[i].dorsal, segToHumanTime(clasificacion[i].tiempo, 0));
  196.             }
  197.         }
  198.     }
  199.  
  200.     /**
  201.      * Método encontrado en internet para pasar los segundos a formato
  202.      * Hora-Minuto-Segundo
  203.      *
  204.      * @param secs
  205.      * @param format_type
  206.      * @return
  207.      */
  208.     public static String segToHumanTime(int secs, int format_type) {
  209.         String time_str = "";
  210.         int hours, mins;
  211.         hours = (int) secs / 3600;
  212.         secs -= (hours * 3600);
  213.         mins = (int) secs / 60;
  214.         secs -= (mins * 60);
  215.         if (format_type == 0) {
  216.             time_str = String.format("%02d:%02d:%02d", hours, mins, secs);
  217.         } else {
  218.             if (hours > 0) {
  219.                 time_str += String.format("%02dh ", hours);
  220.             }
  221.             if (mins > 0) {
  222.                 time_str += String.format("%02dm ", mins);
  223.             }
  224.             time_str += String.format("%02ds", secs);
  225.         }
  226.         return time_str;
  227.     }
  228.  
  229.     public static void dorsalTiempo(TiempoDorsal[] clasificacion) {
  230.         mejor_tiempo = peor_tiempo;
  231.         for (int i = 0; i < clasificacion.length; i++) {
  232.             if (clasificacion[i].dorsal != 0) {
  233.                 if (clasificacion[i].tiempo < mejor_tiempo) {
  234.                     mejor_tiempo = clasificacion[i].tiempo;
  235.                 }
  236.                 System.out.print(clasificacion[i].dorsal + " ");
  237.             }
  238.         }
  239.     }
  240.  
  241.     public static void diferenciaTiempo(TiempoDorsal[] clasificacion) {
  242.         Scanner teclado = new Scanner(System.in);
  243.         d = teclado.nextInt();
  244.         System.out.println();
  245.         for (int i = 0; i < clasificacion.length; i++) {
  246.             if (clasificacion[i].dorsal != 0) {
  247.                 if (d == clasificacion[i].dorsal) {
  248.                     int dif = clasificacion[i].tiempo - mejor_tiempo;
  249.                     if (dif == 0) {
  250.                         System.out.println("El corredor -" + d + "- tiene el mejor tiempo!");
  251.                     } else {
  252.                         System.out.println("El corredor -" + d + "- está a " + segToHumanTime(dif, 1));
  253.                     }
  254.                 }
  255.             }
  256.         }
  257.     }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement