Advertisement
meteor4o

JF-Maps-Exercise-08.Forcebook

Jul 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Forcebook {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         String input = sc.nextLine();
  11.         TreeMap<String, List> users = new TreeMap<String, List>();
  12.  
  13.  
  14.         while (!input.equals("Lumpawaroo")) {
  15.             String[] tokens = null;
  16.             boolean isPresent = false;
  17.  
  18.             if (input.contains("|")) {
  19.  
  20.                 tokens = input.split(" \\| ");
  21.                 String forceSide = tokens[0].trim();
  22.                 String forceUser = tokens[1].trim();
  23.  
  24.                 users.putIfAbsent(forceSide, new ArrayList());
  25.  
  26.                 for (Map.Entry<String, List> entry : users.entrySet()) {
  27.                     if (entry.getValue().contains(forceUser)) {
  28.                         isPresent = true;
  29.                     }
  30.                 }
  31.  
  32.                 if (!isPresent) {
  33.                     users.get(forceSide).add(forceUser);
  34.                 }
  35.  
  36.             } else if (input.contains("->")) {
  37.                 tokens = input.split("\\s->\\s");
  38.                 String forceSide = tokens[1];
  39.                 String forceUser = tokens[0];
  40.  
  41.  
  42.                 for (Map.Entry<String, List> entry : users.entrySet()) {
  43.                     if (entry.getValue().contains(forceUser)) {
  44.                         entry.getValue().remove(forceUser);
  45.                         users.putIfAbsent(forceSide, new ArrayList());
  46.                         users.get(forceSide).add(forceUser);
  47.                         isPresent = true;
  48.                         break;
  49.                     }
  50.                 }
  51.  
  52.  
  53.                 if (!isPresent) {
  54.                     users.putIfAbsent(forceSide, new ArrayList());
  55.                     users.get(forceSide).add(forceUser);
  56.                 }
  57.  
  58.                 System.out.printf("%s joins the %s side!%n", forceUser, forceSide);
  59.  
  60.             }
  61.         input = sc.nextLine();
  62.         }
  63.  
  64.         users.entrySet().stream().sorted((a, b) -> {
  65.             int result = b.getValue().size() - a.getValue().size();
  66.  
  67.             if (result == 0) {
  68.                 result = a.getKey().compareTo(b.getKey());
  69.             }
  70.          return result;
  71.         }).forEach(entry -> {
  72.             if (entry.getValue().size()!= 0) {
  73.             System.out.printf("Side: %s, Members: %d%n", entry.getKey(), entry.getValue().size());
  74.  
  75.             entry.getValue().stream().sorted()
  76.                     .forEach(user -> System.out.println("! " + user));
  77.             }});
  78.  
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement