Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. public class examPrepZad3Followers {
  5. static Map<String,Integer> likes = new HashMap<>();
  6. static Map<String,Integer> comments = new HashMap<>();
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String commandString;
  10. while (!(commandString=scanner.nextLine()).equals("Log out")){
  11. String[] commands= commandString.split("\\s+|:\\s+");
  12. switch (commands[0]){
  13. case "New":
  14. add(commands[2]);
  15. break;
  16. case "Like":
  17. addLike(commands[1],Integer.parseInt(commands[2]));
  18. break;
  19. case "Comment":
  20. addComment(commands[1]);
  21. break;
  22. case "Blocked":
  23. remove(commands[1]);
  24. break;
  25. }
  26. }
  27. System.out.println(likes.size()+" followers");
  28. likes
  29. .entrySet()
  30. .stream()
  31. .sorted((a,b)->{
  32. if (a.getValue() != b.getValue()) {
  33. return Integer.compare(b.getValue(),a.getValue());
  34. } else {
  35. return a.getKey().compareTo(b.getKey());
  36. }
  37. })
  38. .forEach((e)-> System.out.printf("%s: %d%n",e.getKey(),e.getValue() + comments.get(e.getKey())));
  39. }
  40. private static void remove(String user) {
  41. if (comments.containsKey(user)) {
  42. comments.remove(user);
  43. likes.remove(user);
  44. }else {
  45. System.out.println(user + " doesn't exist.");
  46. }
  47. }
  48. private static void addComment(String user) {
  49. comments.putIfAbsent(user,0);
  50. comments.put(user,comments.get(user)+1);
  51. likes.putIfAbsent(user,0);
  52. }
  53. private static void addLike(String user, int countLikes) {
  54. likes.putIfAbsent(user,0);
  55. likes.put(user,likes.get(user)+countLikes);
  56. comments.putIfAbsent(user,0);
  57. }
  58. private static void add(String user) {
  59. if (!comments.containsKey(user)) {
  60. comments.put(user,0);
  61. likes.put(user,0);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement