Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Followers {
- static class User {
- int likes;
- int comments;
- public User(int likes, int comments) {
- this.likes = likes;
- this.comments = comments;
- }
- public int getLikes() {
- return likes;
- }
- public int getComments() {
- return comments;
- }
- public void setLikes(int likes) {
- this.likes = likes;
- }
- public void setComments(int comments) {
- this.comments = comments;
- }
- }
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- HashMap<String, User> peshoAccount = new HashMap<>();
- String input = console.nextLine();
- while (!input.equals("Log out")) {
- String[] tokens = input.split(": ");
- String command = tokens[0];
- switch (command) {
- case "New follower": {
- String username = tokens[1];
- if (!peshoAccount.containsKey(username)) {
- peshoAccount.put(username, new User(0, 0));
- }
- break;
- }
- case "Like": {
- String username = tokens[1];
- int count = Integer.parseInt(tokens[2]);
- if (!peshoAccount.containsKey(username)) {
- peshoAccount.put(username, new User(count, 0));
- } else {
- int likes = peshoAccount.get(username).getLikes() + count;
- peshoAccount.get(username).setLikes(likes);
- }
- break;
- }
- case "Comment": {
- String username = tokens[1];
- if (!peshoAccount.containsKey(username)) {
- peshoAccount.put(username, new User(0, 1));
- } else {
- int likes = peshoAccount.get(username).getComments() + 1;
- peshoAccount.get(username).setComments(likes);
- }
- break;
- }
- case "Blocked": {
- String username = tokens[1];
- if (peshoAccount.containsKey(username)) {
- peshoAccount.remove(username);
- } else {
- System.out.printf("%s doesn't exist.%n", username);
- }
- break;
- }
- }
- input = console.nextLine();
- }
- System.out.printf("%d followers%n", peshoAccount.size());
- peshoAccount.entrySet().stream().sorted((entryA, entryB) -> {
- int sumA = entryA.getValue().getLikes();
- String userA = entryA.getKey();
- int sumB = entryB.getValue().getLikes();
- String userB = entryB.getKey();
- if (sumA != sumB) {
- return Integer.compare(sumB, sumA);
- }else {
- return userA.compareTo(userB);
- }
- }).forEach(user -> System.out.printf("%s: %d%n", user.getKey(), (user.getValue().likes + user.getValue().comments)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment