tanya_zheleva

5

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