Advertisement
Guest User

orderByAge

a guest
Nov 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class exerciseOrderByAge {
  6.  
  7.     static class People {
  8.         String name;
  9.         String numberID;
  10.         int age;
  11.  
  12.         public People(String name, String numberID, int age) {
  13.             this.name = name;
  14.             this.numberID = numberID;
  15.             this.age = age;
  16.         }
  17.  
  18.         public int getAge() {
  19.             return this.age;
  20.         }
  21.  
  22.         @Override
  23.         public String toString() {
  24.             return name + " with ID: " + numberID + " is " + age + " years old.";
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args) {
  29.         Scanner scanner = new Scanner(System.in);
  30.  
  31.         List<People> peopleList = new ArrayList<>();
  32.         String line = scanner.nextLine();
  33.  
  34.         while (!line.equals("End")) {
  35.             String[] tokens = line.split(" ");
  36.             String name = tokens[0];
  37.             String numberID = tokens[1];
  38.             int age = Integer.parseInt(tokens[2]);
  39.  
  40.             People people = new People(name, numberID, age);
  41.             peopleList.add(people);
  42.  
  43.             line = scanner.nextLine();
  44.         }
  45.         peopleList.stream().sorted((e2, e1) -> Integer.compare(e2.getAge(), e1.getAge())).forEach(System.out::println);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement