Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class lqlq {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Map<String,Integer> Likes=new LinkedHashMap<>();
- Map<String,Integer> Comments=new LinkedHashMap<>();
- Map<String,Integer> toPrint=new LinkedHashMap<>();
- //int follower=0;
- String input=sc.nextLine();
- while (!input.equals("Log out")){
- String[]tokens=input.split(": ");
- String command=tokens[0];
- switch (command){
- case"New follower":
- String username1=tokens[1];//username
- //follower+=1;
- if(!Likes.containsKey(username1)){
- Likes.put(username1,0);
- }
- if(!Comments.containsKey(username1)){
- Comments.put(username1,0);
- }
- break;
- case"Like":
- String username2=tokens[1];
- int count = Integer.parseInt(tokens[2]);
- if(!Likes.containsKey(username2)){
- Likes.put(username2,count);
- }else{
- int currentCount = Likes.get(username2);
- Likes.put(username2,(currentCount+count));//remember
- }
- break;
- case "Comment":
- String username3=tokens[1];
- if(!Comments.containsKey(username3)){
- Comments.put(username3,1);
- }else{
- int commentCount=Comments.get(username3);
- Comments.put(username3,(commentCount+1));
- }
- break;
- case"Blocked":
- String username4=tokens[1];
- if(!Comments.containsKey(username4) ){
- System.out.println(String.format("%s doesn't exist.",username4));
- }else{
- Comments.remove(username4);
- }
- if(!Likes.containsKey(username4) ){
- System.out.println(String.format("%s doesn't exist.",username4));
- }else{
- Likes.remove(username4);
- }
- break;
- }
- input=sc.nextLine();
- }
- //System.out.println(String.format("%d followers",follower));
- for (Map.Entry<String, Integer> comment : Comments.entrySet()) {
- if (!toPrint.containsKey(comment.getKey())) {
- toPrint.put(comment.getKey(), comment.getValue());
- }
- else {
- int old = toPrint.get(comment.getKey());
- toPrint.put(comment.getKey(), old + comment.getValue());
- }
- }
- for (Map.Entry<String, Integer> like : Likes.entrySet()) {
- if (!toPrint.containsKey(like.getKey())) {
- toPrint.put(like.getKey(), like.getValue());
- } else {
- int old = toPrint.get(like.getKey());
- toPrint.put(like.getKey(), old + like.getValue());
- }
- }
- System.out.println(toPrint.size() + " followers");//OK TODO
- toPrint.entrySet().stream().sorted((a,b) -> {
- int compared = b.getValue().compareTo(a.getValue());
- if (compared == 0) {
- compared = a.getKey().compareTo(b.getKey());
- }
- return compared;
- }).forEach(entry -> {
- System.out.println(String.format("%s: %d", entry.getKey(), entry.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement