|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class almartinez { |
| 4 | + |
| 5 | + private static final List<Event> events = new ArrayList<>(); |
| 6 | + private static final Map<String, EnumMap<MedalType, Integer>> medalsByCountry = new HashMap<>(); |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + |
| 10 | + Scanner scanner = new Scanner(System.in); |
| 11 | + boolean exit = false; |
| 12 | + |
| 13 | + while (!exit) { |
| 14 | + displayMenu(); |
| 15 | + int option = getOption(scanner); |
| 16 | + |
| 17 | + switch (option) { |
| 18 | + case 1 -> registerEvent(scanner); |
| 19 | + case 2 -> registerParticipant(scanner); |
| 20 | + case 3 -> simulateEvents(); |
| 21 | + case 4 -> displayRanking(); |
| 22 | + case 5 -> exit = true; |
| 23 | + default -> System.out.println("Invalid option."); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + scanner.close(); |
| 28 | + } |
| 29 | + |
| 30 | + private static void displayMenu() { |
| 31 | + System.out.println("\nSelect an action:"); |
| 32 | + System.out.println("1. Register event"); |
| 33 | + System.out.println("2. Register participant"); |
| 34 | + System.out.println("3. Simulate events"); |
| 35 | + System.out.println("4. Create reports"); |
| 36 | + System.out.println("5. exit"); |
| 37 | + } |
| 38 | + |
| 39 | + private static int getOption(Scanner scanner) { |
| 40 | + while (!scanner.hasNextInt()) { |
| 41 | + System.out.println("Please enter a valid number."); |
| 42 | + scanner.next(); |
| 43 | + } |
| 44 | + int option = scanner.nextInt(); |
| 45 | + scanner.nextLine(); |
| 46 | + return option; |
| 47 | + } |
| 48 | + |
| 49 | + private static void registerEvent(Scanner scanner) { |
| 50 | + System.out.println("Enter the event name: "); |
| 51 | + String eventName = scanner.nextLine(); |
| 52 | + events.add(new Event(eventName)); |
| 53 | + System.out.println("Event registered: " + eventName); |
| 54 | + } |
| 55 | + |
| 56 | + private static void registerParticipant(Scanner scanner) { |
| 57 | + if (events.isEmpty()) { |
| 58 | + System.out.println("No events registered. Plase register an event first"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + System.out.println("Enter the participant's name: "); |
| 63 | + String participantName = scanner.nextLine(); |
| 64 | + |
| 65 | + System.out.println("Enter the participant's country: "); |
| 66 | + String participantCountry = scanner.nextLine(); |
| 67 | + |
| 68 | + Participant participan = new Participant(participantName, participantCountry); |
| 69 | + displayEvents(); |
| 70 | + int eventIndex = getEventIndex(scanner); |
| 71 | + |
| 72 | + if (eventIndex == -1) return; |
| 73 | + |
| 74 | + events.get(eventIndex).addParticipant(participan); |
| 75 | + medalsByCountry.putIfAbsent(participantCountry, new EnumMap<>(MedalType.class)); |
| 76 | + System.out.println("Participant registered: " + participantName + " from " + participantCountry); |
| 77 | + } |
| 78 | + |
| 79 | + private static void simulateEvents() { |
| 80 | + for (Event event : events) { |
| 81 | + System.out.println("Simulating event: " + event.name); |
| 82 | + |
| 83 | + if (event.participants.size() < 3) { |
| 84 | + System.out.println("Not enough participants to simulate this event."); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + List<Participant> winners = simulateEvent(event); |
| 89 | + assignMedals(winners); |
| 90 | + displayWinners(event, winners); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static void displayEvents() { |
| 95 | + System.out.println("Available events:"); |
| 96 | + for (int i = 0; i < events.size(); i++) { |
| 97 | + System.out.println(i + ": " + events.get(i).name); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static int getEventIndex(Scanner scanner) { |
| 102 | + System.out.println("Select the event (0-" + (events.size() -1) + "): "); |
| 103 | + while (!scanner.hasNextInt()) { |
| 104 | + System.out.println("Please enter a valid number."); |
| 105 | + scanner.next(); |
| 106 | + } |
| 107 | + int eventIndex = scanner.nextInt(); |
| 108 | + |
| 109 | + if (eventIndex < 0 || eventIndex >= events.size()) { |
| 110 | + System.out.println("Invalid event."); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + return eventIndex; |
| 114 | + } |
| 115 | + private static List<Participant> simulateEvent(Event event) { |
| 116 | + Collections.shuffle(event.participants); |
| 117 | + return event.participants.subList(0, 3); |
| 118 | + } |
| 119 | + |
| 120 | + private static void assignMedals(List<Participant> winners) { |
| 121 | + awardMedal(winners.get(0), MedalType.GOLD); |
| 122 | + awardMedal(winners.get(1), MedalType.SILVER); |
| 123 | + awardMedal(winners.get(2), MedalType.BRONZE); |
| 124 | + } |
| 125 | + |
| 126 | + private static void awardMedal(Participant participant, MedalType medalType) { |
| 127 | + EnumMap<MedalType, Integer> medals = medalsByCountry.get(participant.country); |
| 128 | + medals.put(medalType, medals.getOrDefault(medalType, 0) + 1); |
| 129 | + } |
| 130 | + |
| 131 | + private static void displayWinners(Event event, List<Participant> winners) { |
| 132 | + System.out.println("Winners of " + event.name + ":"); |
| 133 | + System.out.println("Gold: " + winners.get(0).name + " from " + winners.get(0).country); |
| 134 | + System.out.println("Silver: " + winners.get(1).name + " from " + winners.get(1).country); |
| 135 | + System.out.println("Bronze: " + winners.get(2).name + " from " + winners.get(2).country); |
| 136 | + } |
| 137 | + |
| 138 | + private static void displayRanking() { |
| 139 | + System.out.println("\nRanking of countries by number of medals:"); |
| 140 | + medalsByCountry.entrySet().stream() |
| 141 | + .sorted((a, b) -> { |
| 142 | + int totalA = a.getValue().values().stream().mapToInt(Integer::intValue).sum(); |
| 143 | + int totalB = b.getValue().values().stream().mapToInt(Integer::intValue).sum(); |
| 144 | + return Integer.compare(totalB, totalA); |
| 145 | + }) |
| 146 | + .forEach(entry -> { |
| 147 | + String country = entry.getKey(); |
| 148 | + EnumMap<MedalType, Integer> medals = entry.getValue(); |
| 149 | + System.out.printf("%s: Gold=%d, Silver=%d, Bronze=%d%n", |
| 150 | + country, |
| 151 | + medals.getOrDefault(MedalType.GOLD, 0), |
| 152 | + medals.getOrDefault(MedalType.SILVER, 0), |
| 153 | + medals.getOrDefault(MedalType.BRONZE, 0)); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + enum MedalType { |
| 158 | + GOLD, SILVER, BRONZE |
| 159 | + } |
| 160 | + |
| 161 | + static class Participant { |
| 162 | + String name; |
| 163 | + String country; |
| 164 | + |
| 165 | + public Participant(String name, String country) { |
| 166 | + this.name = name; |
| 167 | + this.country = country; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + static class Event { |
| 172 | + String name; |
| 173 | + List<Participant> participants = new ArrayList<>(); |
| 174 | + |
| 175 | + public Event(String name) { |
| 176 | + this.name = name; |
| 177 | + } |
| 178 | + |
| 179 | + void addParticipant(Participant participant) { |
| 180 | + participants.add(participant); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | +} |
0 commit comments