Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Audition Problem 6 (2 / 8)
- Да се имплементира класа за аудиција Audition со следните методи:
- void addParticpant(String city, String code, String name, int age) додава нов кандидат со код code, име и возраст за аудиција во даден град city. Во ист град не се дозволува додавање на кандидат со ист код како некој претходно додаден кандидат (додавањето се игнорира, а комплексноста на овој метод треба да биде O(1))
- void listByCity(String city) ги печати сите кандидати од даден град подредени според името, а ако е исто според возраста (комплексноста на овој метод не треба да надминува O(n∗log2(n)), каде n е бројот на кандидати во дадениот град).
- =================================================================================================================
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Scanner;
- class Participant implements Comparable<Participant> {
- private final String city, code, name;
- private final int age;
- public Participant(String city, String code, String name, int age) {
- this.city = city;
- this.code = code;
- this.name = name;
- this.age = age;
- }
- public String getCity() {
- return city;
- }
- public String getCode() {
- return code;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- @Override
- public String toString() {
- return String.format("%s %s %d", code, name, age);
- }
- @Override
- public int compareTo(Participant o) {
- if (this.name.compareTo(o.name) == 0) {
- return this.age - o.age;
- } else {
- return this.name.compareTo(o.name);
- }
- } // method
- } // class
- class Audition {
- private Hashtable<String, HashMap<String, Participant>> participants;
- public Audition() {
- this.participants = new Hashtable<String, HashMap<String, Participant>>();
- }
- public void addParticpant(String city, String code, String name, int age) {
- Participant participant = new Participant(city, code, name, age);
- if (participants.containsKey(city)) {
- if (participants.get(city).get(code) == null)
- participants.get(city).put(code, participant);
- } else {
- participants.put(city, new HashMap<String, Participant>());
- participants.get(city).put(code, participant);
- }
- } // method
- public void listByCity(String city) {
- this.participants.values()
- .stream()
- .forEach(entry -> entry.values()
- .stream()
- .sorted()
- .filter(t -> t.getCity().equals(city))
- .forEach(System.out :: println)
- );
- }
- } // class
- public class AuditionTest {
- public static void main(String[] args) {
- Audition audition = new Audition();
- List<String> cities = new ArrayList<String>();
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- if (parts.length > 1) {
- audition.addParticpant(parts[0], parts[1], parts[2],
- Integer.parseInt(parts[3]));
- } else {
- cities.add(line);
- }
- }
- for (String city : cities) {
- System.out.printf("+++++ %s +++++\n", city);
- audition.listByCity(city);
- }
- scanner.close();
- }
- }
- ===============================================================================================================
- Sample input
- Скопје;001;Ана;17
- Скопје;002;Борис;19
- Скопје;002;Иван;15
- Скопје;003;Јована;23
- Скопје;004;Михаела;18
- Битола;002;Николина;17
- Битола;001;Стефанија;16
- Битола;001;Елена;19
- Битола;005;Анастасија;21
- Битола;004;Игор;20
- Гевгелија;003;Аце;17
- Гевгелија;001;Иван;25
- Гевгелија;002;Мартина;15
- Гевгелија;005;Наташа;19
- Гевгелија
- Битола
- Скопје
- ===============================================================================================================
- Sample output
- +++++ Гевгелија +++++
- 003 Аце 17
- 001 Иван 25
- 002 Мартина 15
- 005 Наташа 19
- +++++ Битола +++++
- 005 Анастасија 21
- 004 Игор 20
- 002 Николина 17
- 001 Стефанија 16
- +++++ Скопје +++++
- 003 Јована 23
- 001 Ана 17
- 002 Борис 19
- 004 Михаела 18
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement