5.Book Library from Objects and Classes - Exercises 0/100???
Условие:
To model a book library, define classes to hold a book and a library.
The library must have a name and a list of books. The books must contain the title, author, publisher, release date (in dd.MM.yyyy format), ISBN-number and price.
Read a list of books, add them to the library and print the total sum of prices by author, ordered descending by price and then by author’s name lexicographically.
Books in the input will be in format {title} {author} {publisher} {release date} {ISBN} {price}.
The total prices must be printed formatted to the second decimal place.
Examples
Input |
Output |
5 LOTR Tolkien GeorgeAllen 29.07.1954 0395082999 30.00 Hobbit Tolkien GeorgeAll 21.09.1937 0395082888 10.25 HP1 JKRowling Bloomsbury 26.06.1997 0395082777 15.50 HP7 JKRowling Bloomsbury 21.07.2007 0395082666 20.00 AC OBowden PenguinBooks 20.11.2009 0395082555 14.00 |
Tolkien -> 40.25 JKRowling -> 35.50 OBowden -> 14.00 |
Hints
- Create classes Book and Library with all the mentioned above properties:
- an object of type Library.
- Read the input and create a Book object for each book in the input.
- Create a LINQ query that will sum the prices by author, order the results as requested.
Print the results
РЕШЕНИЕ:
using System.Globalization;
using System.Runtime.Remoting.Messaging;
namespace _5.Book_Library
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public 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; }
}
public class Lybrary
{
public string Name { get; set; }
public new List<Book> Books { get; set; }
}
public static void Main()
{
var library=new Lybrary()
{
Name = "Prosveta",
Books = new List<Book>()
};
var booksCount = int.Parse(Console.ReadLine());
for (int i = 0; i < booksCount; i++)
{
// {title} {author} {publisher} {release date} {ISBN} {price}.
var info = Console.ReadLine().Split(' ');
var title = info[0];
var author = info[1];
var publisher = info[2];
var releaseDate = DateTime.ParseExact(info[3], "dd.MM.yyyy", CultureInfo.InvariantCulture);
var isbn = info[4];
var price = Convert.ToDecimal(info[5]);
var book = new Book(title, author, publisher, releaseDate, isbn, price);
library.Books.Add(book);
}
var sortedLybrary = library
.Books
.Select(a => a.Author)
.Distinct()
.Select(a => new
{
Author = a,
Sales = library.Books.Where(book => book.Author == a).Sum(p => p.Price)
})
.OrderByDescending(authorInfo => authorInfo.Sales)
.ThenBy(a => a.Author)
.ToList();
foreach (var item in sortedLybrary)
{
Console.WriteLine($"{item.Author} -> {item.Sales:f2}");
}
}
}
}