Advertisement
NenadKocev

[НП] Audition

Jan 22nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Participant{
  4.     private String city;
  5.     private String code;
  6.     private String name;
  7.     private int age;
  8.  
  9.     public Participant(String city, String code, String name, int age) {
  10.         this.city = city;
  11.         this.code = code;
  12.         this.name = name;
  13.         this.age = age;
  14.     }
  15.  
  16.     @Override
  17.     public boolean equals(Object o) {
  18.         if (this == o) return true;
  19.         if (o == null || getClass() != o.getClass()) return false;
  20.  
  21.         Participant that = (Participant) o;
  22.  
  23.         if (!city.equals(that.city)) return false;
  24.         return code.equals(that.code);
  25.     }
  26.  
  27.     @Override
  28.     public int hashCode() {
  29.         int result = city.hashCode();
  30.         result = 31 * result + code.hashCode();
  31.         return result;
  32.     }
  33.  
  34.     public String getCity() {
  35.         return city;
  36.     }
  37.  
  38.     public void setCity(String city) {
  39.         this.city = city;
  40.     }
  41.  
  42.     public String getCode() {
  43.         return code;
  44.     }
  45.  
  46.     public void setCode(String code) {
  47.         this.code = code;
  48.     }
  49.  
  50.     public String getName() {
  51.         return name;
  52.     }
  53.  
  54.     public void setName(String name) {
  55.         this.name = name;
  56.     }
  57.  
  58.     public int getAge() {
  59.         return age;
  60.     }
  61.  
  62.     public void setAge(int age) {
  63.         this.age = age;
  64.     }
  65.  
  66.     public String toString(){
  67.         return String.format("%s %s %d", code, name, age);
  68.     }
  69. }
  70.  
  71. class Audition{
  72.     HashSet<Participant> people;
  73.  
  74.     public Audition() {
  75.         this.people = new HashSet<>();
  76.     }
  77.  
  78.     public void addParticipant(String city, String code, String name, int age){
  79.         people.add(new Participant(city, code, name, age));
  80.     }
  81.  
  82.     public void listByCity(String city){
  83.         people.stream()
  84.                 .filter(p -> p.getCity().equals(city))
  85.                 .sorted(Comparator.comparing(Participant::getName).thenComparing(Participant::getAge))
  86.                 .forEach(System.out::println);
  87.     }
  88. }
  89.  
  90. class AuditionTest {
  91.     public static void main(String[] args) {
  92.         Audition audition = new Audition();
  93.         List<String> cities = new ArrayList<String>();
  94.         Scanner scanner = new Scanner(System.in);
  95.         while (scanner.hasNextLine()) {
  96.             String line = scanner.nextLine();
  97.             String[] parts = line.split(";");
  98.             if (parts.length > 1) {
  99.                 audition.addParticipant(parts[0], parts[1], parts[2],
  100.                         Integer.parseInt(parts[3]));
  101.             } else {
  102.                 cities.add(line);
  103.             }
  104.         }
  105.         for (String city : cities) {
  106.             System.out.printf("+++++ %s +++++\n", city);
  107.             audition.listByCity(city);
  108.         }
  109.         scanner.close();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement