Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Participant{
- private String city;
- private String code;
- private String name;
- private int age;
- public Participant(String city, String code, String name, int age) {
- this.city = city;
- this.code = code;
- this.name = name;
- this.age = age;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Participant that = (Participant) o;
- if (!city.equals(that.city)) return false;
- return code.equals(that.code);
- }
- @Override
- public int hashCode() {
- int result = city.hashCode();
- result = 31 * result + code.hashCode();
- return result;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String toString(){
- return String.format("%s %s %d", code, name, age);
- }
- }
- class Audition{
- HashSet<Participant> people;
- public Audition() {
- this.people = new HashSet<>();
- }
- public void addParticipant(String city, String code, String name, int age){
- people.add(new Participant(city, code, name, age));
- }
- public void listByCity(String city){
- people.stream()
- .filter(p -> p.getCity().equals(city))
- .sorted(Comparator.comparing(Participant::getName).thenComparing(Participant::getAge))
- .forEach(System.out::println);
- }
- }
- 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.addParticipant(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