Advertisement
Guest User

Followers

a guest
Mar 29th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package FirstExam;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7.  
  8. public class Followers {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         String command = scanner.nextLine();
  12.         TreeMap<String, ArrayList<Integer>> people = new TreeMap<>();
  13.         while (!"Log out".equals(command)) {
  14.             String[] tokens = command.split(": ");
  15.             switch (tokens[0]) {
  16.                 case "New follower":
  17.                     String username = tokens[1];
  18.                     if (!people.containsKey(username)) {
  19.                         people.put(username, new ArrayList<>());
  20.                         people.get(username).add(0, 0);
  21.                         people.get(username).add(1, 0);
  22.                     }
  23.                     break;
  24.                 case "Like":
  25.                     username = tokens[1];
  26.                     int count = Integer.parseInt(tokens[2]);
  27.                     if (!people.containsKey(username)) {
  28.                         people.put(username, new ArrayList<>());
  29.                         people.get(username).add(0, count);
  30.                         people.get(username).add(1, 0);
  31.                     } else {
  32.                         int newCount = people.get(username).get(0) + count;
  33.                         people.get(username).set(0, newCount);
  34.                     }
  35.                     break;
  36.                 case "Comment":
  37.                     username = tokens[1];
  38.                     int comment = 1;
  39.                     if (!people.containsKey(username)) {
  40.                         people.put(username, new ArrayList<>());
  41.                         people.get(username).add(0, 0);
  42.                         people.get(username).add(1, comment);
  43.                     } else {
  44.                         int newCount = people.get(username).get(1) + comment;
  45.                         people.get(username).set(1, newCount);
  46.                     }
  47.                     break;
  48.                 case "Blocked":
  49.                     username = tokens[1];
  50.                     if (people.containsKey(username)) {
  51.                         people.remove(username);
  52.                     } else System.out.printf("%s doesn't exist.%n", username);
  53.                     break;
  54.             }
  55.             command = scanner.nextLine();
  56.         }
  57.         System.out.println(people.size() + " followers");
  58.         people
  59.                 .entrySet()
  60.                 .stream()
  61.                 .sorted((c1, c2) -> {
  62.                     int count1 = c1.getValue().get(0);
  63.                     int count2 = c2.getValue().get(0);
  64.                     return Integer.compare(count2, count1);
  65.                 })
  66.                 .forEach(p -> {
  67.                     System.out.printf("%s: ", p.getKey());
  68.                     int sum = p.getValue().get(0) + p.getValue().get(1);
  69.                     System.out.println(sum);
  70.                 });
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement