Nu har det tillkommit en source med data för demo
This commit is contained in:
@ -2,107 +2,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Books.ConsoleApp;
|
||||
|
||||
namespace Books.Console
|
||||
{
|
||||
class Program
|
||||
{
|
||||
List<BooksByAuthor> BooksByAuthorCatalogue = null;
|
||||
private static IBooksSource BooksSource = new BooksJsonSource();
|
||||
private static List<BooksByAuthor> BooksByAuthorCatalog = null;
|
||||
static void Main(string[] args)
|
||||
{
|
||||
IEnumerable<Book> books = BookSource.Read();
|
||||
BooksByAuthorCatalogue = new List<BooksByAuthor>();
|
||||
IEnumerable<Book> books = BooksSource.Read();
|
||||
BooksByAuthorCatalog = new List<BooksByAuthor>();
|
||||
|
||||
foreach (var book in books)
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (AuthorIsAlreadyCataloged(book.author))
|
||||
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)
|
||||
{
|
||||
// there are some(1 or more) books by this author already found and catalogued
|
||||
var authorCatalogueIndex = LocateAuthorAlreadyCataloged(book.author);
|
||||
|
||||
var existingBooks = BooksByAuthorCatalogue[authorCatalogIndex].Books;
|
||||
existingBooks.Add(book);
|
||||
}
|
||||
else
|
||||
{
|
||||
CatalogueNewAuthor(book);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static bool AuthorIsAlreadyCataloged(string author)
|
||||
{
|
||||
var authorAlreadyCatalogued = false;
|
||||
|
||||
// we'll iterate over the catalogue to find the author - if athors's already been cataloged
|
||||
|
||||
foreach (var entry in BooksByAuthorCatalogue)
|
||||
{
|
||||
if (entry.author == author)
|
||||
{
|
||||
authorAlreadyCatalogued = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return authorAlreadyCatalogued;
|
||||
|
||||
}
|
||||
|
||||
private static int LocateAuthorAlreadyCataloged(string author)
|
||||
{
|
||||
var authorCatalogIndex = 0;
|
||||
|
||||
// We'll iterate over the cataloge to find the author's index
|
||||
for (int j = 0; j < BooksByAuthorCatalogue.Count; j++)
|
||||
{
|
||||
var entry = BooksByAuthorCatalogue[j];
|
||||
if (entry.Author == author)
|
||||
{
|
||||
authorCatalogIndex = j;
|
||||
break;
|
||||
case '1': Output.BooksByAuthor(BooksSource.Read()); break;
|
||||
case '2': DoSearch(); break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void CatalogueNewAuthor(Book b)
|
||||
public static void DoSearch()
|
||||
{
|
||||
// there are NONE books by this author already found and cataloged
|
||||
|
||||
var newBookList = new List<Book> { b };
|
||||
var authorAndBooks = new BooksByAuthor(b.Author, newBookList);
|
||||
|
||||
BooksByAuthorCatalogue.Add(authorAndBooks);
|
||||
}
|
||||
|
||||
private static void OutputBooksByAuthor()
|
||||
{
|
||||
foreach (var ba in BooksByAuthorCatalogue)
|
||||
var books = BooksSource.Read();
|
||||
while (true)
|
||||
{
|
||||
System.Console.Write("Author: {0, -28} Books: ", ba.Author);
|
||||
foreach (var book in ba.Books)
|
||||
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")
|
||||
{
|
||||
System.Console.Write(book.title + ", ");
|
||||
return;
|
||||
}
|
||||
System.Console.Write(Environment.NewLine);
|
||||
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("----------------------");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class BooksByAuthor
|
||||
{
|
||||
public readonly string Author;
|
||||
public readonly List<Book> Books;
|
||||
|
||||
public BooksByAuthor(string author, List<Book> books)
|
||||
{
|
||||
Author = author;
|
||||
Books = books;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user