Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- class Comment implements Comparable<Comment> {
- String author;
- String id;
- String content;
- String replyToId;
- int likes;
- public int getLikes() {
- return likes;
- }
- public Comment(String author, String id, String content, String replyToId) {
- this.author = author;
- this.id = id;
- this.content = content;
- this.replyToId = replyToId;
- this.likes=0;
- }
- public Comment(String author, String id, String content) {
- this.author = author;
- this.id = id;
- this.content = content;
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(String.format(" Comment: %s\n" +
- " Written by: %s\n" +
- " Likes: %d",content,author,likes));
- return stringBuilder.toString();
- }
- @Override
- public int compareTo(Comment o) {
- return Comparator.comparing(Comment::getLikes).reversed().compare(this,o);
- }
- public void incrementLikes() {
- likes++;
- }
- }
- class Post {
- String postAuthor;
- String postContent;
- Map<String,Comment> comments;
- public Post(String postAuthor, String postContent) {
- this.comments = new HashMap<>();
- this.postAuthor = postAuthor;
- this.postContent = postContent;
- }
- public void addComment(String author, String id, String content, String replyToId) {
- if(replyToId==null)
- comments.putIfAbsent(id,new Comment(author,id,content));
- else{
- comments.putIfAbsent(id,new Comment(author,id,content,replyToId));
- }
- }
- public void likeComment(String id) {
- comments.get(id).incrementLikes();
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(String.format("Post: %s\nWritten by: %s\nComments:\n", postContent, postAuthor));
- comments.values().stream()
- .filter(c -> c.replyToId == null)
- .sorted() // ова е ново!!!
- .forEach(comment -> {
- appendCommentWithReplies(comment, stringBuilder, 1);
- });
- return stringBuilder.toString().trim();
- }
- private void appendCommentWithReplies(Comment comment, StringBuilder sb, int indentLevel) {
- // Add indentation
- sb.append(" ".repeat(indentLevel));
- sb.append(String.format("Comment: %s\n", comment.content));
- sb.append(" ".repeat(indentLevel));
- sb.append(String.format("Written by: %s\n", comment.author));
- sb.append(" ".repeat(indentLevel));
- sb.append(String.format("Likes: %d\n", comment.likes));
- // Find replies to this comment
- comments.values().stream()
- .filter(c -> comment.id.equals(c.replyToId))
- .sorted()
- .forEach(reply -> appendCommentWithReplies(reply, sb, indentLevel + 1));
- }
- }
- public class PostTester {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String postAuthor = sc.nextLine();
- String postContent = sc.nextLine();
- Post p = new Post(postAuthor, postContent);
- while (sc.hasNextLine()) {
- String line = sc.nextLine();
- String[] parts = line.split(";");
- String testCase = parts[0];
- if (testCase.equals("addComment")) {
- String author = parts[1];
- String id = parts[2];
- String content = parts[3];
- String replyToId = null;
- if (parts.length == 5) {
- replyToId = parts[4];
- }
- p.addComment(author, id, content, replyToId);
- } else if (testCase.equals("likes")) { //likes;1;2;3;4;1;1;1;1;1 example
- for (int i = 1; i < parts.length; i++) {
- p.likeComment(parts[i]);
- }
- } else {
- System.out.println(p);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement