|
| 1 | +package ejercicio31; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Scanner; |
| 8 | +import java.util.TreeMap; |
| 9 | + |
| 10 | +/* |
| 11 | + * EJERCICIO: |
| 12 | + * ¡Los JJOO de París 2024 han comenzado! |
| 13 | + * Crea un programa que simule la celebración de los juegos. |
| 14 | + * El programa debe permitir al usuario registrar eventos y participantes, |
| 15 | + * realizar la simulación de los eventos asignando posiciones de manera aleatoria |
| 16 | + * y generar un informe final. Todo ello por terminal. |
| 17 | + * Requisitos: |
| 18 | + * 1. Registrar eventos deportivos. |
| 19 | + * 2. Registrar participantes por nombre y país. |
| 20 | + * 3. Simular eventos de manera aleatoria en base a los participantes (mínimo 3). |
| 21 | + * 4. Asignar medallas (oro, plata y bronce) basándose en el resultado del evento. |
| 22 | + * 5. Mostrar los ganadores por cada evento. |
| 23 | + * 6. Mostrar el ranking de países según el número de medallas. |
| 24 | + * Acciones: |
| 25 | + * 1. Registro de eventos. |
| 26 | + * 2. Registro de participantes. |
| 27 | + * 3. Simulación de eventos. |
| 28 | + * 4. Creación de informes. |
| 29 | + * 5. Salir del programa. |
| 30 | + */ |
| 31 | +public class JesusWay69 { |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + Scanner sc = new Scanner(System.in); |
| 35 | + String option; |
| 36 | + OlympicGames olympicGames = new OlympicGames(); |
| 37 | + |
| 38 | + do { |
| 39 | + System.out.print(""" |
| 40 | + Elija una opci\u00f3n: |
| 41 | + 1- Registrar evento |
| 42 | + 2- Registrar participante |
| 43 | + 3- Simular evento |
| 44 | + 4- Crear informe |
| 45 | + 5- Salir |
| 46 | + ---> """); |
| 47 | + option = sc.next().strip(); |
| 48 | + sc.nextLine(); |
| 49 | + switch (option) { |
| 50 | + case "1": |
| 51 | + System.out.print("Escriba el nombre de la competición a registrar: "); |
| 52 | + String sport = sc.nextLine().strip().toUpperCase(); |
| 53 | + olympicGames.registerEvent(sport); |
| 54 | + break; |
| 55 | + case "2": |
| 56 | + System.out.print("Escriba el nombre del participante a registrar: "); |
| 57 | + String name = sc.nextLine().strip().toUpperCase(); |
| 58 | + System.out.print("Escriba el nombre del pais de " + name + " : "); |
| 59 | + String country = sc.nextLine().strip().toUpperCase(); |
| 60 | + olympicGames.registerAthlete(name, country); |
| 61 | + break; |
| 62 | + case "3": |
| 63 | + olympicGames.eventSimulator(); |
| 64 | + break; |
| 65 | + case "4": |
| 66 | + olympicGames.showEvents(); |
| 67 | + olympicGames.showAthletes(); |
| 68 | + break; |
| 69 | + case "5": |
| 70 | + break; |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + } while (!"5".equals(option)); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +class OlympicGames { |
| 81 | + |
| 82 | + List<String> sportEvents = new ArrayList<>(); |
| 83 | + Map<String, String> competitors = new HashMap<>(); |
| 84 | + |
| 85 | + public OlympicGames() { |
| 86 | + } |
| 87 | + |
| 88 | + public void registerEvent(String event) { |
| 89 | + sportEvents.add(event); |
| 90 | + System.out.println("El evento deportivo " + event + " se ha registrado correctamente\n"); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + public void registerAthlete(String name, String country) { |
| 95 | + competitors.put(name, country); |
| 96 | + System.out.print("El participante " + name + " de " + country + " se ha registrado correctamente\n"); |
| 97 | + showEvents(); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public void showEvents() { |
| 102 | + System.out.println("Deportes disponibles:"); |
| 103 | + int i = 0; |
| 104 | + for (String sport : sportEvents) { |
| 105 | + i++; |
| 106 | + System.out.println(i + "-" + sport); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + public void showAthletes() { |
| 112 | + int i = 0; |
| 113 | + System.out.println("Atletas disponibles: "); |
| 114 | + for (String key : competitors.keySet()) { |
| 115 | + String value = competitors.get(key); |
| 116 | + i++; |
| 117 | + System.out.println(i + "-" + key + " --> " + value); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public void eventSimulator() { |
| 122 | + int athNum = 1, sportNum = 0; |
| 123 | + Scanner sc = new Scanner(System.in); |
| 124 | + System.out.println("Comienzan los eventos!!"); |
| 125 | + System.out.println("-----------------------"); |
| 126 | + //TreeMap<String, String> selectecCompetitors = new TreeMap<>(); |
| 127 | + List<String> competitorsNames = new ArrayList<>(); |
| 128 | + List<String> competitorsCountries = new ArrayList<>(); |
| 129 | + |
| 130 | + boolean flag = true; |
| 131 | + |
| 132 | + while (flag) { |
| 133 | + showEvents(); |
| 134 | + System.out.print("Elija el número del deporte para empezar la competición: "); |
| 135 | + sportNum = sc.nextInt(); |
| 136 | + if (sportNum < 1 || sportNum > sportEvents.size()) { |
| 137 | + System.out.println("El número " + sportNum + " no corresponde a ningún deporte"); |
| 138 | + } else { |
| 139 | + System.out.println("Empieza la competición de " + sportEvents.get(sportNum - 1)); |
| 140 | + flag = false; |
| 141 | + } |
| 142 | + } |
| 143 | + while (!competitors.isEmpty() || athNum != 0) { |
| 144 | + showAthletes(); |
| 145 | + System.out.print("Elija el número de un deportista para empezar la competición de: " |
| 146 | + + sportEvents.get(sportNum - 1) |
| 147 | + + "\n e introduce 0 cuando se hayan seleccionado al menos 3 participantes para terminar la selección: "); |
| 148 | + athNum = sc.nextInt(); |
| 149 | + if (athNum < 0 || athNum > competitors.size()) { |
| 150 | + System.out.println("El número " + athNum + " no corresponde a ningún deportista"); |
| 151 | + } else { |
| 152 | + int i = 0; |
| 153 | + for (String key : competitors.keySet()) { |
| 154 | + String value = competitors.get(key); |
| 155 | + i++; |
| 156 | + if (athNum == i) { |
| 157 | + competitorsNames.add(key); |
| 158 | + competitorsCountries.add(value); |
| 159 | + //selectecCompetitors.put(key, value); |
| 160 | + System.out.println("Participante nº " + athNum + " añadido a la competición de " + sportEvents.get(sportNum - 1)); |
| 161 | + } else if (athNum == 0 && competitorsNames.size() < 3) { |
| 162 | + System.out.println("Debe elegir al menos 3 participantes"); |
| 163 | + } else if (athNum == 0 && competitorsNames.size() >= 3) { |
| 164 | + competitors.clear(); |
| 165 | + randomCompetition(competitorsNames, competitorsCountries); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + public void randomCompetition(List competitorsNames, List competitorsCountries) { |
| 174 | + String[] podium = {"oro", "plata", "bronce"}; |
| 175 | + int medal = 0; |
| 176 | + |
| 177 | + while (!competitorsNames.isEmpty() && !competitorsCountries.isEmpty()) { |
| 178 | + |
| 179 | + for (int i = 0; i < 3; i++) { |
| 180 | + int winner = (int) (Math.random() * competitorsNames.size()); |
| 181 | + System.out.println("El atleta " + competitorsNames.get(winner) + " de " + competitorsCountries.get(winner) |
| 182 | + + " gana la medalla de " + podium[medal]); |
| 183 | + medal++; |
| 184 | + competitorsNames.remove(winner); |
| 185 | + competitorsCountries.remove(winner); |
| 186 | + if (medal>=2){ |
| 187 | + break; |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments