Advertisement
dzocesrce

[NP] Audition

Apr 22nd, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. class Audition {
  8.     Map<String, List<Participant>> participantsByCity;
  9.  
  10.     public Audition() {
  11.         participantsByCity = new HashMap<>();
  12.     }
  13.  
  14.     public void addParticpant(String city, String code, String name, int age) {
  15.         participantsByCity.putIfAbsent(city, new ArrayList<Participant>());
  16.         if(participantsByCity.get(city).stream().map(p->p.getCode()).collect(Collectors.toList()).contains(code))
  17.             return ;
  18.         participantsByCity.get(city).add(new Participant(city, code, name, age));
  19.  
  20.     }
  21.  
  22.     public void listByCity(String city) {
  23.         Comparator<Participant> comparator = Comparator.comparing(Participant::getName)
  24.                 .thenComparing(Participant::getAge).thenComparing(Participant::getCode);
  25.         participantsByCity.get(city).stream().sorted(comparator).forEach(System.out::println);
  26.     }
  27. }
  28.  
  29.  
  30. class Participant {
  31.  
  32.     String city;
  33.     String code;
  34.     String name;
  35.     int age;
  36.  
  37.     public Participant(String city, String code, String name, int age) {
  38.         this.city = city;
  39.         this.code = code;
  40.         this.name = name;
  41.         this.age = age;
  42.     }
  43.  
  44.     public String getCity() {
  45.         return city;
  46.     }
  47.  
  48.     public String getCode() {
  49.         return code;
  50.     }
  51.  
  52.     public String getName() {
  53.         return name;
  54.     }
  55.  
  56.     public int getAge() {
  57.         return age;
  58.     }
  59.  
  60.     @Override
  61.     public String toString() {
  62.         return String.format("%s %s %d", code, name, age);
  63.     }
  64. }
  65.  
  66. public class AuditionTest {
  67.     public static void main(String[] args) {
  68.         Audition audition = new Audition();
  69.         List<String> cities = new ArrayList<String>();
  70.         Scanner scanner = new Scanner(System.in);
  71.         while (scanner.hasNextLine()) {
  72.             String line = scanner.nextLine();
  73.             String[] parts = line.split(";");
  74.             if (parts.length > 1) {
  75.                 audition.addParticpant(parts[0], parts[1], parts[2],
  76.                         Integer.parseInt(parts[3]));
  77.             } else {
  78.                 cities.add(line);
  79.             }
  80.         }
  81.         for (String city : cities) {
  82.             System.out.printf("+++++ %s +++++\n", city);
  83.             audition.listByCity(city);
  84.         }
  85.         scanner.close();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement