Advertisement
AlexTasev

05_BookLibrary

Jun 15th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace _05_BookLibrary
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.  
  14.             List<Book> books = new List<Book>();
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 string[] tokens = Console.ReadLine().Split();
  19.  
  20.                 string title = tokens[0];
  21.                 string author = tokens[1];
  22.                 string publisher = tokens[2];
  23.                 DateTime releaseDate = DateTime.ParseExact(tokens[3], "dd.MM.yyyy", CultureInfo.InvariantCulture);
  24.                 string isbn = tokens[4];
  25.                 decimal price = decimal.Parse(tokens[5]);
  26.  
  27.                 Book book = new Book(title, author, publisher, releaseDate, isbn, price);
  28.  
  29.                 books.Add(book);
  30.             }
  31.  
  32.             var authors = new Dictionary<string, decimal>();
  33.  
  34.             foreach (Book book in books)
  35.             {
  36.                 if (authors.ContainsKey(book.Author) == false)
  37.                 {
  38.                     authors.Add(book.Author, book.Price);
  39.                 }
  40.                 else
  41.                 {
  42.                     authors[book.Author] += book.Price;
  43.                 }
  44.             }
  45.  
  46.             var sortedAuthors = authors
  47.                 .OrderByDescending(p => p.Value)
  48.                 .ThenBy(a => a.Key)
  49.                 .ToDictionary(d => d.Key, d => d.Value);
  50.  
  51.             foreach (var author in sortedAuthors)
  52.             {
  53.                 Console.WriteLine($"{author.Key} -> {author.Value:f2}");
  54.             }
  55.         }
  56.     }
  57.  
  58.     public class Book
  59.     {
  60.         public Book(string title, string author, string publisher, DateTime releaseDate, string isbn, decimal price)
  61.         {
  62.             Title = title;
  63.             Author = author;
  64.             Publisher = publisher;
  65.             ReleaseDate = releaseDate;
  66.             Isbn = isbn;
  67.             Price = price;
  68.         }
  69.  
  70.         public string Title { get; set; }
  71.         public string Author { get; set; }
  72.         public string Publisher { get; set; }
  73.         public DateTime ReleaseDate { get; set; }
  74.         public string Isbn { get; set; }
  75.         public decimal Price { get; set; }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement