Advertisement
Guest User

07. Order by Age

a guest
Dec 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10. public static class Peoples {
  11. private String name;
  12. private String ID;
  13. private Integer age;
  14.  
  15. public Peoples(String name, String ID, int age) {
  16. this.name = name;
  17. this.ID = ID;
  18. this.age = age;
  19.  
  20. }
  21.  
  22. public String getName() {
  23. return this.name;
  24. }
  25.  
  26. public String getID() {
  27. return this.ID;
  28. }
  29.  
  30. public Integer getAge() {
  31. return this.age;
  32. }
  33.  
  34. @Override
  35. public String toString() {
  36. return String.format("%s with ID: %s is %d years old.", this.getName(), this.getID(), this.getAge());
  37. }
  38. }
  39.  
  40. public static void main(String[] args) {
  41. Scanner sc = new Scanner(System.in);
  42. String input = sc.nextLine();
  43. ArrayList<Peoples> peoples = new ArrayList<>();
  44.  
  45. while (!input.equalsIgnoreCase("End")) {
  46. String[] data = input.split("\\s+");
  47. String name = data[0];
  48. String ID = data[1];
  49. int age = Integer.parseInt(data[2]);
  50. Peoples people = new Peoples(name, ID, age);
  51. peoples.add(people);
  52.  
  53. input= sc.nextLine();
  54.  
  55. }
  56. peoples = peoples.stream().collect(Collectors.toCollection(ArrayList::new));
  57. peoples.sort(Comparator.comparingInt(Peoples::getAge));
  58. peoples.stream().forEach(people -> System.out.println(people.toString()));
  59.  
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement