Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ZadachiOtIzpita;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Followers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, int[]> followers = new LinkedHashMap<>(); // LinkedHashMap preserves insertion order
- String input = scanner.nextLine();
- while (!input.equals("Log out")) {
- String[] tokens = input.split(": ");
- switch (tokens[0]) {
- case "New follower":
- String username = tokens[1];
- if (!followers.containsKey(username)) {
- followers.put(username, new int[]{0, 0});
- }
- break;
- case "Like":
- String userLiked = tokens[1];
- int likesCount = Integer.parseInt(tokens[2]);
- followers.putIfAbsent(userLiked, new int[]{0, 0});
- followers.get(userLiked)[0] += likesCount;
- break;
- case "Comment":
- String userCommented = tokens[1];
- followers.putIfAbsent(userCommented, new int[]{0, 0});
- followers.get(userCommented)[1]++;
- break;
- case "Blocked":
- String userBlocked = tokens[1];
- if (followers.containsKey(userBlocked)) {
- followers.remove(userBlocked);
- } else {
- System.out.println(userBlocked + " doesn't exist.");
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println(followers.size() + " followers");
- for (Map.Entry<String, int[]> entry : followers.entrySet()) {
- System.out.printf("%s: %d%n", entry.getKey(), entry.getValue()[0] + entry.getValue()[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement