Didart

Opinion Poll

Nov 1st, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class OpinionPoll {
  7.     public static class Person {
  8.         private String name;
  9.         private int age;
  10.  
  11.         public Person(String name, int age) {
  12.             this.name = name;
  13.             this.age = age;
  14.         }
  15.  
  16.         public String getName() {
  17.             return this.name;
  18.         }
  19.  
  20.         public int getAge() {
  21.             return this.age;
  22.         }
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Scanner scanner = new Scanner(System.in);
  27.  
  28.         List<Person> listOfPeople = new ArrayList<>();
  29.  
  30.         int lines = Integer.parseInt(scanner.nextLine());
  31.  
  32.         for (int line = 1; line <= lines; line++) {
  33.             String personalInformation = scanner.nextLine();
  34.  
  35.             String name = personalInformation.split(" ")[0];
  36.             int age = Integer.parseInt(personalInformation.split(" ")[1]);
  37.  
  38.             Person person = new Person(name, age);
  39.  
  40.             if (age > 30) {
  41.                 listOfPeople.add(person);
  42.             }
  43.         }
  44.         listOfPeople.sort(Comparator.comparing(Person::getName));
  45.         for (Person person : listOfPeople) {
  46.             System.out.println(person.getName() + " - " + person.getAge());
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment