emodev

Untitled

Dec 9th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package a_examPreparation1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashMap;
  7. import java.util.Scanner;
  8.  
  9. public class MeTubeStatistics {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         HashMap<String, Video> videos = new HashMap<>();
  14.         while (true) {
  15.             String input = reader.readLine();
  16.             if (input.equals("stats time")) {
  17.                 break;
  18.             }
  19. //            String cut = input.substring(0,8);
  20.             if (input.contains("dislike")) {
  21.                 String[] tokens = input.split(":");
  22.                 String name = tokens[1];
  23.                 if (videos.containsKey(name)) {
  24.                     videos.get(name).decrementLikes();
  25.                     continue;
  26.                 }
  27.             }
  28.  
  29.             if (input.contains("like")) {
  30.                 String[] tokens = input.split(":");
  31.                 String name = tokens[1];
  32.                 if (videos.containsKey(name)) {
  33.                     videos.get(name).incrementLikes();
  34.                     continue;
  35.                 }
  36.             }
  37.  
  38.  
  39.             String[] tokens = input.split("-");
  40.             String name = tokens[0];
  41.             int views = Integer.parseInt(tokens[1]);
  42.  
  43.             if (videos.containsKey(name)) {
  44.                 Video video = videos.get(name);
  45.                 video.setViews(video.getViews() + views);
  46.             } else {
  47.                 Video video = new Video(name, views);
  48.                 videos.put(name, video);
  49.             }
  50.  
  51.  
  52.         }
  53.  
  54.         String filter = reader.readLine();
  55.         if (filter.equals("by views")) {
  56.             videos.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue().getViews()
  57.                     , e1.getValue().getViews())).forEach(e ->
  58.                     System.out.printf("%s - %d views - %d likes%n", e.getValue().getName()
  59.                             , e.getValue().getViews(), e.getValue().getLikes()));
  60.         } else if (filter.equals("by likes")) {
  61.             videos.entrySet().stream().sorted((e1,e2) -> Integer.compare(e2.getValue().getLikes()
  62.             ,e1.getValue().getLikes())).forEach(e -> {
  63.                 System.out.printf("%s - %d views - %d likes%n"
  64.                 ,e.getValue().getName(),e.getValue().getViews(),e.getValue().getLikes());
  65.             });
  66.         }
  67.  
  68.     }
  69.  
  70.  
  71. }
  72.  
  73.  
  74. class Video {
  75.     private String name;
  76.     private int views;
  77.     private int likes;
  78.  
  79.     public Video(String name, int views) {
  80.         this.name = name;
  81.         this.views = views;
  82.         this.likes = 0;
  83.     }
  84.  
  85.     public String getName() {
  86.         return name;
  87.     }
  88.  
  89.     public void setName(String name) {
  90.         this.name = name;
  91.     }
  92.  
  93.     public int getViews() {
  94.         return views;
  95.     }
  96.  
  97.     public void setViews(int views) {
  98.         this.views = views;
  99.     }
  100.  
  101.     public int getLikes() {
  102.         return likes;
  103.     }
  104.  
  105.     public void setLikes(int likes) {
  106.         this.likes = likes;
  107.     }
  108.  
  109.     public void incrementLikes() {
  110.         this.likes++;
  111.     }
  112.  
  113.     public void decrementLikes() {
  114.         this.likes--;
  115.     }
  116. }
Add Comment
Please, Sign In to add comment