Advertisement
SashkoKlincharov

[Java][НП] - Audition

Aug 26th, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1.  
  2.  
  3. Да се имплементира класа за аудиција Audition со следните методи:
  4.  
  5. void addParticpant(String city, String code, String name, int age) додава нов кандидат со код code, име и возраст за аудиција во даден град city. Во ист град не се дозволува додавање на кандидат со ист код како некој претходно додаден кандидат (додавањето се игнорира, а комплексноста на овој метод треба да биде O(1))
  6. void listByCity(String city) ги печати сите кандидати од даден град подредени според името, а ако е исто според возраста (комплексноста на овој метод не треба да надминува O(n∗log2(n)), каде n е бројот на кандидати во дадениот град).
  7.  
  8.  
  9.  
  10. import java.util.*;
  11.  
  12. class Participant implements Comparable<Participant> {
  13. private String city;
  14. private String code;
  15. private String name;
  16. private int age;
  17.  
  18. public Participant(String city, String code, String name, int age) {
  19. this.city = city;
  20. this.code = code;
  21. this.name = name;
  22. this.age = age;
  23. }
  24.  
  25. @Override
  26. public int compareTo(Participant o) {
  27. int res = name.toLowerCase().compareTo(o.name.toLowerCase());
  28. if(res == 0)
  29. return Integer.compare(age,o.age);
  30. return res;
  31. }
  32.  
  33. @Override
  34. public boolean equals(Object o) {
  35. if (this == o) return true;
  36. if (o == null || getClass() != o.getClass()) return false;
  37. Participant that = (Participant) o;
  38. return Objects.equals(code, that.code);
  39. }
  40.  
  41.  
  42. @Override
  43. public int hashCode() {
  44. return Objects.hash(code);
  45. }
  46.  
  47. public String getCity() {
  48. return city;
  49. }
  50.  
  51. public int getAge() {
  52. return age;
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. return String.format("%s %s %d",code,name,age);
  58. }
  59. }
  60.  
  61. class Audition {
  62. private Map<String,Set<Participant>> participants_by_city;
  63.  
  64. public Audition() {
  65. participants_by_city = new HashMap<>();
  66. }
  67. public void addParticpant(String city, String code, String name, int age){
  68. participants_by_city.putIfAbsent(city,new HashSet<>());
  69. Participant participant = new Participant(city, code, name, age);
  70. participants_by_city.computeIfPresent(city,(k,v) -> {
  71. v.add(participant);//koristi equals, ako kodovite im se isti ne dodava takov
  72. return v;
  73. });
  74.  
  75.  
  76. }
  77.  
  78. public void listByCity(String city){
  79. Set<Participant> participants_of_city = participants_by_city.get(city);
  80. participants_of_city.stream()
  81. .sorted(Participant::compareTo)
  82. .forEach(System.out::println);
  83. }
  84. }
  85.  
  86. public class AuditionTest {
  87. public static void main(String[] args) {
  88. Audition audition = new Audition();
  89. List<String> cities = new ArrayList<String>();
  90. Scanner scanner = new Scanner(System.in);
  91. while (scanner.hasNextLine()) {
  92. String line = scanner.nextLine();
  93. String[] parts = line.split(";");
  94. if (parts.length > 1) {
  95. audition.addParticpant(parts[0], parts[1], parts[2],
  96. Integer.parseInt(parts[3]));
  97. } else {
  98. cities.add(line);
  99. }
  100. }
  101. for (String city : cities) {
  102. System.out.printf("+++++ %s +++++\n", city);
  103. audition.listByCity(city);
  104. }
  105. scanner.close();
  106. }
  107. }
  108.  
  109.  
  110.  
  111.  
  112. Sample input
  113.  
  114. Скопје;001;Ана;17
  115. Скопје;002;Борис;19
  116. Скопје;002;Иван;15
  117. Скопје;003;Јована;23
  118. Скопје;004;Михаела;18
  119. Битола;002;Николина;17
  120. Битола;001;Стефанија;16
  121. Битола;001;Елена;19
  122. Битола;005;Анастасија;21
  123. Битола;004;Игор;20
  124. Гевгелија;003;Аце;17
  125. Гевгелија;001;Иван;25
  126. Гевгелија;002;Мартина;15
  127. Гевгелија;005;Наташа;19
  128. Гевгелија
  129. Битола
  130. Скопје
  131.  
  132. Sample output
  133.  
  134. +++++ Гевгелија +++++
  135. 003 Аце 17
  136. 001 Иван 25
  137. 002 Мартина 15
  138. 005 Наташа 19
  139. +++++ Битола +++++
  140. 005 Анастасија 21
  141. 004 Игор 20
  142. 002 Николина 17
  143. 001 Стефанија 16
  144. +++++ Скопје +++++
  145. 003 Јована 23
  146. 001 Ана 17
  147. 002 Борис 19
  148. 004 Михаела 18
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement