Advertisement
Kancho

Persons_Age

May 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. package OpinionPoll;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class Main {
  10. public static void main(String[] args) throws IOException {
  11.  
  12. BufferedReader reader =
  13. new BufferedReader(
  14. new InputStreamReader(
  15. System.in));
  16.  
  17. System.out.println("Number of people ");
  18. int n = Integer.parseInt(reader.readLine());
  19. List<Persons> people = new ArrayList<>();
  20.  
  21. while (n -- > 0) {
  22. System.out.println("Person - age ");
  23. String []data = reader.readLine().split("\\s+");
  24.  
  25. Persons persons = new Persons(data[0], Integer.parseInt(data[1]));
  26. people.add(persons);
  27. }
  28.  
  29. people.stream().filter(persons -> persons.getAge() >= 20)
  30. .sorted((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()))
  31. .forEach(persons -> {
  32. System.out.println(persons.toString());
  33. });
  34. }
  35. }
  36. private String name;
  37. private int age;
  38.  
  39. public Persons(String name, int age) {
  40. this.name = name;
  41. this.age = age;
  42. }
  43.  
  44. public String getName() {
  45. return this.name;
  46. }
  47.  
  48. public int getAge() {
  49. return this.age;
  50. }
  51.  
  52. @Override
  53. public String toString() {
  54. return String.format("%s - %d", name, age);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement