Untitled

 avataruser_6122548
plain_text
7 days ago
1.9 kB
2
Indexable
Never
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Ja_06_Exercise {

    static class IdentifikacionaLista {
        private String ime;
        private String ID;
        private int age;

        public IdentifikacionaLista (String red) {
            String [] niz = red.split(" ");
            this.ime = niz[0];
            this.ID = niz[1];
            this.age = Integer.parseInt(niz[2]);
        } public int getAge (){
            return this.age;

        } public String getIme () {
            return this.ime;
        } public String getID () {

            return this.ID;
        }

        @Override
        public String toString() {
            return String.format("%s with ID: %s is %d years old.\n", this.ime, this.ID, this.ID);
        }
    }

    public static void main(String[] args) {

        //You will receive an unknown number of lines. On each line, you will receive an array with 3 elements. The first element will be a string and represents the name of the person. The second element will be a string and will represent the ID of the person. The last element will be an integer which represents the age of the person.
        //When you receive the command "End", stop taking input and print all the people, ordered by age.
        Scanner sc = new Scanner(System.in);
        ArrayList<IdentifikacionaLista> listaOsoba = new ArrayList<>();
        while (true) {
            String red = sc.nextLine();
            if (red.equals("End")) break;
            IdentifikacionaLista osoba = new IdentifikacionaLista(red);
            listaOsoba.add(osoba);
        } listaOsoba.sort(Comparator.comparing(IdentifikacionaLista::getAge).reversed());
        for (IdentifikacionaLista trenutnaOsoba : listaOsoba) {
            System.out.println(trenutnaOsoba);
        }
    }
}