Advertisement
Kent_St_1

Untitled

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