72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.Contracts;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using Books.ConsoleApp;
|
|
|
|
namespace Books.ConsoleApp
|
|
{
|
|
class Program
|
|
{
|
|
private static IBooksSource BooksSource = new BooksJsonSource();
|
|
private static List<BooksByAuthor> BooksByAuthorCatalog = null;
|
|
static void Main(string[] args)
|
|
{
|
|
IEnumerable<Book> books = BooksSource.Read();
|
|
BooksByAuthorCatalog = new List<BooksByAuthor>();
|
|
|
|
|
|
while (true)
|
|
{
|
|
System.Console.WriteLine("\nActions available:");
|
|
System.Console.WriteLine("1 - Output all books by author (Section 2)");
|
|
System.Console.WriteLine("2 - Search books by title (Section 3)");
|
|
System.Console.WriteLine("Any other key - Exit");
|
|
|
|
var key = System.Console.ReadKey();
|
|
switch (key.KeyChar)
|
|
{
|
|
case '1': Output.BooksByAuthor(BooksSource.Read()); break;
|
|
case '2': DoSearch(); break;
|
|
default: return;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static void DoSearch()
|
|
{
|
|
var books = BooksSource.Read();
|
|
while (true)
|
|
{
|
|
System.Console.WriteLine("\nSearch by book title or a part of it. \n^^^^Type 'exit' to go back^^^^");
|
|
var searchTerm = System.Console.ReadLine();
|
|
if (searchTerm == "exit")
|
|
{
|
|
return;
|
|
}
|
|
if (!string.IsNullOrEmpty(searchTerm))
|
|
{
|
|
var booksByAuthor = Search.ByTitle(books, searchTerm);
|
|
|
|
if (booksByAuthor.Count() == 0)
|
|
{
|
|
System.Console.WriteLine($"No books found for '{searchTerm}'");
|
|
}
|
|
else
|
|
{
|
|
foreach (var b in booksByAuthor)
|
|
{
|
|
System.Console.WriteLine($"{b.author}: {b.title}");
|
|
}
|
|
}
|
|
}
|
|
|
|
System.Console.WriteLine("----------------------");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|