desislava_topuzakova

11. The Party Reservation Filter Module

Sep 30th, 2021
1,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.*;
  3. import java.util.function.*;
  4. import java.util.stream.Collectors;
  5. import java.util.stream.IntStream;
  6.  
  7. public class Demo {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         ArrayList<String> guests = Arrays.stream(scanner.nextLine().split("\\s+"))
  12.                 .collect(Collectors.toCollection(ArrayList::new));
  13.  
  14.         String line = scanner.nextLine();
  15.  
  16.         HashMap<String, Predicate> predicates = new HashMap<>();
  17.  
  18.         while (!line.equals("Print")){
  19.  
  20.             String[] tokens = line.substring(line.indexOf(";") + 1).split("([ ;]+)");
  21.             int index = 2;
  22.             if(tokens.length == 2){
  23.                 index = 1;
  24.             }
  25.             String name = tokens[0] + tokens[index];
  26.  
  27.             if(line.contains("Add")){
  28.                 Predicate<String> predicate = null;
  29.                switch (tokens[0]){
  30.                    case "Starts":
  31.                        predicate = s -> s.startsWith(tokens[2]);
  32.                        predicates.putIfAbsent(name, predicate);
  33.                        break;
  34.                    case "Ends":
  35.                        predicate = s -> s.endsWith(tokens[2]);
  36.                        predicates.putIfAbsent(name, predicate);
  37.                        break;
  38.                    case "Length":
  39.                        predicate = s -> s.length() == Integer.parseInt(tokens[2]);
  40.                        predicates.putIfAbsent(name, predicate);
  41.                        break;
  42.                    case "Contains":
  43.                        predicate = s -> s.contains(tokens[1]);
  44.                        predicates.putIfAbsent(name, predicate);
  45.                        break;
  46.                }
  47.             }else{
  48.                 predicates.remove(name);
  49.             }
  50.             line = scanner.nextLine();
  51.         }
  52.  
  53.         for (String guest : guests) {
  54.             boolean isGoing = true;
  55.  
  56.             for (String entry : predicates.keySet()) {
  57.                 if(predicates.get(entry).test(guest)){
  58.                     isGoing = false;
  59.                     break;
  60.                 }
  61.             }
  62.  
  63.             if(isGoing){
  64.                 System.out.print(guest + " ");
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment