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