Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ForceBook {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Map<String, List<String>> book = new LinkedHashMap<>();
- Map<String, String> users = new HashMap<>();
- while (true) {
- String input = sc.nextLine();
- if (input.equalsIgnoreCase("Lumpawaroo")) break;
- String side = "", user = "";
- if (input.contains(" | ")) {
- String[] inputValues = input.split(" \\| ");
- side = inputValues[0].trim();
- user = inputValues[1].trim();
- if (!users.containsKey(user)) {
- users.put(user, side);
- if (!book.containsKey(side)) {
- book.put(side, new ArrayList<>());
- }
- book.get(side).add(user);
- }
- } else if (input.contains(" -> ")) {
- String[] inputValues = input.split(" -> ");
- side = inputValues[1].trim();
- user = inputValues[0].trim();
- if (users.containsKey(user)) {
- String oldSide = users.remove(user);
- book.get(oldSide).remove(user);
- book.get(side).add(user);
- users.put(user, side);
- } else {
- users.put(user, side);
- if (!book.containsKey(side)) {
- book.put(side, new ArrayList<>());
- }
- book.get(side).add(user);
- }
- System.out.printf("%s joins the %s side!%n", user, side);
- }
- }
- sc.close();
- for (Map.Entry<String, List<String>> force : book.entrySet()) {
- if (force.getValue().size() > 0) {
- System.out.printf("Side: %s, Members: %s%n", force.getKey(), force.getValue().size());
- for (String user : force.getValue()) {
- System.out.println("! " + user);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment