Advertisement
Ligh7_of_H3av3n

10. Predicate Party!

May 27th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7. import java.util.function.Predicate;
  8. import java.util.stream.Collectors;
  9.  
  10. public class PredicateParty {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.         String line = reader.readLine();
  14.  
  15.         List<String> guests = Arrays.stream(line.split("\\s+")).collect(Collectors.toList());
  16.  
  17.         while (!"Party!".equals(line = reader.readLine())) {
  18.             String[] commandTokens = line.split("\\s+");
  19.             String command = commandTokens[0];
  20.             String predicateType = commandTokens[1];
  21.             String predicateArgument = commandTokens[2];
  22.  
  23.             if (command.equals("Remove")) {
  24.                 guests.removeIf(getPredicate(predicateType, predicateArgument));
  25.             } else if (command.equals("Double")) {
  26.                 List<String> temp = new ArrayList<>();
  27.                 for (String guest : guests) {
  28.                     if (getPredicate(predicateType, predicateArgument).test(guest)) {
  29.                         temp.add(guest);
  30.                     }
  31.                     temp.add(guest);
  32.                 }
  33.                 guests = temp;
  34.             }
  35.         }
  36.  
  37.         if (guests.isEmpty()) {
  38.             System.out.println("Nobody is going to the party!");
  39.         } else {
  40.             Collections.sort(guests);
  41.             System.out.println(String.join(", ", guests) + " are going to the party!");
  42.         }
  43.     }
  44.  
  45.     private static Predicate<String> getPredicate(String type, String parameter) {
  46.         switch (type) {
  47.             case "StartsWith":
  48.                 return text -> text.startsWith(parameter);
  49.             case "EndsWith":
  50.                 return text -> text.endsWith(parameter);
  51.             case "Length":
  52.                 return text -> text.length() == Integer.parseInt(parameter);
  53.             default:
  54.                 return text -> false;
  55.         }
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement