Didart

Order By Age

Nov 1st, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 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 OrderByAge {
  7.  
  8.     public static class Person {
  9.         private String name;
  10.         private String id;
  11.         private int age;
  12.  
  13.         public Person(String name, String id, int age) {
  14.             this.name = name;
  15.             this.id = id;
  16.             this.age = age;
  17.         }
  18.  
  19.         public int getAge() {
  20.             return this.age;
  21.         }
  22.  
  23.         @Override
  24.         public String toString() {
  25.             return String.format("%s with ID: %s is %d years old.", this.name, this.id, this.age);
  26.         }
  27.     }
  28.  
  29.     public static void main(String[] args) {
  30.         Scanner scanner = new Scanner(System.in);
  31.  
  32.         String personalInformation = scanner.nextLine();
  33.  
  34.         List<Person> listOfPeople = new ArrayList<>();
  35.  
  36.         while (!personalInformation.equals("End")) {
  37.          
  38.             String name = personalInformation.split(" ")[0];
  39.             String id = personalInformation.split(" ")[1];
  40.             int age = Integer.parseInt(personalInformation.split(" ")[2]);
  41.  
  42.             Person person = new Person(name, id, age);
  43.             listOfPeople.add(person);
  44.  
  45.             personalInformation = scanner.nextLine();
  46.         }
  47.      
  48.         listOfPeople.sort(Comparator.comparing(Person::getAge));
  49.  
  50.         for (Person person : listOfPeople) {
  51.             System.out.println(person.toString());
  52.         }
  53.     }
  54. }
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment