Advertisement
IrinaIgnatova

Order By Age

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