LardaX

PredicateParty

Feb 9th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.function.*;
  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.         List<String> guestList = Arrays.stream(reader.readLine().split(" ")).collect(Collectors.toList());
  14.  
  15.         String[] commandLine = reader.readLine().split(" ");
  16.  
  17.         while (!"Party!".equals(commandLine[0])) {
  18.             String command = commandLine[0];
  19.             Predicate<String> validate = getPredicate(commandLine);
  20.  
  21.             switch (command) {
  22.                 case "Remove":
  23.                     guestList.removeIf(validate);
  24.                     break;
  25.                 case "Double":
  26.                     List<String> temp = new ArrayList<>();
  27.                     guestList.forEach(guest -> {
  28.                         if (validate.test(guest)) {
  29.                             temp.add(guest);
  30.                         }
  31.                         temp.add(guest);
  32.                     });
  33.                     guestList = temp;
  34.                     break;
  35.             }
  36.  
  37.             commandLine = reader.readLine().split(" ");
  38.         }
  39.  
  40.         if (guestList.isEmpty()) {
  41.             System.out.println("Nobody is going to the party!");
  42.             return;
  43.         }
  44.         System.out.println(String.join(", ", guestList) + " are going to the party!");
  45.     }
  46.  
  47.     public static Predicate<String> getPredicate(String[] commandLine) {
  48.         String command = commandLine[1];
  49.         switch (command) {
  50.             case "StartsWith":
  51.                 return x -> x.indexOf(commandLine[2]) == 0;
  52.             case "EndsWith":
  53.                 return x -> x.lastIndexOf(commandLine[2]) == x.length() - commandLine[2].length();
  54.             case "Length":
  55.                 return x -> x.length() == Integer.parseInt(commandLine[2]);
  56.             default:
  57.                 return null;
  58.         }
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment