JulianJulianov

11.ObjectsAndClasses-Articles 2.0

Mar 5th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. 11.ObjectsAndClasses-Articles 2.0
  2. Change the program in such a way, that you will be able to store a list of articles. You will not need to use the previous methods any more (except the ToString method). On the first line, you will receive the number of articles. On the next lines, you will receive the articles in the same format as in the previous problem: "{title}, {content}, {author}". Finally, you will receive a string: "title", "content" or an "author". You need to order the articles alphabetically, based on the given criteria.
  3. Example
  4. Input                                     Output
  5. 2
  6. Science, planets, Bill                    Article - content: Johnny
  7. Article, content, Johnny                  Science - planets: Bill
  8. title  
  9.  
  10. 3
  11. title1, C, author1                        title3 - A: author3
  12. title2, B, author2                        title2 - B: author2
  13. title3, A, author3                        title1 - C: author1
  14. content
  15.  
  16.  
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20.  
  21. namespace _03Articles2._0
  22. {
  23.     public class Article
  24.     {
  25.         public Article(string title, string content, string author)
  26.         {
  27.             Title = title;
  28.             Content = content;
  29.             Author = author;
  30.         }
  31.  
  32.         public string Title { get; set; }
  33.         public string Content { get; set; }
  34.         public string Author { get; set; }
  35.  
  36.         public override string ToString()
  37.         {
  38.             return $"{Title} - {Content}: {Author}";
  39.         }
  40.     }
  41.  
  42.     class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.             int countCommands = int.Parse(Console.ReadLine());
  47.             var articles = new List<Article>();
  48.  
  49.             for (int i = 0; i < countCommands; i++)
  50.             {
  51.                 string[] splittedCommand = Console.ReadLine()
  52.                     .Split(", ");
  53.  
  54.                 string title = splittedCommand[0];
  55.                 string content = splittedCommand[1];
  56.                 string author = splittedCommand[2];
  57.  
  58.                 var article = new Article(title, content, author);
  59.  
  60.                 articles.Add(article);
  61.             }
  62.             string orderBy = Console.ReadLine();
  63.  
  64.             if (orderBy == "title")
  65.             {
  66.                 articles = articles.OrderBy(x => x.Title)
  67.                     .ToList();
  68.             }
  69.             else if (orderBy == "content")
  70.             {
  71.                 articles = articles.OrderBy(x => x.Content)
  72.                      .ToList();
  73.             }
  74.             else if (orderBy == "author")
  75.             {
  76.                 articles = articles.OrderBy(x => x.Author)
  77.                     .ToList();
  78.             }
  79.             Console.WriteLine(string.Join(Environment.NewLine, articles));
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment