Advertisement
vkarakolev

objectsEx3

Feb 25th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import javax.xml.namespace.QName;
  2. import java.util.*;
  3.  
  4. public class E03_OpinionPoll {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int count = Integer.parseInt(scanner.nextLine());
  8.  
  9.         List<Person> persons = new ArrayList<>();
  10.  
  11.         for (int i = 0; i < count; i++) {
  12.             String[] personInfo = scanner.nextLine().split("\\s+");
  13.             String personName = personInfo[0];
  14.             int personAge = Integer.parseInt(personInfo[1]);
  15.  
  16.             Person p = new Person(personName, personAge);
  17.             if(personAge > 30){
  18.                
  19.                 persons.add(p);
  20.             }
  21.         }
  22.  
  23.         Collections.sort(persons, Comparator.comparing(Person::getName));
  24.  
  25.         for (Person person : persons) {
  26.             System.out.print(person.toString());
  27.         }
  28.  
  29.  
  30.     }
  31.  
  32.     static class Person {
  33.         String name;
  34.         int age;
  35.  
  36.         public Person(String name, int age){
  37.             this.name = name;
  38.             this.age = age;
  39.         }
  40.  
  41.         public String getName() {
  42.             return name;
  43.         }
  44.  
  45.         public int getAge() {
  46.             return age;
  47.         }
  48.  
  49.         public String toString(){
  50.             return String.format("%s - %d%n", getName(), getAge());
  51.         }
  52.     }
  53.  
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement