Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FirstExam;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class Followers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String command = scanner.nextLine();
- TreeMap<String, ArrayList<Integer>> people = new TreeMap<>();
- while (!"Log out".equals(command)) {
- String[] tokens = command.split(": ");
- switch (tokens[0]) {
- case "New follower":
- String username = tokens[1];
- if (!people.containsKey(username)) {
- people.put(username, new ArrayList<>());
- people.get(username).add(0, 0);
- people.get(username).add(1, 0);
- }
- break;
- case "Like":
- username = tokens[1];
- int count = Integer.parseInt(tokens[2]);
- if (!people.containsKey(username)) {
- people.put(username, new ArrayList<>());
- people.get(username).add(0, count);
- people.get(username).add(1, 0);
- } else {
- int newCount = people.get(username).get(0) + count;
- people.get(username).set(0, newCount);
- }
- break;
- case "Comment":
- username = tokens[1];
- int comment = 1;
- if (!people.containsKey(username)) {
- people.put(username, new ArrayList<>());
- people.get(username).add(0, 0);
- people.get(username).add(1, comment);
- } else {
- int newCount = people.get(username).get(1) + comment;
- people.get(username).set(1, newCount);
- }
- break;
- case "Blocked":
- username = tokens[1];
- if (people.containsKey(username)) {
- people.remove(username);
- } else System.out.printf("%s doesn't exist.%n", username);
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println(people.size() + " followers");
- people
- .entrySet()
- .stream()
- .sorted((c1, c2) -> {
- int count1 = c1.getValue().get(0);
- int count2 = c2.getValue().get(0);
- return Integer.compare(count2, count1);
- })
- .forEach(p -> {
- System.out.printf("%s: ", p.getKey());
- int sum = p.getValue().get(0) + p.getValue().get(1);
- System.out.println(sum);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement