Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Forcebook {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input = sc.nextLine();
- TreeMap<String, List> users = new TreeMap<String, List>();
- while (!input.equals("Lumpawaroo")) {
- String[] tokens = null;
- boolean isPresent = false;
- if (input.contains("|")) {
- tokens = input.split(" \\| ");
- String forceSide = tokens[0].trim();
- String forceUser = tokens[1].trim();
- users.putIfAbsent(forceSide, new ArrayList());
- for (Map.Entry<String, List> entry : users.entrySet()) {
- if (entry.getValue().contains(forceUser)) {
- isPresent = true;
- }
- }
- if (!isPresent) {
- users.get(forceSide).add(forceUser);
- }
- } else if (input.contains("->")) {
- tokens = input.split("\\s->\\s");
- String forceSide = tokens[1];
- String forceUser = tokens[0];
- for (Map.Entry<String, List> entry : users.entrySet()) {
- if (entry.getValue().contains(forceUser)) {
- entry.getValue().remove(forceUser);
- users.putIfAbsent(forceSide, new ArrayList());
- users.get(forceSide).add(forceUser);
- isPresent = true;
- break;
- }
- }
- if (!isPresent) {
- users.putIfAbsent(forceSide, new ArrayList());
- users.get(forceSide).add(forceUser);
- }
- System.out.printf("%s joins the %s side!%n", forceUser, forceSide);
- }
- input = sc.nextLine();
- }
- users.entrySet().stream().sorted((a, b) -> {
- int result = b.getValue().size() - a.getValue().size();
- if (result == 0) {
- result = a.getKey().compareTo(b.getKey());
- }
- return result;
- }).forEach(entry -> {
- if (entry.getValue().size()!= 0) {
- System.out.printf("Side: %s, Members: %d%n", entry.getKey(), entry.getValue().size());
- entry.getValue().stream().sorted()
- .forEach(user -> System.out.println("! " + user));
- }});
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement