Advertisement
gjorgjikirkov

AuditionTest

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