JulianJulianov

10.ObjectsAndClasses-Articles

Mar 5th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. 10.ObjectsAndClasses-Articles
  2. Create a class Article with the following properties:
  3. • Title – a string
  4. • Content – a string
  5. • Author – a string
  6. The class should have a constructor and the following methods:
  7. • Edit (new content) – change the old content with the new one
  8. • ChangeAuthor (new author) – change the author
  9. • Rename (new title) – change the title of the article
  10. Override the ToString method – print the article in the following format:
  11. "{title} - {content}: {autor}"
  12. Write a program that reads an article in the following format "{title}, {content}, {author}". On the next line, you will receive a number n, representing the number of commands, which will follow after it. On the next n lines, you will be receiving the following commands: "Edit: {new content}"; "ChangeAuthor: {new author}"; "Rename: {new title}". At the end, print the final state of the article.
  13. Example
  14. Input                                                Output
  15. some title, some content, some author                better title - better content: better author
  16. 3
  17. Edit: better content
  18. ChangeAuthor:  better author
  19. Rename: better title   
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. namespace LecturePractice
  25. {
  26.     public class Program
  27.     {
  28.         public static void Main()
  29.         {
  30.             var inputText = Console.ReadLine().Split(", ").ToList();
  31.  
  32.             var number = int.Parse(Console.ReadLine());
  33.  
  34.             for (int i = 0; i < number; i++)
  35.             {
  36.                 var newInputText = Console.ReadLine().Split(':').Select(s => s.Trim()).ToList();
  37.                                                     //Чрез метода Trim() се премахва ненужния спейс, който е останал след сплитването!
  38.                 if (newInputText[0] == "Edit")
  39.                 {
  40.                     inputText[1] = newInputText[1];
  41.                 }
  42.                 else if (newInputText[0] == "ChangeAuthor")
  43.                 {
  44.                     inputText[2] = newInputText[1];
  45.                 }
  46.                 else if (newInputText[0] == "Rename")
  47.                 {
  48.                     inputText[0] = newInputText[1];
  49.                 }
  50.             }
  51.             Console.WriteLine($"{inputText[0]} - {inputText[1]}: {inputText[2]}");
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment