Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.*;
- import java.util.stream.Collectors;
- class Audition {
- Map<String, List<Participant>> participantsByCity;
- public Audition() {
- participantsByCity = new HashMap<>();
- }
- public void addParticpant(String city, String code, String name, int age) {
- participantsByCity.putIfAbsent(city, new ArrayList<Participant>());
- if(participantsByCity.get(city).stream().map(p->p.getCode()).collect(Collectors.toList()).contains(code))
- return ;
- participantsByCity.get(city).add(new Participant(city, code, name, age));
- }
- public void listByCity(String city) {
- Comparator<Participant> comparator = Comparator.comparing(Participant::getName)
- .thenComparing(Participant::getAge).thenComparing(Participant::getCode);
- participantsByCity.get(city).stream().sorted(comparator).forEach(System.out::println);
- }
- }
- class Participant {
- String city;
- String code;
- String name;
- 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);
- }
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement