MarkoS2900

Audition

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