Advertisement
SIRAKOV4444

Untitled

Apr 2nd, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class lqlq {
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. Map<String,Integer> Likes=new LinkedHashMap<>();
  12. Map<String,Integer> Comments=new LinkedHashMap<>();
  13. Map<String,Integer> toPrint=new LinkedHashMap<>();
  14. //int follower=0;
  15. String input=sc.nextLine();
  16. while (!input.equals("Log out")){
  17.  
  18. String[]tokens=input.split(": ");
  19. String command=tokens[0];
  20.  
  21. switch (command){
  22. case"New follower":
  23. String username1=tokens[1];//username
  24. //follower+=1;
  25. if(!Likes.containsKey(username1)){
  26. Likes.put(username1,0);
  27. }
  28. if(!Comments.containsKey(username1)){
  29. Comments.put(username1,0);
  30. }
  31. break;
  32. case"Like":
  33. String username2=tokens[1];
  34. int count = Integer.parseInt(tokens[2]);
  35. if(!Likes.containsKey(username2)){
  36. Likes.put(username2,count);
  37. }else{
  38. int currentCount = Likes.get(username2);
  39. Likes.put(username2,(currentCount+count));//remember
  40. }
  41. break;
  42. case "Comment":
  43. String username3=tokens[1];
  44. if(!Comments.containsKey(username3)){
  45. Comments.put(username3,1);
  46. }else{
  47. int commentCount=Comments.get(username3);
  48. Comments.put(username3,(commentCount+1));
  49. }
  50. break;
  51. case"Blocked":
  52. String username4=tokens[1];
  53.  
  54. if(!Comments.containsKey(username4) ){
  55. System.out.println(String.format("%s doesn't exist.",username4));
  56. }else{
  57. Comments.remove(username4);
  58. }
  59. if(!Likes.containsKey(username4) ){
  60. System.out.println(String.format("%s doesn't exist.",username4));
  61. }else{
  62. Likes.remove(username4);
  63. }
  64. break;
  65. }
  66. input=sc.nextLine();
  67. }
  68. //System.out.println(String.format("%d followers",follower));
  69. for (Map.Entry<String, Integer> comment : Comments.entrySet()) {
  70. if (!toPrint.containsKey(comment.getKey())) {
  71. toPrint.put(comment.getKey(), comment.getValue());
  72. }
  73. else {
  74. int old = toPrint.get(comment.getKey());
  75. toPrint.put(comment.getKey(), old + comment.getValue());
  76. }
  77. }
  78. for (Map.Entry<String, Integer> like : Likes.entrySet()) {
  79. if (!toPrint.containsKey(like.getKey())) {
  80. toPrint.put(like.getKey(), like.getValue());
  81. } else {
  82. int old = toPrint.get(like.getKey());
  83. toPrint.put(like.getKey(), old + like.getValue());
  84. }
  85. }
  86. System.out.println(toPrint.size() + " followers");//OK TODO
  87.  
  88. toPrint.entrySet().stream().sorted((a,b) -> {
  89. int compared = b.getValue().compareTo(a.getValue());
  90.  
  91. if (compared == 0) {
  92. compared = a.getKey().compareTo(b.getKey());
  93. }
  94. return compared;
  95.  
  96. }).forEach(entry -> {
  97. System.out.println(String.format("%s: %d", entry.getKey(), entry.getValue()));
  98. });
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement