Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class examPrepZad3Followers {
- static Map<String,Integer> likes = new HashMap<>();
- static Map<String,Integer> comments = new HashMap<>();
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String commandString;
- while (!(commandString=scanner.nextLine()).equals("Log out")){
- String[] commands= commandString.split("\\s+|:\\s+");
- switch (commands[0]){
- case "New":
- add(commands[2]);
- break;
- case "Like":
- addLike(commands[1],Integer.parseInt(commands[2]));
- break;
- case "Comment":
- addComment(commands[1]);
- break;
- case "Blocked":
- remove(commands[1]);
- break;
- }
- }
- System.out.println(likes.size()+" followers");
- likes
- .entrySet()
- .stream()
- .sorted((a,b)->{
- if (a.getValue() != b.getValue()) {
- return Integer.compare(b.getValue(),a.getValue());
- } else {
- return a.getKey().compareTo(b.getKey());
- }
- })
- .forEach((e)-> System.out.printf("%s: %d%n",e.getKey(),e.getValue() + comments.get(e.getKey())));
- }
- private static void remove(String user) {
- if (comments.containsKey(user)) {
- comments.remove(user);
- likes.remove(user);
- }else {
- System.out.println(user + " doesn't exist.");
- }
- }
- private static void addComment(String user) {
- comments.putIfAbsent(user,0);
- comments.put(user,comments.get(user)+1);
- likes.putIfAbsent(user,0);
- }
- private static void addLike(String user, int countLikes) {
- likes.putIfAbsent(user,0);
- likes.put(user,likes.get(user)+countLikes);
- comments.putIfAbsent(user,0);
- }
- private static void add(String user) {
- if (!comments.containsKey(user)) {
- comments.put(user,0);
- likes.put(user,0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement