Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class FollowersWithLikesAndComments {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Integer> followersWithLikesAndComments = new TreeMap<>();
- String command;
- while (!"Log out".equals(command = scanner.nextLine())) {
- String[] tokens = command.split(": ");
- String currentCommand = tokens[0];
- String username = tokens[1];
- switch (currentCommand) {
- case "New follower":
- if (!followersWithLikesAndComments.containsKey(username)) {
- followersWithLikesAndComments.put(username, 0);
- }
- break;
- case "Like":
- int countOfLikes = Integer.parseInt(tokens[2]);
- if (!followersWithLikesAndComments.containsKey(username)) {
- followersWithLikesAndComments.put(username, countOfLikes);
- } else {
- followersWithLikesAndComments.put(username, followersWithLikesAndComments.get(username) + countOfLikes);
- }
- break;
- case "Comment":
- if (!followersWithLikesAndComments.containsKey(username)) {
- followersWithLikesAndComments.put(username, 1);
- } else {
- followersWithLikesAndComments.put(username, followersWithLikesAndComments.get(username) + 1);
- }
- break;
- case "Blocked":
- if (followersWithLikesAndComments.containsKey(username)) {
- followersWithLikesAndComments.remove(username);
- } else {
- System.out.printf("%s doesn't exist.%n", username);
- }
- break;
- }
- }
- System.out.printf("%d followers%n", followersWithLikesAndComments.size());
- followersWithLikesAndComments.entrySet().stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue()
- .reversed()
- .thenComparing(Map.Entry::getKey))
- .forEach(entry -> System.out.printf("%s: %d%n", entry.getKey(), entry.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement