Advertisement
LoraOrliGeo

MeTubeStatistics

Apr 10th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package addRetakeExam_28Oct2018;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class MeTubeStatistics {
  8.     public static void main(String[] args) {
  9.         @SuppressWarnings("resource")
  10.  
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         Map<String, Integer> videoViews = new LinkedHashMap<>();
  14.         Map<String, Integer> videoLikes = new LinkedHashMap<>();
  15.  
  16.         String line = "";
  17.  
  18.         while (!"stats time".equals(line = sc.nextLine())) {
  19.  
  20.             if (line.contains("-")) {
  21.                 String[] tokensDash = line.split("-");
  22.                 String name = tokensDash[0];
  23.                 int views = Integer.parseInt(tokensDash[1]);
  24.  
  25.                 if (!videoViews.containsKey(name)) {
  26.                     videoViews.put(name, views);
  27.                     if (videoViews.containsKey(name)) {
  28.                         videoLikes.putIfAbsent(name, 0);
  29.                     }
  30.                 } else {
  31.                     videoViews.put(name, videoViews.get(name) + views);
  32.                 }
  33.             } else {
  34.                 String[] tokensCol = line.split(":");
  35.                 String option = tokensCol[0];
  36.                 String name = tokensCol[1];
  37.  
  38.                 switch (option) {
  39.                 case "like":
  40.                     if (videoLikes.containsKey(name)) {
  41.                         videoLikes.put(name, videoLikes.get(name) + 1);
  42.                     }
  43.                     break;
  44.                 case "dislike":
  45.                     if (videoLikes.containsKey(name)) {
  46.                         videoLikes.put(name, videoLikes.get(name) - 1);
  47.                     }
  48.                     break;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         String criteria = sc.nextLine();
  54.  
  55.         if (criteria.equals("by views")) {
  56.             videoViews.entrySet().stream().sorted((v1, v2) -> {
  57.                 int sort = Integer.compare(v2.getValue(), v1.getValue());
  58.                 return sort;
  59.             }).forEach(e -> System.out.println(
  60.                     String.format("%s - %d views - %d likes", e.getKey(), e.getValue(), videoLikes.get(e.getKey()))));
  61.         } else if (criteria.equals("by likes")) {
  62.             videoLikes.entrySet().stream().sorted((l1, l2) -> {
  63.                 int sort = Integer.compare(l2.getValue(), l1.getValue());
  64.                 return sort;
  65.             }).forEach(e -> System.out.println(
  66.                     String.format("%s - %d views - %d likes", e.getKey(), videoViews.get(e.getKey()), e.getValue())));
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement