SvetlanPetrova

Order By Age SoftUni

Aug 10th, 2021
1,446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package Exercise1002;
  2.  
  3. import java.nio.charset.IllegalCharsetNameException;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9. import java.util.Arrays;
  10.  
  11.  
  12.  
  13. public class Exercise1002 {
  14.  
  15.     public static class Person {
  16.          String name;
  17.          String id;
  18.          int age;
  19.  
  20.         Person(String name,String id, int age){
  21.             this.name = name;
  22.             this.id = id;
  23.             this.age = age;
  24.         }
  25.  
  26.         public String getName() {
  27.             return name;
  28.         }
  29.  
  30.         public String getId() {
  31.             return id;
  32.         }
  33.  
  34.         public int getAge() {
  35.             return age;
  36.         }
  37.     }
  38.  
  39.  
  40.     public static void main(String[] args) {
  41.  
  42.         Scanner scanner = new Scanner(System.in);
  43.  
  44.         String input = scanner.nextLine();
  45.  
  46.  
  47.        List<Person> people = new ArrayList<>();
  48.  
  49.         while (!input.equals("End")){
  50.            String[] tokens = input.split("\\s+");
  51.  
  52.             String name = tokens[0];
  53.             String id =  tokens[1];
  54.             int age = Integer.parseInt(tokens[2]);
  55.  
  56.             Person p =  new Person(name, id, age);
  57.  
  58.  
  59.             people.add(p);
  60.  
  61.            input = scanner.nextLine();
  62.  
  63.  
  64.         }
  65.  
  66.  
  67.         people.sort((f, s) ->f.getAge() - s.getAge());
  68.  
  69.         for (Person person:people) {
  70.             System.out.println(String.format("%s with ID: %s is %d years old.",person.getName(),person.getId(),person.getAge()));
  71.         }
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment