Advertisement
Guest User

Untitled

a guest
May 25th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.function.Predicate;
  3. import java.util.stream.Collectors;
  4.  
  5. public class Predicate_Party {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         ArrayList<String> guests = Arrays.stream(scanner.nextLine().split("\\s+"))
  10.                 .collect(Collectors.toCollection(ArrayList::new));
  11.         Map<String, Integer> guestsOccurrences = initializeMap(guests);
  12.  
  13.         String line = scanner.nextLine();
  14.  
  15.         while (!"Party!".equals(line)) {
  16.             String[] tokens = line.split("\\s+");
  17.             String command = tokens[0];
  18.             String criteria = tokens[1];
  19.             String parameter = tokens[2];
  20.  
  21.             Predicate<String> predicate = null;
  22.  
  23.             switch (criteria) {
  24.                 case "StartsWith":
  25.                     predicate = name -> name.startsWith(parameter);
  26.                     break;
  27.                 case "EndsWith":
  28.                     predicate = name -> name.endsWith(parameter);
  29.                     break;
  30.                 case "Length":
  31.                     predicate = name -> name.length() == Integer.parseInt(parameter);
  32.                     break;
  33.             }
  34.  
  35.             Predicate<String> finalPredicate = predicate;
  36.  
  37.             if (command.equals("Double")) {
  38.                 guestsOccurrences
  39.                         .forEach((name, value) -> {
  40.                             if (finalPredicate != null && finalPredicate.test(name)) {
  41.                                 guestsOccurrences.put(name, guestsOccurrences.get(name) * 2);
  42.                             }
  43.                         });
  44.             } else if (command.equals("Remove")) {
  45.                 ArrayList<String> guestsToRemove = new ArrayList<>();
  46.                 guestsOccurrences
  47.                         .forEach((name, value) -> {
  48.                             if (finalPredicate != null && finalPredicate.test(name)) {
  49.                                 guestsToRemove.add(name);
  50.                             }
  51.                         });
  52.  
  53.                 for (String currentGuest : guestsToRemove) {
  54.                     guestsOccurrences.remove(currentGuest);
  55.                     guests.remove(currentGuest);
  56.                 }
  57.             }
  58.             line = scanner.nextLine();
  59.         }
  60.  
  61.         if (guestsOccurrences.size() != 0) {
  62.             Collections.sort(guests);
  63.             StringBuilder allGuest = new StringBuilder();
  64.  
  65.             for (String guest : guests) {
  66.                 int count = guestsOccurrences.get(guest);
  67.                 while (count > 0) {
  68.                     allGuest.append(String.format("%s, ", guest));
  69.                     count--;
  70.                 }
  71.             }
  72.  
  73.             allGuest = new StringBuilder(allGuest.substring(0, allGuest.lastIndexOf(",")));
  74.  
  75.             System.out.println(String.format("%s are going to the party!", allGuest.toString()));
  76.         } else {
  77.             System.out.println("Nobody is going to the party!");
  78.         }
  79.     }
  80.  
  81.     private static Map<String, Integer> initializeMap(ArrayList<String> guests) {
  82.         Map<String, Integer> map = new HashMap<>();
  83.         for (int i = 0; i < guests.size(); i++) {
  84.             if (!map.containsKey(guests.get(i))) {
  85.                 map.put(guests.get(i), 1);
  86.             } else {
  87.                 map.put(guests.get(i), map.get(guests.get(i)) + 1);
  88.             }
  89.         }
  90.         return map;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement