SylviaPetrova

4. Social Media Posts

Mar 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _4.Social_Media_Posts
  8. {
  9. class Program
  10. {
  11. public class Post
  12. {
  13. public string Name { get; set; }
  14. public int Like { get; set; }
  15. public int DisLike { get; set; }
  16. public List <Comment> CommentList { get; set; }
  17.  
  18. }
  19. public Program()
  20. {
  21. int Like = 0;
  22. int DisLike = 0;
  23. }
  24. public class Comment
  25. {
  26. public string PostName { get; set; }
  27. public string CommentatorName { get; set; }
  28. public string Content { get; set; }
  29.  
  30. }
  31.  
  32. static void Main(string[] args)
  33. {
  34. var listOfPosts = new Dictionary<string, Post>();
  35. var postsNameToComment = new Dictionary<string,List<Comment>>();
  36.  
  37.  
  38. var line = Console.ReadLine();
  39. while (line!= "drop the media")
  40. {
  41. Post post = new Post();
  42. Comment comment = new Comment();
  43.  
  44. var tokens = line.Split().ToList();
  45. if (tokens[0] == "post")
  46. {
  47.  
  48. post.Name = tokens[1];
  49. listOfPosts.Add(post.Name, post);
  50. }
  51. else if (tokens[0] == "like")
  52. {
  53. var nameOfPost = tokens[1];
  54. if (listOfPosts.ContainsKey(nameOfPost))
  55. {
  56. listOfPosts[nameOfPost].Like++;
  57. }
  58. }
  59.  
  60. else if (tokens[0] == "dislike")
  61. {
  62. var nameOfPost = tokens[1];
  63. if (listOfPosts.ContainsKey(nameOfPost))
  64. {
  65. listOfPosts[nameOfPost].DisLike++;
  66. }
  67. }
  68. else
  69. {
  70.  
  71. var postName = tokens[1];
  72. var commentator = tokens[2];
  73. var content = tokens[3];
  74.  
  75. comment.CommentatorName = commentator;
  76. comment.PostName = postName;
  77. comment.Content = content;
  78.  
  79.  
  80. if (!postsNameToComment.ContainsKey(postName))
  81. {
  82. postsNameToComment.Add(postName, new List<Comment> {comment });
  83. }
  84. else
  85. {
  86. postsNameToComment[postName].Add(comment);
  87. }
  88.  
  89. }
  90.  
  91.  
  92. line = Console.ReadLine();
  93. }
  94.  
  95. foreach (var post in listOfPosts)
  96. {
  97. Console.WriteLine($"Post: {post.Key} | Likes: {post.Value.Like } | Dislikes: {post.Value.DisLike } ");
  98. Console.WriteLine("Comments:");
  99.  
  100. var resultedComment = new List<Comment>();
  101.  
  102. if (!postsNameToComment.ContainsKey(post.Key))
  103. {
  104. Console.WriteLine("None");
  105. }
  106. else
  107. {
  108. resultedComment.AddRange( postsNameToComment[post.Key]);
  109.  
  110. foreach (var item in resultedComment)
  111. {
  112.  
  113. Console.WriteLine($"* {item.CommentatorName}: {item.Content}");
  114.  
  115.  
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122. }
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment