Advertisement
Guest User

Followers

a guest
Mar 28th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Followers {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Map<String, Integer> comments = new LinkedHashMap<>();
  9.         Map<String, Integer> likes = new LinkedHashMap<>();
  10.  
  11.         String input = scanner.nextLine();
  12.  
  13.         while (!"Log out".equals(input)) {
  14.             String[] tokens = input.split(":\\s+");
  15.             String command = tokens[0];
  16.             String username = tokens[1];
  17.  
  18.             switch (command) {
  19.                 case "New follower":
  20.                     comments.putIfAbsent(username, 0);
  21.                     likes.putIfAbsent(username, 0);
  22.                     break;
  23.                 case "Like":
  24.                     Integer currentLikes = likes.get(username);
  25.                     int newLikes = Integer.parseInt(tokens[2]);
  26.  
  27.                     if (currentLikes != null) {
  28.                         likes.put(username, currentLikes + newLikes);
  29.                     } else {
  30.                         likes.put(username, newLikes);
  31.                         comments.put(username, 0);
  32.                     }
  33.                     break;
  34.                 case "Comment":
  35.                     Integer currentComments = comments.get(username);
  36.                     if (currentComments != null) {
  37.                         comments.put(username, currentComments + 1);
  38.                     } else {
  39.                         comments.put(username, 1);
  40.                         likes.put(username, 0);
  41.                     }
  42.                     break;
  43.                 case "Blocked":
  44.                     if (comments.containsKey(username)) {
  45.                         comments.remove(username);
  46.                         likes.remove(username);
  47.                     } else {
  48.                         System.out.println(String.format("%s doesn't exist.", username));
  49.                     }
  50.                     break;
  51.                 default:
  52.                     throw new IllegalStateException("Unknown command" + command);
  53.             }
  54.             input = scanner.nextLine();
  55.         }
  56.         System.out.println(String.format("%d followers", likes.size()));
  57.         likes
  58.                 .entrySet()
  59.                 .stream()
  60.                 .sorted((a, b) -> {
  61.                     int result = b.getValue().compareTo(a.getValue());
  62.                     if (result == 0) {
  63.                         result = a.getKey().compareTo(b.getKey());
  64.                     }
  65.                     return result;
  66.                 })
  67.                 .forEach(entry -> {
  68.                     String name = entry.getKey();
  69.                     int like = entry.getValue();
  70.                     int comment = comments.get(name);
  71.                     int sum = like + comment;
  72.                     System.out.println(String.format("%s: %d", name, sum));
  73.                 });
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement