Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class _04_MeTubeStatistics {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Integer> view = new HashMap<>();
- Map<String, Integer> rate = new HashMap<>();
- String line = "";
- while (!"stats time".equals(line = scanner.nextLine())) {
- if (line.contains("-")) {
- String[] viewLine = line.split("-");
- String name = viewLine[0];
- int watching = Integer.parseInt(viewLine[1]);
- view.putIfAbsent(name, 0);
- view.put(name, view.get(name) + watching);
- rate.putIfAbsent(name, 0);
- } else if (line.contains(":")) {
- String[] viewLine1 = line.split(":");
- String name1 = viewLine1[1];
- String command = viewLine1[0];
- rate.putIfAbsent(name1, 0);
- if (command.equals("like")) {
- rate.put(name1, rate.get(name1) + 1);
- } else if (command.equals("dislike")) {
- rate.put(name1, rate.get(name1) - 1);
- }
- }
- }
- String sortType = scanner.nextLine();
- if (sortType.equals("by likes")) {
- rate.entrySet().stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) //order by amount/descending-value
- .forEach(entry -> {
- System.out.println(String.format("%s - %d views - %d likes",
- entry.getKey(), view.get(entry.getKey()),entry.getValue()));
- });
- }
- if (sortType.equals("by views")){
- view.entrySet().stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) //order by amount/descending-value
- .forEach(entry -> {
- System.out.println(String.format("%s - %d views - %d likes",
- entry.getKey(), entry.getValue(), rate.get(entry.getKey())));
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement