Advertisement
nikeza

10.Predicate Party!

Oct 8th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.function.Predicate;
  3. import java.util.stream.Collectors;
  4.  
  5. public class functional1_Exercises_10Predicate_Party {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         List<String> names = Arrays.stream(scanner.nextLine().split("\\s+"))
  10.                 .collect(Collectors.toList());
  11.  
  12.         String command = scanner.nextLine();
  13.  
  14.         while (!command.equals("Party!")) {
  15.             String[] tokens = command.split("\\s+");
  16.  
  17.             if (tokens[0].equals("Remove")) {
  18.  
  19.                 List<String> namesDouble = names.stream()
  20.                         .filter(predicateRemove(names, tokens[1], tokens[2]))
  21.                         .collect(Collectors.toList());
  22.                 names.clear();
  23.                 names.addAll(namesDouble);
  24.             } else {
  25.  
  26.                 List<String> namesDouble = names.stream()
  27.                         .filter(predicateAdd(names, tokens[1], tokens[2]))
  28.                         .collect(Collectors.toList());
  29.                 names.addAll(namesDouble);
  30.             }
  31.             command = scanner.nextLine();
  32.         }
  33.  
  34.         if (names.isEmpty()) {
  35.             System.out.println("Nobody is going to the party!");
  36.         } else {
  37.             Collections.sort(names);
  38.             System.out.print(names.toString().replaceAll("[\\[\\]]", ""));
  39.             System.out.println(" are going to the party!");
  40.         }
  41.     }
  42.  
  43.     private static Predicate<String> predicateRemove(List<String> names, String type, String data) {
  44.         Predicate<String> predicate = null;
  45.  
  46.         if (type.equals("StartsWith")) {
  47.             predicate = f -> !f.startsWith(data);
  48.  
  49.         } else if (type.equals("EndsWith")) {
  50.             predicate = e -> !e.endsWith(data);
  51.  
  52.         }
  53.         if (type.equals("Length")) {
  54.             predicate = e -> e.length() != Integer.parseInt(data);
  55.  
  56.         }
  57.         return predicate;
  58.     }
  59.  
  60.     private static Predicate<String> predicateAdd(List<String> names, String type, String data) {
  61.         Predicate<String> predicate = null;
  62.         if (type.equals("StartsWith")) {
  63.             predicate = f -> f.startsWith(data);
  64.  
  65.         } else if (type.equals("EndsWith")) {
  66.             predicate = e -> e.endsWith(data);
  67.  
  68.         } else if (type.equals("Length")) {
  69.             predicate = e -> e.length() == Integer.parseInt(data);
  70.  
  71.         }
  72.         return predicate;
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement