fork download
  1. using System;
  2. using static System.Console;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. class Program {
  7. static void Main() {
  8. var p1 = new Post(DateTime.Now, "Travelling to New Zealand", "I'm going to visit this wonderful country!", 12);
  9. p1.AddComment(new Comment("Have a nice trip!"));
  10. p1.AddComment(new Comment("Wow that's awesome!"));
  11. var p2 = new Post(DateTime.Now, "Good night guys", "See you tomorrow", 5);
  12. p2.AddComment(new Comment("Good night"));
  13. p2.AddComment(new Comment("May the force be with you"));
  14. WriteLine(p1);
  15. WriteLine(p2);
  16. }
  17. }
  18.  
  19. class Comment {
  20. public string Text { get; set; }
  21. public Comment() {}
  22. public Comment(string text) => Text = text;
  23. }
  24. class Post {
  25. public DateTime Moment { get; set; }
  26. public string Title { get; set; }
  27. public string Content { get; set; }
  28. public int Likes { get; set; }
  29. public List<Comment> Comments { get; set; } = new List<Comment>();
  30. public Post() {}
  31. public Post(DateTime moment, string title, string content, int likes) {
  32. Moment = moment;
  33. Title = title;
  34. Content = content;
  35. Likes = likes;
  36. }
  37. public void AddComment(Comment comment) => Comments.Add(comment);
  38. public void RemoveComment(Comment comment) => Comments.Remove(comment);
  39. public override string ToString() {
  40. var sb = new StringBuilder();
  41. sb.AppendLine(Title);
  42. sb.Append(Likes);
  43. sb.Append(" Likes - ");
  44. sb.AppendLine(Moment.ToString("dd/MM/yyyy HH:mm:ss"));
  45. sb.AppendLine(Content);
  46. sb.AppendLine("Comments:");
  47. foreach (Comment c in Comments) sb.AppendLine(c.Text);
  48. return sb.ToString();
  49. }
  50. }
  51.  
  52. //https://pt.stackoverflow.com/q/417809/101
Success #stdin #stdout 0.03s 17968KB
stdin
Standard input is empty
stdout
Travelling to New Zealand
12 Likes - 24/10/2019 12:18:42
I'm going to visit this wonderful country!
Comments:
Have a nice trip!
Wow that's awesome!

Good night guys
5 Likes - 24/10/2019 12:18:42
See you tomorrow
Comments:
Good night
May the force be with you