Advertisement
marking2112

MeTube Statistics

Nov 25th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class MeTube {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String input = scanner.nextLine();
  10.  
  11.         Map<String, Integer> videoViews = new HashMap<>();
  12.         Map<String, Integer> videosLikes = new HashMap<>();
  13.  
  14.         while (!"stats time".equals(input)) {
  15.  
  16.             if (input.contains("-")) {
  17.                 String[] tokens = input.split("-");
  18.                 String videoName = tokens[0];
  19.                 int views = Integer.parseInt(tokens[1]);
  20.  
  21.                 if (!videoViews.containsKey(videoName)) {
  22.                     videoViews.put(videoName, views);
  23.                 } else {
  24.                     videoViews.put(videoName, videoViews.get(videoName) + views);
  25.                 }
  26.                 videosLikes.putIfAbsent(videoName, 0);
  27.  
  28.             } else {
  29.                 String[] tokens = input.split(":");
  30.  
  31.                 if (tokens[0].equals("like")) {
  32.                     if (videosLikes.containsKey(tokens[1])) {
  33.                         videosLikes.put(tokens[1], videosLikes.get(tokens[1]) + 1);
  34.                     }
  35.                 } else {
  36.                     if (videosLikes.containsKey(tokens[1])) {
  37.                         videosLikes.put(tokens[1], videosLikes.get(tokens[1]) - 1);
  38.                     }
  39.                 }
  40.             }
  41.             input = scanner.nextLine();
  42.         }
  43.         String sortingCriteria = scanner.nextLine();
  44.         if (sortingCriteria.equals("by views")) {
  45.             videoViews.entrySet().stream().sorted((es1, es2) -> es2.getValue() - es1.getValue()).forEach(e -> {
  46.                 System.out.printf("%s - %d views - %d likes%n", e.getKey(), e.getValue(), videosLikes.get(e.getKey()));
  47.             });
  48.         } else {
  49.             videosLikes.entrySet().stream().sorted((es1, es2) -> es2.getValue() - es1.getValue()).forEach(e ->
  50.                     System.out.printf("%s - %d views - %d likes%n", e.getKey(), videoViews.get(e.getKey()), e.getValue()));
  51.         }
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement