Advertisement
shady_obeyd

05.BookLibrary

Nov 1st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Book_Library_Q
  9. {
  10.     class Book
  11.     {
  12.         public string Title { get; set; }
  13.         public string Author { get; set; }
  14.         public string Publisher { get; set; }
  15.         public DateTime ReleaseDate { get; set; }
  16.         public string ISBN { get; set; }
  17.         public decimal Price { get; set; }
  18.     }
  19.     class Library
  20.     {
  21.         public string Name { get; set; }
  22.         public List<Book> Books { get; set; }
  23.     }
  24.     class Program
  25.     {
  26.         static void Main()
  27.         {
  28.             Dictionary<string, decimal> output = new Dictionary<string, decimal>();
  29.  
  30.             int num = int.Parse(Console.ReadLine());
  31.             Library library = new Library();
  32.             ImportBooks(library, num);
  33.  
  34.  
  35.              foreach (string author in library.Books.Select(x => x.Author).Distinct())
  36.              {
  37.                  output.Add(author, library.Books.Where(x => x.Author == author)
  38.                                                .Select(x => x.Price)
  39.                                                .Sum());
  40.              }
  41.                
  42.              foreach (var pair in output.OrderByDescending(x => x.Value).ThenBy(x => x.Key))
  43.                  Console.WriteLine("{0} -> {1:f2}", pair.Key, pair.Value);
  44.  
  45.         }
  46.  
  47.         static void ImportBooks(Library library, int num)
  48.         {
  49.             for (int i = 0; i < num; i++)
  50.             {
  51.                 library.Books = new List<Book>(); // Initialize the List before using it
  52.                 string[] input = Console.ReadLine().Split();
  53.                 library.Books.Add(new Book()
  54.                 {
  55.                     Title = input[0],
  56.                     Author = input[1],
  57.                     Publisher = input[2],
  58.                     ReleaseDate = DateTime.ParseExact(input[3], "dd.MM.yyyy", CultureInfo.InvariantCulture),
  59.                     ISBN = input[4],
  60.                     Price = decimal.Parse(input[5])
  61.                 });
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement