Valik98

Followers

Dec 3rd, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. public class Followers {
  2.     static class User {
  3.         int likes;
  4.         int comments;
  5.  
  6.         public User(int likes, int comments) {
  7.             this.likes = likes;
  8.             this.comments = comments;
  9.         }
  10.  
  11.         public int getLikes() {
  12.             return likes;
  13.         }
  14.  
  15.         public int getComments() {
  16.             return comments;
  17.         }
  18.  
  19.         public void setLikes(int likes) {
  20.             this.likes = likes;
  21.         }
  22.  
  23.         public void setComments(int comments) {
  24.             this.comments = comments;
  25.         }
  26.     }
  27.  
  28.     public static void main(String[] args) {
  29.         Scanner console = new Scanner(System.in);
  30.  
  31.         HashMap<String, User> peshoAccount = new HashMap<>();
  32.  
  33.         String input = console.nextLine();
  34.  
  35.         while (!input.equals("Log out")) {
  36.             String[] tokens = input.split(": ");
  37.             String command = tokens[0];
  38.  
  39.             switch (command) {
  40.                 case "New follower": {
  41.                     String username = tokens[1];
  42.  
  43.                     if (!peshoAccount.containsKey(username)) {
  44.                         peshoAccount.put(username, new User(0, 0));
  45.                     }
  46.                     break;
  47.                 }
  48.                 case "Like": {
  49.                     String username = tokens[1];
  50.                     int count = Integer.parseInt(tokens[2]);
  51.  
  52.                     if (!peshoAccount.containsKey(username)) {
  53.                         peshoAccount.put(username, new User(count, 0));
  54.                     } else {
  55.                         int likes = peshoAccount.get(username).getLikes() + count;
  56.                         peshoAccount.get(username).setLikes(likes);
  57.                     }
  58.                     break;
  59.                 }
  60.                 case "Comment": {
  61.                     String username = tokens[1];
  62.  
  63.                     if (!peshoAccount.containsKey(username)) {
  64.                         peshoAccount.put(username, new User(0, 1));
  65.                     } else {
  66.                         int likes = peshoAccount.get(username).getComments() + 1;
  67.                         peshoAccount.get(username).setComments(likes);
  68.                     }
  69.                     break;
  70.                 }
  71.                 case "Blocked": {
  72.                     String username = tokens[1];
  73.  
  74.                     if (peshoAccount.containsKey(username)) {
  75.                         peshoAccount.remove(username);
  76.                     } else {
  77.                         System.out.printf("%s doesn't exist.%n", username);
  78.                     }
  79.                     break;
  80.                 }
  81.             }
  82.  
  83.             input = console.nextLine();
  84.         }
  85.  
  86.         System.out.printf("%d followers%n", peshoAccount.size());
  87.  
  88.         peshoAccount.entrySet().stream().sorted((entryA, entryB) -> {
  89.             int sumA = entryA.getValue().getLikes();
  90.             String userA = entryA.getKey();
  91.  
  92.             int sumB = entryB.getValue().getLikes();
  93.             String userB = entryB.getKey();
  94.  
  95.             if (sumA != sumB) {
  96.                 return Integer.compare(sumB, sumA);
  97.             }else {
  98.                 return userA.compareTo(userB);
  99.             }
  100.         }).forEach(user -> System.out.printf("%s: %d%n", user.getKey(), (user.getValue().likes + user.getValue().comments)));
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment