Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 11.ObjectsAndClasses-Articles 2.0
- 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.
- Example
- Input Output
- 2
- Science, planets, Bill Article - content: Johnny
- Article, content, Johnny Science - planets: Bill
- title
- 3
- title1, C, author1 title3 - A: author3
- title2, B, author2 title2 - B: author2
- title3, A, author3 title1 - C: author1
- content
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03Articles2._0
- {
- public class Article
- {
- public Article(string title, string content, string author)
- {
- Title = title;
- Content = content;
- Author = author;
- }
- public string Title { get; set; }
- public string Content { get; set; }
- public string Author { get; set; }
- public override string ToString()
- {
- return $"{Title} - {Content}: {Author}";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int countCommands = int.Parse(Console.ReadLine());
- var articles = new List<Article>();
- for (int i = 0; i < countCommands; i++)
- {
- string[] splittedCommand = Console.ReadLine()
- .Split(", ");
- string title = splittedCommand[0];
- string content = splittedCommand[1];
- string author = splittedCommand[2];
- var article = new Article(title, content, author);
- articles.Add(article);
- }
- string orderBy = Console.ReadLine();
- if (orderBy == "title")
- {
- articles = articles.OrderBy(x => x.Title)
- .ToList();
- }
- else if (orderBy == "content")
- {
- articles = articles.OrderBy(x => x.Content)
- .ToList();
- }
- else if (orderBy == "author")
- {
- articles = articles.OrderBy(x => x.Author)
- .ToList();
- }
- Console.WriteLine(string.Join(Environment.NewLine, articles));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment