Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class products {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- HashMap<String, List<Integer>> namesAndCount = new HashMap<>();
- String line = scanner.nextLine();
- while (!"Log out".equals(line)) {
- String[] tokens = line.split("\\: ");
- String name = tokens[1];
- String command = tokens[0];
- switch (command){
- case"New follower":
- if(!namesAndCount.containsKey(name)){
- namesAndCount.put(name,new ArrayList<>());
- namesAndCount.get(name).add(0,0);
- namesAndCount.get(name).add(1,0);
- }
- break;
- case"Like":
- int likes = Integer.parseInt(tokens[2]);
- if(!namesAndCount.containsKey(name)) {
- namesAndCount.put(name,new ArrayList<>());
- namesAndCount.get(name).add(0,likes);
- namesAndCount.get(name).add(1,0);
- }else{
- int oldLikes = namesAndCount.get(name).get(0);
- namesAndCount.get(name).set(0, oldLikes + likes);
- }
- break;
- case "Comment":
- int comment = 1;
- if(!namesAndCount.containsKey(name)){
- namesAndCount.put(name,new ArrayList<>());
- namesAndCount.get(name).add(0,0);
- namesAndCount.get(name).add(1,comment);
- }else{
- int oldComments = namesAndCount.get(name).get(1);
- namesAndCount.get(name).set(1, oldComments + comment);
- }
- break;
- case "Blocked":
- if(namesAndCount.containsKey(name)){
- namesAndCount.remove(name, namesAndCount.get(name));
- }else{
- System.out.printf("%s doesn't exist.%n", name);
- }
- break;
- }
- line = scanner.nextLine();
- }
- System.out.printf("%d followers%n", namesAndCount.size());
- namesAndCount.entrySet().stream().sorted((f,s) ->{
- if(like(s.getValue()) == like(f.getValue())){
- return f.getKey().compareTo(s.getKey());
- }
- return Integer.compare(like(s.getValue()),like(f.getValue()));
- }).forEach(entry -> {
- System.out.printf("%s: %d%n",entry.getKey(),sun(entry.getValue()));
- });
- }
- public static int sun(List<Integer>list){
- return list.get(0) + list.get(1);
- }
- public static int like(List<Integer>list){
- return list.get(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement