Advertisement
DaniPasteBin

Untitled

Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 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(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             List<Book> books = new List<Book>();
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] tokens = Console.ReadLine().Split(' ');
  18.                 string title = tokens[0];
  19.                 string author = tokens[1];
  20.                 string publisher = tokens[2];
  21.                 DateTime releaseDate = DateTime.ParseExact(tokens[3], "dd.MM.yyyy", CultureInfo.InvariantCulture);  //парсвам във формат Дата
  22.                 string isbn = tokens[4];
  23.                 decimal price = decimal.Parse(tokens[5]); //парсвам стринга в decimal
  24.                                                           //следваща стъпка да направя книга
  25.                                                           //направих конструктор, който да приема всички стойности и да създава книга с тези стойности
  26.                 Book book = new Book(title, author, publisher, releaseDate, isbn, price);
  27.                 books.Add(book);
  28.             }
  29.             Dictionary<string, decimal> authors = new Dictionary<string, decimal>();
  30.             foreach (var book in books)
  31.             {
  32.                 string authorName = book.Author;
  33.                 decimal price = book.Price;
  34.                 if (authors.ContainsKey(authorName) == false)
  35.                 {
  36.                     authors.Add(authorName, price);
  37.                 }
  38.                 else
  39.                 {
  40.                     authors[authorName] += price;  //достъпвам съществуващ ключ и увеличавам стойността му
  41.                 }
  42.             }
  43.             //сортиране
  44.            Dictionary<string,decimal> sortedAuthors=  authors
  45.                 .OrderByDescending(a => a.Value)
  46.                 .ThenBy(a => a.Key)
  47.                 .ToDictionary(x=>x.Key, x=>x.Value);
  48.             //принтирам
  49.             foreach (var author in sortedAuthors)
  50.             {
  51.                 Console.WriteLine($"{author.Key} -> {author.Value:f}");
  52.             }
  53.  
  54.  
  55.         }
  56.         class Book
  57.         {    //конструктор който сетва тези пропъртита
  58.            
  59.             public Book(string title, string author, string publisher, DateTime releaseDate, string isbn, decimal price)
  60.             {
  61.                 Title = title;
  62.                 Author = author;
  63.                 Publisher = publisher;
  64.                 ReleaseDate = releaseDate;
  65.                 Isbn = isbn;
  66.                 Price = price;
  67.             }
  68.  
  69.             public string Title { get; set; }
  70.             public string Author { get; set; }
  71.             public string Publisher { get; set; }
  72.             public DateTime ReleaseDate { get; set; }
  73.             public string Isbn { get; set; }
  74.             public decimal Price { get; set; }
  75.  
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement