Advertisement
dzocesrce

[NP] Post, Comments & Likes

Apr 26th, 2025
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. class Comment implements Comparable<Comment> {
  8.  
  9.     String author;
  10.     String id;
  11.     String content;
  12.     String replyToId;
  13.     int likes;
  14.  
  15.     public int getLikes() {
  16.         return likes;
  17.     }
  18.  
  19.     public Comment(String author, String id, String content, String replyToId) {
  20.         this.author = author;
  21.         this.id = id;
  22.         this.content = content;
  23.         this.replyToId = replyToId;
  24.         this.likes=0;
  25.     }
  26.  
  27.     public Comment(String author, String id, String content) {
  28.         this.author = author;
  29.         this.id = id;
  30.         this.content = content;
  31.     }
  32.  
  33.     @Override
  34.     public String toString() {
  35.         StringBuilder  stringBuilder = new StringBuilder();
  36.                 stringBuilder.append(String.format("        Comment: %s\n" +
  37.                 "        Written by: %s\n" +
  38.                 "        Likes: %d",content,author,likes));
  39.         return stringBuilder.toString();
  40.     }
  41.  
  42.     @Override
  43.     public int compareTo(Comment o) {
  44.         return Comparator.comparing(Comment::getLikes).reversed().compare(this,o);
  45.     }
  46.  
  47.  
  48.     public void incrementLikes() {
  49.         likes++;
  50.     }
  51. }
  52.  
  53. class Post {
  54.     String postAuthor;
  55.     String postContent;
  56.     Map<String,Comment> comments;
  57.  
  58.  
  59.     public Post(String postAuthor, String postContent) {
  60.         this.comments = new HashMap<>();
  61.         this.postAuthor = postAuthor;
  62.         this.postContent = postContent;
  63.     }
  64.  
  65.     public void addComment(String author, String id, String content, String replyToId) {
  66.         if(replyToId==null)
  67.             comments.putIfAbsent(id,new Comment(author,id,content));
  68.         else{
  69.             comments.putIfAbsent(id,new Comment(author,id,content,replyToId));
  70.         }
  71.     }
  72.  
  73.     public void likeComment(String id) {
  74.         comments.get(id).incrementLikes();
  75.     }
  76.  
  77.  
  78.     @Override
  79.     public String toString() {
  80.         StringBuilder stringBuilder = new StringBuilder();
  81.         stringBuilder.append(String.format("Post: %s\nWritten by: %s\nComments:\n", postContent, postAuthor));
  82.  
  83.         comments.values().stream()
  84.                 .filter(c -> c.replyToId == null)
  85.                 .sorted() // ова е ново!!!
  86.                 .forEach(comment -> {
  87.                     appendCommentWithReplies(comment, stringBuilder, 1);
  88.                 });
  89.  
  90.         return stringBuilder.toString().trim();
  91.     }
  92.  
  93.  
  94.     private void appendCommentWithReplies(Comment comment, StringBuilder sb, int indentLevel) {
  95.         // Add indentation
  96.         sb.append("        ".repeat(indentLevel));
  97.         sb.append(String.format("Comment: %s\n", comment.content));
  98.         sb.append("        ".repeat(indentLevel));
  99.         sb.append(String.format("Written by: %s\n", comment.author));
  100.         sb.append("        ".repeat(indentLevel));
  101.         sb.append(String.format("Likes: %d\n", comment.likes));
  102.  
  103.         // Find replies to this comment
  104.         comments.values().stream()
  105.                 .filter(c -> comment.id.equals(c.replyToId))
  106.                 .sorted()
  107.                 .forEach(reply -> appendCommentWithReplies(reply, sb, indentLevel + 1));
  108.     }
  109. }
  110.  
  111. public class PostTester {
  112.     public static void main(String[] args) {
  113.         Scanner sc = new Scanner(System.in);
  114.         String postAuthor = sc.nextLine();
  115.         String postContent = sc.nextLine();
  116.  
  117.         Post p = new Post(postAuthor, postContent);
  118.  
  119.         while (sc.hasNextLine()) {
  120.             String line = sc.nextLine();
  121.             String[] parts = line.split(";");
  122.             String testCase = parts[0];
  123.  
  124.             if (testCase.equals("addComment")) {
  125.                 String author = parts[1];
  126.                 String id = parts[2];
  127.                 String content = parts[3];
  128.                 String replyToId = null;
  129.                 if (parts.length == 5) {
  130.                     replyToId = parts[4];
  131.                 }
  132.                 p.addComment(author, id, content, replyToId);
  133.             } else if (testCase.equals("likes")) { //likes;1;2;3;4;1;1;1;1;1 example
  134.                 for (int i = 1; i < parts.length; i++) {
  135.                     p.likeComment(parts[i]);
  136.                 }
  137.             } else {
  138.                 System.out.println(p);
  139.             }
  140.  
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement