stoyanov7

Opinion Poll

Oct 7th, 2016
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. class Person implements Comparable<Person> {
  7.     public String name;
  8.     public int age;
  9.  
  10.     public Person(String name, int age) {
  11.         this.name = name;
  12.         this.age = age;
  13.     }
  14.  
  15.     public String getName() {
  16.         return name;
  17.     }
  18.  
  19.     @Override
  20.     public int compareTo(Person p) {
  21.         return this.getName().compareTo(p.getName());
  22.     }
  23. }
  24.  
  25. public class Main {
  26.     public static void main(String[] args) {
  27.         Scanner scanner = new Scanner(System.in);
  28.         Person person;
  29.         List<Person> personList = new ArrayList<>();
  30.         int n = Integer.parseInt(scanner.nextLine());
  31.  
  32.         for (int i = 0; i < n; i++) {
  33.             String[] input = scanner.nextLine().split(" ");
  34.             person = new Person(input[0], Integer.parseInt(input[1]));
  35.  
  36.             if (person.age >= 30) {
  37.                 personList.add(person);
  38.             }
  39.         }
  40.  
  41.         Collections.sort(personList);
  42.         personList.forEach(p -> System.out.printf("%s - %d%n", p.name, p.age));
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment