Advertisement
IvaAnd

OpinionPoll_03

Jun 30th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 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 Ex03_OpinionPoll {
  7.  
  8.     static class PersonClass {
  9.         private String name;
  10.         private int age;
  11.  
  12.         public PersonClass(String name, int age) {
  13.             this.name = name;
  14.             this.age = age;
  15.         }
  16.  
  17.         public String getName() {
  18.             return this.name;
  19.         }
  20.  
  21.         public int getAge() {
  22.             return this.age;
  23.         }
  24.  
  25.         @Override
  26.         public String toString() {
  27.             return String.format( "%s - %d",
  28.                     this.name,
  29.                     this.age);
  30.  
  31.         }
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         Scanner scanner = new Scanner(System.in);
  36.  
  37.         int n = Integer.parseInt(scanner.nextLine());
  38.         List<PersonClass> people = new ArrayList<>();
  39.  
  40.         for (int i = 0; i < n; i++) {
  41.  
  42.             String[] input = scanner.nextLine().split(" ");
  43.  
  44.             String name = input[0];
  45.             int age = Integer.parseInt(input[1]);
  46.             if (age < 30) {
  47.                 continue;
  48.             }
  49.  
  50.             PersonClass listOfPeople = new PersonClass(name, age);
  51.  
  52.             people.add(listOfPeople);
  53.  
  54.         }
  55.         people.sort((f, s) -> f.getName().compareTo(s.getName()));
  56.  
  57.  
  58.         for (int i = 0; i <people.size(); i++) {
  59.             System.out.println(people.get(i));
  60.         }
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement