Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class BookLibrary
  9. {
  10.     static void Main()
  11.     {
  12.         List<Book> books = new List<Book>();
  13.         books = ReadLibrary();
  14.  
  15.         List<string> authors = books.Select(a => a.Author).Distinct().OrderBy(x => x).ToList();
  16.  
  17.         foreach (var author in authors)
  18.         {
  19.             decimal totalPrice = books.Where(b => b.Author.Equals(author)).Select(p => p.Price = p.Price).Sum();
  20.             Console.WriteLine($"{author} -> {totalPrice:f2}");
  21.         }
  22.     }
  23.  
  24.     public static List<Book> ReadLibrary()
  25.     {
  26.         int n = int.Parse(Console.ReadLine());
  27.         List<Book> lib = new List<Book>();
  28.         for (int i = 0; i < n; i++)
  29.         {
  30.             Book book = ReadBook();
  31.             lib.Add(book);
  32.            
  33.         }
  34.         return lib;
  35.     }
  36.  
  37.     public static Book ReadBook()
  38.     {
  39.         string[] inputData = Console.ReadLine().Split();
  40.         Book book = new Book();
  41.         book.Title = inputData[0];
  42.         book.Author = inputData[1];
  43.         book.Publisher = inputData[2];
  44.         book.ReleaseDate = DateTime.ParseExact(inputData[3], "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
  45.         book.ISBNNumber = inputData[4];
  46.         book.Price = decimal.Parse(inputData[5]);
  47.         return book;
  48.     }
  49. }
  50.  
  51. class Book
  52. {
  53.     public string Title { get; set; }
  54.     public string Author { get; set; }
  55.     public string Publisher { get; set; }
  56.     public DateTime ReleaseDate { get; set; }
  57.     public string ISBNNumber { get; set; }
  58.     public decimal Price { get; set; }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement