Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- namespace _05.BookLibrary
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<Book> books = new List<Book>();
- for (int i = 0; i < n; i++)
- {
- string[] tokens = Console.ReadLine().Split(' ');
- string title = tokens[0];
- string author = tokens[1];
- string publisher = tokens[2];
- DateTime releaseDate = DateTime.ParseExact(tokens[3], "dd.MM.yyyy", CultureInfo.InvariantCulture); //парсвам във формат Дата
- string isbn = tokens[4];
- decimal price = decimal.Parse(tokens[5]); //парсвам стринга в decimal
- //следваща стъпка да направя книга
- //направих конструктор, който да приема всички стойности и да създава книга с тези стойности
- Book book = new Book(title, author, publisher, releaseDate, isbn, price);
- books.Add(book);
- }
- Dictionary<string, decimal> authors = new Dictionary<string, decimal>();
- foreach (var book in books)
- {
- string authorName = book.Author;
- decimal price = book.Price;
- if (authors.ContainsKey(authorName) == false)
- {
- authors.Add(authorName, price);
- }
- else
- {
- authors[authorName] += price; //достъпвам съществуващ ключ и увеличавам стойността му
- }
- }
- //сортиране
- Dictionary<string,decimal> sortedAuthors= authors
- .OrderByDescending(a => a.Value)
- .ThenBy(a => a.Key)
- .ToDictionary(x=>x.Key, x=>x.Value);
- //принтирам
- foreach (var author in sortedAuthors)
- {
- Console.WriteLine($"{author.Key} -> {author.Value:f}");
- }
- }
- class Book
- { //конструктор който сетва тези пропъртита
- public Book(string title, string author, string publisher, DateTime releaseDate, string isbn, decimal price)
- {
- Title = title;
- Author = author;
- Publisher = publisher;
- ReleaseDate = releaseDate;
- Isbn = isbn;
- Price = price;
- }
- public string Title { get; set; }
- public string Author { get; set; }
- public string Publisher { get; set; }
- public DateTime ReleaseDate { get; set; }
- public string Isbn { get; set; }
- public decimal Price { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement