Advertisement
svephoto

Followers with Likes and Comments [Java]

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