Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Scanner;
- public class Followers {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- HashMap<String, Integer> records = new HashMap<>();
- String input = scan.nextLine();
- while (!"Log out".equals(input)) {
- String[] tokens = input.split("\\: ");
- String command = tokens[0];
- String username = tokens[1];
- switch (command) {
- case "New follower":
- if (!records.containsKey(username)) {
- records.put(username, 0);
- }
- break;
- case "Like": {
- int newCount = Integer.parseInt(tokens[2]);
- records.putIfAbsent(username, 0);
- records.put(username, records.get(username) + newCount);
- }
- break;
- case "Comment":
- records.putIfAbsent(username, 0);
- records.put(username, records.get(username) + 1);
- break;
- case "Blocked":
- if (records.containsKey(username)) {
- records.remove(username);
- } else {
- System.out.println(String.format("%s doesn't exist.", username));
- }
- break;
- }
- input = scan.nextLine();
- }
- System.out.println(records.size() + " followers");
- records
- .entrySet()
- .stream()
- .sorted((v1, v2) -> {
- int result = v2.getValue() - v1.getValue();
- if (result == 0) {
- result = v1.getKey().compareTo(v2.getKey());
- }
- return result;
- })
- .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement