Didart

ForceBook

Nov 7th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ForceBook {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.  
  9.         Map<String, List<String>> forceBook = new LinkedHashMap<>();
  10.        
  11.         while (!input.equals("Lumpawaroo")) {
  12.  
  13.             if (input.contains("|")) {
  14.  
  15.                 String forceSide = input.split("\\s+\\|\\s+")[0];
  16.                 String forceUser = input.split("\\s++\\|\\s+")[1];
  17.  
  18.                 forceBook.putIfAbsent(forceSide, new ArrayList<>());
  19.  
  20.                 boolean isExist = false;
  21.                 for (List<String> list : forceBook.values()) {
  22.                     if (list.contains(forceUser)) {
  23.                         isExist = true;
  24.                     }
  25.                 }
  26.                 if (!isExist) {
  27.                     forceBook.get(forceSide).add(forceUser);
  28.                 }
  29.             } else if (input.contains(" -> ")) {
  30.                 String forceUser = input.split("\\s+->\\s+")[0];
  31.                 String forceSide = input.split("\\s+->\\s+")[1];
  32.  
  33.                 forceBook.entrySet().forEach(entry -> entry.getValue().remove(forceUser));
  34.                 forceBook.putIfAbsent(forceSide, new ArrayList<>());
  35.  
  36.                 forceBook.get(forceSide).add(forceUser);
  37.                 System.out.printf("%s joins the %s side!%n", forceUser, forceSide);
  38.             }
  39.             input = scanner.nextLine();
  40.         }
  41.         forceBook.entrySet()
  42.                 .stream()
  43.                 .filter(entry -> entry.getValue().size() > 0)
  44.                 .sorted((e1, e2) -> {
  45.                     int sortedResult = Integer.compare(e2.getValue().size(), e1.getValue().size());
  46.  
  47.                     if (sortedResult == 0) {
  48.                         sortedResult = e1.getKey().compareTo(e2.getKey());
  49.                     }
  50.                     return sortedResult;
  51.                 }).forEach(entry -> {
  52.             System.out.printf("Side: %s, Members: %d%n", entry.getKey(), entry.getValue().size());
  53.             entry.getValue()
  54.                     .stream()
  55.                     .sorted()
  56.                     .forEach(user -> System.out.println("! " + user));
  57.         });
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment