Advertisement
Ligh7_of_H3av3n

03. Followers

Mar 31st, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package ZadachiOtIzpita;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Followers {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         Map<String, int[]> followers = new LinkedHashMap<>(); // LinkedHashMap preserves insertion order
  13.  
  14.         String input = scanner.nextLine();
  15.  
  16.         while (!input.equals("Log out")) {
  17.             String[] tokens = input.split(": ");
  18.  
  19.             switch (tokens[0]) {
  20.                 case "New follower":
  21.                     String username = tokens[1];
  22.                     if (!followers.containsKey(username)) {
  23.                         followers.put(username, new int[]{0, 0});
  24.                     }
  25.                     break;
  26.                 case "Like":
  27.                     String userLiked = tokens[1];
  28.                     int likesCount = Integer.parseInt(tokens[2]);
  29.                     followers.putIfAbsent(userLiked, new int[]{0, 0});
  30.                     followers.get(userLiked)[0] += likesCount;
  31.                     break;
  32.                 case "Comment":
  33.                     String userCommented = tokens[1];
  34.                     followers.putIfAbsent(userCommented, new int[]{0, 0});
  35.                     followers.get(userCommented)[1]++;
  36.                     break;
  37.                 case "Blocked":
  38.                     String userBlocked = tokens[1];
  39.                     if (followers.containsKey(userBlocked)) {
  40.                         followers.remove(userBlocked);
  41.                     } else {
  42.                         System.out.println(userBlocked + " doesn't exist.");
  43.                     }
  44.                     break;
  45.             }
  46.  
  47.             input = scanner.nextLine();
  48.         }
  49.  
  50.         System.out.println(followers.size() + " followers");
  51.  
  52.         for (Map.Entry<String, int[]> entry : followers.entrySet()) {
  53.             System.out.printf("%s: %d%n", entry.getKey(), entry.getValue()[0] + entry.getValue()[1]);
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement