Lyubohd

10. Predicate Party!

Jun 22nd, 2020
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.function.BiPredicate;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         List<String> names = Arrays.stream(scan.nextLine().split("\\s+")).collect(Collectors.toList());
  11.  
  12.         String input = scan.nextLine();
  13.         while (!"Party!".equals(input)) {
  14.             BiPredicate<String, String> predicate = null;
  15.             String[] tokens = input.split("\\s+");
  16.             String command = tokens[1];
  17.             switch (command) {
  18.                 case "StartsWith":
  19.                     predicate = (n, s) -> n.startsWith(s);
  20.                     break;
  21.                 case "EndsWith":
  22.                     predicate = (n, e) -> n.endsWith(e);
  23.                     break;
  24.                 case "Length":
  25.                     predicate = (n, l) -> {
  26.                         int len = Integer.parseInt(l);
  27.                         return n.length() == len;
  28.                     };
  29.                     break;
  30.             }
  31.  
  32.             String removeOrDouble = tokens[0];
  33.             if ("Double".equals(removeOrDouble)) {
  34.                 int startSize = names.size();
  35.                 for (int i = 0; i < startSize; i++) {
  36.                     if (predicate.test(names.get(i), tokens[2])) {
  37.                         names.add(names.get(i));
  38.                     }
  39.                 }
  40.             } else if ("Remove".equals(removeOrDouble)) {
  41.                 for (int i = 0; i < names.size(); i++) {
  42.                     if (predicate.test(names.get(i), tokens[2])) {
  43.                         names.remove(i);
  44.                         i--;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             input = scan.nextLine();
  50.         }
  51.  
  52.         names = names.stream().sorted((n1, n2) -> n1.compareTo(n2)).collect(Collectors.toList());
  53.         if (names.size() == 0) {
  54.             System.out.println("Nobody is going to the party!");
  55.         } else {
  56.             System.out.printf("%s are going to the party!",names.toString().replaceAll("\\[|]", ""));
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment