Advertisement
Guest User

3. Followers

a guest
Mar 31st, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class Followers {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         HashMap<String, Integer> records = new HashMap<>();
  9.  
  10.         String input = scan.nextLine();
  11.         while (!"Log out".equals(input)) {
  12.             String[] tokens = input.split("\\: ");
  13.             String command = tokens[0];
  14.             String username = tokens[1];
  15.  
  16.             switch (command) {
  17.                 case "New follower":
  18.                     if (!records.containsKey(username)) {
  19.                         records.put(username, 0);
  20.                     }
  21.                     break;
  22.                 case "Like": {
  23.                     int newCount = Integer.parseInt(tokens[2]);
  24.                     records.putIfAbsent(username, 0);
  25.                     records.put(username, records.get(username) + newCount);
  26.                 }
  27.                     break;
  28.                 case "Comment":
  29.                     records.putIfAbsent(username, 0);
  30.                     records.put(username, records.get(username) + 1);
  31.  
  32.                     break;
  33.                 case "Blocked":
  34.                     if (records.containsKey(username)) {
  35.                         records.remove(username);
  36.  
  37.                     } else {
  38.                         System.out.println(String.format("%s doesn't exist.", username));
  39.                     }
  40.                     break;
  41.             }
  42.             input = scan.nextLine();
  43.         }
  44.         System.out.println(records.size() + " followers");
  45.         records
  46.                 .entrySet()
  47.                 .stream()
  48.                 .sorted((v1, v2) -> {
  49.                     int result = v2.getValue() - v1.getValue();
  50.                     if (result == 0) {
  51.                         result = v1.getKey().compareTo(v2.getKey());
  52.                     }
  53.                     return result;
  54.                 })
  55.                 .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement