Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_examPreparation1;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Scanner;
- public class MeTubeStatistics {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- HashMap<String, Video> videos = new HashMap<>();
- while (true) {
- String input = reader.readLine();
- if (input.equals("stats time")) {
- break;
- }
- // String cut = input.substring(0,8);
- if (input.contains("dislike")) {
- String[] tokens = input.split(":");
- String name = tokens[1];
- if (videos.containsKey(name)) {
- videos.get(name).decrementLikes();
- continue;
- }
- }
- if (input.contains("like")) {
- String[] tokens = input.split(":");
- String name = tokens[1];
- if (videos.containsKey(name)) {
- videos.get(name).incrementLikes();
- continue;
- }
- }
- String[] tokens = input.split("-");
- String name = tokens[0];
- int views = Integer.parseInt(tokens[1]);
- if (videos.containsKey(name)) {
- Video video = videos.get(name);
- video.setViews(video.getViews() + views);
- } else {
- Video video = new Video(name, views);
- videos.put(name, video);
- }
- }
- String filter = reader.readLine();
- if (filter.equals("by views")) {
- videos.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue().getViews()
- , e1.getValue().getViews())).forEach(e ->
- System.out.printf("%s - %d views - %d likes%n", e.getValue().getName()
- , e.getValue().getViews(), e.getValue().getLikes()));
- } else if (filter.equals("by likes")) {
- videos.entrySet().stream().sorted((e1,e2) -> Integer.compare(e2.getValue().getLikes()
- ,e1.getValue().getLikes())).forEach(e -> {
- System.out.printf("%s - %d views - %d likes%n"
- ,e.getValue().getName(),e.getValue().getViews(),e.getValue().getLikes());
- });
- }
- }
- }
- class Video {
- private String name;
- private int views;
- private int likes;
- public Video(String name, int views) {
- this.name = name;
- this.views = views;
- this.likes = 0;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getViews() {
- return views;
- }
- public void setViews(int views) {
- this.views = views;
- }
- public int getLikes() {
- return likes;
- }
- public void setLikes(int likes) {
- this.likes = likes;
- }
- public void incrementLikes() {
- this.likes++;
- }
- public void decrementLikes() {
- this.likes--;
- }
- }
Add Comment
Please, Sign In to add comment