Advertisement
popovstefan

[NP 2 kol.] Audition

Dec 2nd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. Audition Problem 6 (2 / 8)
  2. Да се имплементира класа за аудиција Audition со следните методи:
  3.  
  4. void addParticpant(String city, String code, String name, int age) додава нов кандидат со код code, име и возраст за аудиција во даден град city. Во ист град не се дозволува додавање на кандидат со ист код како некој претходно додаден кандидат (додавањето се игнорира, а комплексноста на овој метод треба да биде O(1))
  5. void listByCity(String city) ги печати сите кандидати од даден град подредени според името, а ако е исто според возраста (комплексноста на овој метод не треба да надминува O(n∗log2(n)), каде n е бројот на кандидати во дадениот град).
  6.  
  7. =================================================================================================================
  8.  
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Hashtable;
  12. import java.util.List;
  13. import java.util.Scanner;
  14.  
  15. class Participant implements Comparable<Participant> {
  16.     private final String city, code, name;
  17.     private final int age;
  18.  
  19.     public Participant(String city, String code, String name, int age) {
  20.         this.city = city;
  21.         this.code = code;
  22.         this.name = name;
  23.         this.age = age;
  24.     }
  25.  
  26.     public String getCity() {
  27.         return city;
  28.     }
  29.  
  30.     public String getCode() {
  31.         return code;
  32.     }
  33.  
  34.     public String getName() {
  35.         return name;
  36.     }
  37.  
  38.     public int getAge() {
  39.         return age;
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return String.format("%s %s %d", code, name, age);
  45.     }
  46.  
  47.     @Override
  48.     public int compareTo(Participant o) {
  49.         if (this.name.compareTo(o.name) == 0) {
  50.             return this.age - o.age;
  51.         } else {
  52.             return this.name.compareTo(o.name);
  53.         }
  54.     } // method
  55.  
  56. } // class
  57.  
  58. class Audition {
  59.     private Hashtable<String, HashMap<String, Participant>> participants;
  60.  
  61.     public Audition() {
  62.         this.participants = new Hashtable<String, HashMap<String, Participant>>();
  63.     }
  64.  
  65.     public void addParticpant(String city, String code, String name, int age) {
  66.         Participant participant = new Participant(city, code, name, age);
  67.  
  68.         if (participants.containsKey(city)) {
  69.             if (participants.get(city).get(code) == null)
  70.                 participants.get(city).put(code, participant);
  71.         } else {
  72.             participants.put(city, new HashMap<String, Participant>());
  73.             participants.get(city).put(code, participant);
  74.         }
  75.     } // method
  76.  
  77.     public void listByCity(String city) {
  78.         this.participants.values()
  79.         .stream()
  80.         .forEach(entry -> entry.values()
  81.                  .stream()
  82.                  .sorted()
  83.                  .filter(t -> t.getCity().equals(city))
  84.                  .forEach(System.out :: println)
  85.                 );
  86.     }
  87.  
  88. } // class
  89.  
  90. public 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.addParticpant(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. }
  112.  
  113. ===============================================================================================================
  114. Sample input
  115. Скопје;001;Ана;17
  116. Скопје;002;Борис;19
  117. Скопје;002;Иван;15
  118. Скопје;003;Јована;23
  119. Скопје;004;Михаела;18
  120. Битола;002;Николина;17
  121. Битола;001;Стефанија;16
  122. Битола;001;Елена;19
  123. Битола;005;Анастасија;21
  124. Битола;004;Игор;20
  125. Гевгелија;003;Аце;17
  126. Гевгелија;001;Иван;25
  127. Гевгелија;002;Мартина;15
  128. Гевгелија;005;Наташа;19
  129. Гевгелија
  130. Битола
  131. Скопје
  132. ===============================================================================================================
  133. Sample output
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement