borovaneca

PredicateParty

Feb 27th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package Advance.FunctionalProgramming.Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.function.BiPredicate;
  8. import java.util.stream.Collectors;
  9.  
  10. public class PredicateParty {
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.  
  15.         List<String> names = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  16.  
  17.         String input;
  18.         while (!"Party!".equals(input = scanner.nextLine()) && !names.isEmpty()) {
  19.             String[] tokens = input.split(" ");
  20.             String command = tokens[0];
  21.             String secondCommand = tokens[1];
  22.  
  23.             if (command.equals("Remove")) {
  24.                 if (secondCommand.equals("StartsWith")) {
  25.                     String lastCommand = tokens[2];
  26.                     names = names.stream().filter(name -> !name.startsWith(lastCommand)).collect(Collectors.toList());
  27.                 } else if (secondCommand.equals("EndsWith")) {
  28.                     String lastCommand = tokens[2];
  29.                     names = names.stream().filter(name -> !name.endsWith(lastCommand)).collect(Collectors.toList());
  30.                 } else if (secondCommand.equals("Length")) {
  31.                     int namesLength = Integer.parseInt(tokens[2]);
  32.                     names = names.stream().filter(name -> name.length() != namesLength).collect(Collectors.toList());
  33.                 }
  34.  
  35.             } else if (command.equals("Double")) {
  36.                 if (secondCommand.equals("StartsWith")) {
  37.                     String lastCommand = tokens[2];
  38.                     BiPredicate<String, String> doubleStartsWithPredicate = String::startsWith;
  39.                     List<String> current = names.stream().filter(name -> doubleStartsWithPredicate.test(name, lastCommand)).collect(Collectors.toList());
  40.                     for (int i = 0; i < current.size(); i++) {
  41.                         names.add(names.indexOf(current.get(i)),current.get(i));
  42.                     }
  43.  
  44.                 } else if (secondCommand.equals("EndsWith")) {
  45.                     String lastCommand = tokens[2];
  46.                     BiPredicate<String, String> doubleEndsWithPredicate = String::endsWith;
  47.                     List<String> current = names.stream().filter(name -> doubleEndsWithPredicate.test(name, lastCommand)).collect(Collectors.toList());
  48.                     for (int i = 0; i < current.size(); i++) {
  49.                         names.add(names.indexOf(current.get(i)), current.get(i));
  50.                     }
  51.                 } else if (secondCommand.equals("Length")) {
  52.                     int namesLength = Integer.parseInt(tokens[2]);
  53.                     BiPredicate<String, Integer> doubleLengthPredicate = (name, length) -> name.length() == length;
  54.                     List<String> current = names.stream().filter(name -> doubleLengthPredicate.test(name, namesLength)).collect(Collectors.toList());
  55.                     for (int i = 0; i < current.size(); i++) {
  56.                         names.add(names.indexOf(current.get(i)), current.get(i));
  57.                     }
  58.  
  59.                 }
  60.             }
  61.         }
  62.  
  63.         Collections.sort(names);
  64.         if (!names.isEmpty()) {
  65.  
  66.             System.out.println(String.join(", ", names) + " are going to the party!");
  67.         } else {
  68.             System.out.println("Nobody is going to the party!");
  69.         }
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment