Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3. import java.util.Map;
  4.  
  5.  
  6. public class izpit {
  7.  
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. Map<String, Integer> comments = new HashMap<String, Integer>();
  12. Map<String, Integer> likes = new HashMap<String, Integer>();
  13.  
  14. String input = scanner.nextLine();
  15.  
  16. while (!"Log out".equals(input)){
  17. input = scanner.nextLine();
  18. String[] tokens = input.split(": ");
  19. String command = tokens[0];
  20. String username = tokens[1];
  21.  
  22. switch (command){
  23. case "New follower":
  24. comments.putIfAbsent(username, 0);
  25. likes.putIfAbsent(username, 0);
  26. break;
  27. case "Like":
  28. Integer newLikes = Integer.parseInt(tokens[2]);
  29. if (likes.containsKey(username)){
  30. int currentLikes = likes.get(username);
  31. likes.put(username, currentLikes + newLikes) ;
  32. }else{
  33. likes.put(username, newLikes);
  34. comments.put(username, 0);
  35. }
  36. break;
  37. case "Comment":
  38. Integer currentComments = comments.get(username);
  39. if (currentComments != null){
  40. comments.put(username, currentComments + 1);
  41. }else{
  42. comments.put(username, 1);
  43. likes.put(username, 0);
  44. }
  45. break;
  46. case "Blocked":
  47. if (comments.containsKey(username)){
  48. comments.remove(username);
  49. likes.remove(username);
  50. }else {
  51. System.out.printf("%s doesn't exist.%n", username);
  52. }
  53. break;
  54. }
  55.  
  56. }
  57. likes.entrySet()
  58. .stream()
  59. .sorted((a,b) -> {
  60. int result = b.getValue().compareTo(a.getValue()) ;
  61. if (result == 0){
  62. result = a.getKey().compareTo(b.getKey());
  63. }
  64. return result;
  65. })
  66. .forEach(entry ->
  67. {
  68. String name = entry.getKey();
  69. int like = entry.getValue();
  70. int comment = comments.get(name);
  71. int sum = like + comment;
  72. System.out.println(String.format ("%s: %d", name , sum
  73. ));
  74. });
  75.  
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement