Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Followers {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. Map<String, List<Integer>> peshoFollowers = new HashMap<>();
  8.  
  9. String input = scanner.nextLine();
  10. while (!"Log out".equals(input)) {
  11. String[] tokens = input.split(":\\s+");
  12. String action = tokens[0];
  13. String username = tokens[1];
  14.  
  15. switch (action) {
  16. case "New follower":
  17. if (!peshoFollowers.containsKey(username)) {
  18. peshoFollowers.put(username, new ArrayList<>());
  19. peshoFollowers.get(username).add(0, 0); // likes
  20. peshoFollowers.get(username).add(1, 0); // comments
  21.  
  22. }
  23. break;
  24. case "Like":
  25. int newLikes = Integer.parseInt(tokens[2]);
  26.  
  27. if (!peshoFollowers.containsKey(username)) {
  28. peshoFollowers.put(username, new ArrayList<>());
  29. peshoFollowers.get(username).add(0, newLikes); // add the likes
  30. peshoFollowers.get(username).add(1, 0); // add 0 comments
  31.  
  32. } else {
  33. int oldLikes = peshoFollowers.get(username).get(0); // get current likes from List
  34. peshoFollowers.get(username).set(0, newLikes + oldLikes); // re-write the likes
  35. }
  36. break;
  37. case "Comment":
  38. int newComments = 1;
  39.  
  40. if (!peshoFollowers.containsKey(username)) {
  41. peshoFollowers.put(username, new ArrayList<>());
  42. peshoFollowers.get(username).add(0, 0); // add 0 likes
  43. peshoFollowers.get(username).add(1, newComments); // add comments
  44.  
  45. } else {
  46. int oldComments = peshoFollowers.get(username).get(1); // get current comments from List
  47. peshoFollowers.get(username).set(1, newComments + oldComments); // re-write the comments
  48. }
  49. break;
  50. case "Blocked":
  51. if (!peshoFollowers.containsKey(username)) {
  52. System.out.println(String.format("%s doesn't exist.", username));
  53. } else {
  54. peshoFollowers.remove(username, peshoFollowers.get(username));
  55. }
  56. break;
  57.  
  58. }
  59.  
  60.  
  61. input = scanner.nextLine();
  62. }
  63.  
  64.  
  65. System.out.println(String.format("%d followers", peshoFollowers.size()));
  66. peshoFollowers.entrySet()
  67. .stream()
  68. .sorted((e1, e2) -> {
  69. if (getLikes(e1.getValue()) == getLikes(e2.getValue())) {
  70. return e1.getKey().compareTo(e2.getKey());
  71. }
  72.  
  73. return Integer.compare(getLikes(e2.getValue()), getLikes(e1.getValue()));
  74. })
  75. .forEach(e -> System.out.println(String
  76. .format("%s: %d", e.getKey(), sumLikesAndComments(e.getValue()))));
  77.  
  78. }
  79.  
  80.  
  81. public static int sumLikesAndComments(List<Integer> list) {
  82. return list.get(0) + list.get(1);
  83. }
  84.  
  85. public static int getLikes(List<Integer> list){
  86. return list.get(0);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement