Advertisement
Ligh7_of_H3av3n

05. Filter by Age

May 27th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package Lekciq;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.function.BiPredicate;
  7. import java.util.function.Function;
  8.  
  9. public class FilterByAge {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.  
  14.         // Read number of entries
  15.         int n = Integer.parseInt(scanner.nextLine());
  16.  
  17.         // Read name and age pairs
  18.         List<Person> people = new ArrayList<>();
  19.         for (int i = 0; i < n; i++) {
  20.             String[] input = scanner.nextLine().split(", ");
  21.             String name = input[0];
  22.             int age = Integer.parseInt(input[1]);
  23.             people.add(new Person(name, age));
  24.         }
  25.  
  26.         // Read condition, age, and format
  27.         String condition = scanner.nextLine();
  28.         int ageLimit = Integer.parseInt(scanner.nextLine());
  29.         String format = scanner.nextLine();
  30.  
  31.         // Define predicates and functions
  32.         BiPredicate<Integer, Integer> ageCondition = getAgeCondition(condition);
  33.         Function<Person, String> formatFunction = getFormatFunction(format);
  34.  
  35.         // Filter and print results
  36.         people.stream()
  37.                 .filter(person -> ageCondition.test(person.age, ageLimit))
  38.                 .map(formatFunction)
  39.                 .forEach(System.out::println);
  40.     }
  41.  
  42.     // Method to get age condition predicate
  43.     private static BiPredicate<Integer, Integer> getAgeCondition(String condition) {
  44.         if (condition.equals("younger")) {
  45.             return (age, limit) -> age <= limit;
  46.         } else if (condition.equals("older")) {
  47.             return (age, limit) -> age >= limit;
  48.         } else {
  49.             throw new IllegalArgumentException("Invalid condition: " + condition);
  50.         }
  51.     }
  52.  
  53.     // Method to get format function
  54.     private static Function<Person, String> getFormatFunction(String format) {
  55.         switch (format) {
  56.             case "name":
  57.                 return person -> person.name;
  58.             case "age":
  59.                 return person -> String.valueOf(person.age);
  60.             case "name age":
  61.                 return person -> person.name + " - " + person.age;
  62.             default:
  63.                 throw new IllegalArgumentException("Invalid format: " + format);
  64.         }
  65.     }
  66.  
  67.     // Person class to store name and age
  68.     private static class Person {
  69.         String name;
  70.         int age;
  71.  
  72.         Person(String name, int age) {
  73.             this.name = name;
  74.             this.age = age;
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement