Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ExamPreparation
- {
- public sealed class Book
- {
- public Book(string author, decimal price)
- {
- this.Author = author;
- this.Price = price;
- }
- public string Author { get; private set; }
- public decimal Price { get; set; }
- public override string ToString()
- {
- return $"{this.Author} -> {this.Price:F2}";
- }
- }
- public sealed class Library
- {
- public Library()
- {
- this.Books = new List<Book>();
- }
- public List<Book> Books { get; set; }
- public override string ToString()
- {
- StringBuilder output = new StringBuilder();
- foreach (var book in this.Books.OrderByDescending(b => b.Price).ThenBy(b => b.Author))
- {
- output.AppendLine(book.ToString());
- }
- return output.ToString().Trim();
- }
- }
- public sealed class Startup
- {
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Library library = new Library();
- for (int i = 0; i < n; i++)
- {
- string[] tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string authorName = tokens[1];
- decimal bookPrice = decimal.Parse(tokens[tokens.Length - 1]);
- if (library.Books.Any(b => b.Author == authorName))
- {
- library.Books.Find(b => b.Author == authorName).Price += bookPrice;
- }
- else
- {
- Book book = new Book(authorName, bookPrice);
- library.Books.Add(book);
- }
- }
- Console.WriteLine(library);
- }
- }
- }
Add Comment
Please, Sign In to add comment