Advertisement
nikeza

11.The Party Reservation Filter Module

Oct 8th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import javax.naming.ldap.SortResponseControl;
  2. import java.util.*;
  3. import java.util.function.Predicate;
  4. import java.util.stream.Collectors;
  5.  
  6. public class functional_Exercises_11The_Party_Reservation_Filter_Module {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> names = Arrays.stream(scanner.nextLine().split("\\s+"))
  11.                 .collect(Collectors.toList());
  12.         String input = scanner.nextLine();
  13.         Map<String, List<String>> command = new LinkedHashMap<>();
  14.         while (!input.equals("Print")) {
  15.             String[] tokens = input.split(";");
  16.             String typeFilter = tokens[1];
  17.             String param = tokens[2];
  18.  
  19.             if (tokens[0].contains("Add")) {
  20.                 command.putIfAbsent(typeFilter, new ArrayList<>());
  21.                 command.get(typeFilter).add(param);
  22.             } else {
  23.                 if (command.get(typeFilter).size() > 1) {
  24.                     command.get(typeFilter).remove(param);
  25.                 } else {
  26.                     command.remove(typeFilter);
  27.                 }
  28.             }
  29.             input = scanner.nextLine();
  30.         }
  31.      
  32.  
  33.         for (String name : prdicateFilter(names, command)) {
  34.             System.out.print(name + " ");
  35.         }
  36. // ->method to do
  37.     }
  38.  
  39.     private static List<String> prdicateFilter(List<String> names, Map<String, List<String>> command) {
  40.         List<String> nameChanged = null;
  41.         for (Map.Entry<String, List<String>> entry : command.entrySet()) {
  42.             for (String param : entry.getValue()) {
  43.                 if (entry.getKey().equals("Starts with")) {
  44.                     names = names.stream()
  45.                             .filter(f -> !f.startsWith(param))
  46.                             .collect(Collectors.toList());
  47.  
  48.                 } else if (entry.getKey().equals("Ends with")) {
  49.                     names = names.stream()
  50.                             .filter(f -> !f.endsWith(param))
  51.                             .collect(Collectors.toList());
  52.                 } else if (entry.getKey().equals("Length")) {
  53.                     names = names.stream()
  54.                             .filter(f -> f.length() != Integer.parseInt(param))
  55.                             .collect(Collectors.toList());
  56.  
  57.                 } else if (entry.getKey().equals("Contains")) {
  58.                     names = names.stream()
  59.                             .filter(f -> !f.contains(param))
  60.                             .collect(Collectors.toList());
  61.  
  62.                 }
  63.             }
  64.         }
  65.  
  66.         return names;
  67.  
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement