Advertisement
Guest User

Untitled

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