Advertisement
Guest User

Fo

a guest
Apr 3rd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Followers {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         Map<String, int[]> usernameLikesCommentsMap = new TreeMap<>();
  9.  
  10.         String command;
  11.         while (!"Log out".equals(command = scanner.nextLine())) {
  12.             String[] tokens = command.split(": ");
  13.  
  14.             switch (tokens[0]) {
  15.                 case "New follower":
  16.                     String username = tokens[1];
  17.                     usernameLikesCommentsMap.putIfAbsent(username, new int[2]);
  18.                     break;
  19.                 case "Like":
  20.                     String usernameLike = tokens[1];
  21.                     int count = Integer.parseInt(tokens[2]);
  22.                     if (!usernameLikesCommentsMap.containsKey(usernameLike)) {
  23.                         usernameLikesCommentsMap.put(usernameLike, new int[2]);
  24.                         usernameLikesCommentsMap.get(usernameLike)[0] = count;
  25.                     } else {
  26.                         int currentCount = usernameLikesCommentsMap.get(usernameLike)[0];
  27.                         usernameLikesCommentsMap.get(usernameLike)[0] = currentCount + count;
  28.                     }
  29.                     break;
  30.                 case "Comment":
  31.                     String usernameComment = tokens[1];
  32.                     if (!usernameLikesCommentsMap.containsKey(usernameComment)) {
  33.                         usernameLikesCommentsMap.put(usernameComment, new int[2]);
  34.                         usernameLikesCommentsMap.get(usernameComment)[1] = 1;
  35.                     } else {
  36.                         int currentComments = usernameLikesCommentsMap.get(usernameComment)[1];
  37.                         usernameLikesCommentsMap.get(usernameComment)[1] = currentComments + 1;
  38.                     }
  39.                     break;
  40.                 case "Blocked":
  41.                     String blockedUsername = tokens[1];
  42.                     if (usernameLikesCommentsMap.containsKey(blockedUsername)) {
  43.                         usernameLikesCommentsMap.remove(blockedUsername);
  44.                     } else {
  45.                         System.out.printf("%s doesn't exist.%n", blockedUsername);
  46.                     }
  47.                     break;
  48.             }
  49.         }
  50.         System.out.printf("%d followers%n", usernameLikesCommentsMap.size());
  51.         usernameLikesCommentsMap
  52.                 .entrySet()
  53.                 .stream()
  54.                 .sorted((a, b) -> b.getValue()[0] - a.getValue()[0])
  55.                 .forEach(entry -> {
  56.                     System.out.printf("%s: %d%n", entry.getKey(), entry.getValue()[0] + entry.getValue()[1]);
  57.                 });
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement