Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package orderByAge;
- public class Person {
- //полета -> характеристики -> име, ID, възраст
- private String name;
- private String id;
- private int age;
- //конструктор -> създавам обекти от класа
- public Person(String name, String id, int age) {
- //нов празен обект (name = null, id = null, age = 0)
- this.name = name;
- this.id = id;
- this.age = age;
- }
- public int getAge() {
- return this.age;
- }
- @Override
- public String toString() {
- //"{name} with ID: {id} is {age} years old."
- return String.format("%s with ID: %s is %d years old.", this.name, this.id, this.age);
- }
- }
- package orderByAge;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Person> personList = new ArrayList<>();
- String input = scanner.nextLine();
- while (!input.equals("End")) {
- //input -> данни за даден човек
- //input = "Desislava 1234 24".split(" ") -> ["Desislava", "1234", "24"]
- String name = input.split(" ")[0];
- String id = input.split(" ")[1];
- int age = Integer.parseInt(input.split(" ")[2]);
- Person person = new Person(name, id, age);
- personList.add(person);
- input = scanner.nextLine();
- }
- //списък с хора
- //1. сортираме по възраст (в нарастващ ред -> ascending order)
- personList.sort(Comparator.comparing(Person::getAge));
- //2. принтираме всеки човек
- for (Person person : personList) {
- //"{name} with ID: {id} is {age} years old."
- System.out.println(person);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement