RGAlex

Test 3

Apr 3rd, 2021 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class test {
  8.     static Map<String, Integer> likes = new HashMap<>();
  9.     static Map<String, Integer> comments = new HashMap<>();
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         String input;
  15.  
  16.         while (!"Log out".equals(input = scanner.nextLine())) {
  17.             String[] tokens = input.split(": ");
  18.  
  19.             switch (tokens[0]) {
  20.                 case "New follower":
  21.                     add(tokens[1]);
  22.                     break;
  23.                 case "Like":
  24.                     like(tokens[1], Integer.parseInt(tokens[2]));
  25.                     break;
  26.                 case "Comment":
  27.                     comment(tokens[1]);
  28.                     break;
  29.                 case "Blocked":
  30.                     blocked(tokens[1]);
  31.                     break;
  32.             }
  33.         }
  34.         List<String> names = likes.entrySet().stream()
  35.                 .sorted((entryA, entryB) -> {
  36.                     int likesA = entryA.getValue();
  37.                     String nameA = entryA.getKey();
  38.                     int likesB = entryB.getValue();
  39.                     String nameB = entryB.getKey();
  40.  
  41.                     if (likesA != likesB) {
  42.                         return Integer.compare(likesB, likesA);
  43.                     } else {
  44.                         return nameA.compareTo(nameB);
  45.                     }
  46.                 })
  47.                 .map(entry -> entry.getKey())
  48.                 .collect(Collectors.toList());
  49.         System.out.printf("%d followers\n", names.size());
  50.         for (String name : names) {
  51.             System.out.printf("%s: %d\n", name, likes.get(name) + comments.get(name));
  52.         }
  53.  
  54.     }
  55.  
  56.     private static void blocked(String username) {
  57.         if (!likes.containsKey(username)) {
  58.             System.out.printf("%s doesn't exist.\n", username);
  59.         } else {
  60.             likes.remove(username);
  61.             comments.remove(username);
  62.         }
  63.     }
  64.  
  65.     private static void comment(String username) {
  66.         if (!comments.containsKey(username)) {
  67.             comments.put(username, 1);
  68.             likes.put(username, 0);
  69.         } else {
  70.             comments.put(username, comments.get(username) + 1);
  71.         }
  72.     }
  73.  
  74.     private static void like(String username, int count) {
  75.         if (!likes.containsKey(username)) {
  76.             likes.put(username, count);
  77.             comments.put(username, 0);
  78.         } else {
  79.             likes.put(username, likes.get(username) + count);
  80.         }
  81.     }
  82.  
  83.     private static void add(String username) {
  84.         if (!isItContained(username)) {
  85.             likes.put(username, 0);
  86.             comments.put(username, 0);
  87.         }
  88.     }
  89.  
  90.     private static boolean isItContained(String username) {
  91.         if (likes.containsKey(username)) {
  92.             return true;
  93.         }
  94.         return false;
  95.     }
  96. }
Add Comment
Please, Sign In to add comment