Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package addRetakeExam_28Oct2018;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class MeTubeStatistics {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- Scanner sc = new Scanner(System.in);
- Map<String, Integer> videoViews = new LinkedHashMap<>();
- Map<String, Integer> videoLikes = new LinkedHashMap<>();
- String line = "";
- while (!"stats time".equals(line = sc.nextLine())) {
- if (line.contains("-")) {
- String[] tokensDash = line.split("-");
- String name = tokensDash[0];
- int views = Integer.parseInt(tokensDash[1]);
- if (!videoViews.containsKey(name)) {
- videoViews.put(name, views);
- if (videoViews.containsKey(name)) {
- videoLikes.putIfAbsent(name, 0);
- }
- } else {
- videoViews.put(name, videoViews.get(name) + views);
- }
- } else {
- String[] tokensCol = line.split(":");
- String option = tokensCol[0];
- String name = tokensCol[1];
- switch (option) {
- case "like":
- if (videoLikes.containsKey(name)) {
- videoLikes.put(name, videoLikes.get(name) + 1);
- }
- break;
- case "dislike":
- if (videoLikes.containsKey(name)) {
- videoLikes.put(name, videoLikes.get(name) - 1);
- }
- break;
- }
- }
- }
- String criteria = sc.nextLine();
- if (criteria.equals("by views")) {
- videoViews.entrySet().stream().sorted((v1, v2) -> {
- int sort = Integer.compare(v2.getValue(), v1.getValue());
- return sort;
- }).forEach(e -> System.out.println(
- String.format("%s - %d views - %d likes", e.getKey(), e.getValue(), videoLikes.get(e.getKey()))));
- } else if (criteria.equals("by likes")) {
- videoLikes.entrySet().stream().sorted((l1, l2) -> {
- int sort = Integer.compare(l2.getValue(), l1.getValue());
- return sort;
- }).forEach(e -> System.out.println(
- String.format("%s - %d views - %d likes", e.getKey(), videoViews.get(e.getKey()), e.getValue())));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement