tanya_zheleva

6

Feb 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5. using System.Text;
  6.  
  7. namespace ExamPreparation
  8. {
  9.     public sealed class Book
  10.     {
  11.         public Book(string title, DateTime releaseDate)
  12.         {
  13.             this.Title = title;
  14.             this.ReleaseDate = releaseDate;
  15.         }
  16.  
  17.         public string Title { get; private set; }
  18.  
  19.         public DateTime ReleaseDate { get; set; }
  20.  
  21.         public override string ToString()
  22.         {
  23.             return $"{this.Title} -> {this.ReleaseDate.ToString("dd.MM.yyyy")}";
  24.         }
  25.     }
  26.  
  27.     public sealed class Library
  28.     {
  29.         public Library()
  30.         {
  31.             this.Books = new List<Book>();
  32.         }
  33.  
  34.         public List<Book> Books { get; set; }
  35.  
  36.         public string ReleasedBooks(DateTime endDate)
  37.         {
  38.             StringBuilder output = new StringBuilder();
  39.             IEnumerable<Book> releasedAfter = this.Books.Where(b => b.ReleaseDate > endDate);
  40.  
  41.             foreach (var book in releasedAfter.OrderBy(b => b.ReleaseDate).ThenBy(b => b.Title))
  42.             {
  43.                 output.AppendLine(book.ToString());
  44.             }
  45.  
  46.             return output.ToString().Trim();
  47.         }
  48.     }
  49.  
  50.     public sealed class Startup
  51.     {
  52.         public static void Main()
  53.         {
  54.             int n = int.Parse(Console.ReadLine());
  55.             Library library = new Library();
  56.  
  57.             for (int i = 0; i < n; i++)
  58.             {
  59.                 string[] tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  60.                 string title = tokens[0];
  61.                 DateTime releaseDate = DateTime.ParseExact(tokens[3], "dd.MM.yyyy", CultureInfo.InvariantCulture);
  62.  
  63.                 Book book = new Book(title, releaseDate);
  64.  
  65.                 if (library.Books.Contains(book))
  66.                 {
  67.                     library.Books.Find(b => b.Title == title).ReleaseDate = releaseDate;
  68.                 }
  69.                 else
  70.                 {
  71.                     library.Books.Add(book);
  72.                 }
  73.             }
  74.  
  75.             DateTime endDate = DateTime.ParseExact(Console.ReadLine(), "dd.MM.yyyy", CultureInfo.InvariantCulture);
  76.             string releasedBooks = library.ReleasedBooks(endDate);
  77.             Console.WriteLine(releasedBooks);
  78.         }
  79.     }
  80. }
Add Comment
Please, Sign In to add comment