diff --git a/Books.ConsoleApp/Books.Console.csproj b/Books.ConsoleApp/Books.Console.csproj new file mode 100644 index 0000000..d05b737 --- /dev/null +++ b/Books.ConsoleApp/Books.Console.csproj @@ -0,0 +1,23 @@ + + + + Exe + netcoreapp2.0 + Books.ConsoleApp + Books.ConsoleApp + + + + + + + + + PreserveNewest + + + Always + + + + diff --git a/Books.ConsoleApp/BooksByAuthor.cs b/Books.ConsoleApp/BooksByAuthor.cs new file mode 100644 index 0000000..5c60124 --- /dev/null +++ b/Books.ConsoleApp/BooksByAuthor.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace Books.ConsoleApp +{ + public class BooksByAuthor + { + public readonly string Author; + public readonly List Books; + + public BooksByAuthor(string author, List books) + { + Author = author; + Books = books; + } + } +} \ No newline at end of file diff --git a/Books.ConsoleApp/IEnumerableAndIEnumerator.cs b/Books.ConsoleApp/IEnumerableAndIEnumerator.cs new file mode 100644 index 0000000..e399527 --- /dev/null +++ b/Books.ConsoleApp/IEnumerableAndIEnumerator.cs @@ -0,0 +1,18 @@ +namespace Books.Examples +{ + + public interface IEnumerable + { + IEnumerator GetEnumerator(); + } + + public interface IEnumerator + { + T Current { get; } + + bool MoveNext(); + + void Reset(); + } + +} diff --git a/Books.ConsoleApp/Output.cs b/Books.ConsoleApp/Output.cs index a829ae0..22e951a 100644 --- a/Books.ConsoleApp/Output.cs +++ b/Books.ConsoleApp/Output.cs @@ -65,16 +65,4 @@ namespace Books.ConsoleApp } } } - - public class BooksByAuthor - { - public readonly string Author; - public readonly List Books; - - public BooksByAuthor(string author, List books) - { - Author = author; - Books = books; - } - } } diff --git a/Books.ConsoleApp/Program.cs b/Books.ConsoleApp/Program.cs index 9df9b2b..79b5ced 100644 --- a/Books.ConsoleApp/Program.cs +++ b/Books.ConsoleApp/Program.cs @@ -1,71 +1,108 @@ -using System; + +#region imports +using System; using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Globalization; using System.Linq; -using Books.ConsoleApp; - +using System.Threading.Tasks; +#endregion namespace Books.ConsoleApp { class Program { private static IBooksSource BooksSource = new BooksJsonSource(); - private static List BooksByAuthorCatalog = null; - static void Main(string[] args) + + // we will use this as global state + private static List BooksByAuthorCatalog; + + public static void Main() { IEnumerable books = BooksSource.Read(); + BooksByAuthorCatalog = new List(); - - - while (true) + + foreach(var book in books) { - 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) + if (AuthorIsAlreadyCataloged(book.author)) { - case '1': Output.BooksByAuthor(BooksSource.Read()); break; - case '2': DoSearch(); break; - default: return; + // there are some(1 or more) books by this author already found and catalogued + var authorCatalogIndex = LocateAuthorAlreadyCataloged(book.author); + + var existingBooks = BooksByAuthorCatalog[authorCatalogIndex].Books; + existingBooks.Add(book); + } + else + { + CatalogueNewAuthor(book); } } + // now we have an list that has all the authors catalogued + OutputBooksByAuthor(); + + Console.WriteLine("Finished cataloguing authors. (press a key to exit...)"); + Console.ReadLine(); } - public static void DoSearch() + private static bool AuthorIsAlreadyCataloged(string author) { - var books = BooksSource.Read(); - while (true) + var authorAlreadyCatalogued = false; + + // we'll iterate over the cataloge to find the author - if author's already been cataloged + for (int j = 0; j < BooksByAuthorCatalog.Count; j++) { - 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") + var entry = BooksByAuthorCatalog[j]; + if (entry.Author == author) { - return; + authorAlreadyCatalogued = true; + break; } - 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("----------------------"); } + + 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 < BooksByAuthorCatalog.Count; j++) + { + var entry = BooksByAuthorCatalog[j]; + if (entry.Author == author) + { + authorCatalogIndex = j; + break; + } + } + + return authorCatalogIndex; + } + + private static void CatalogueNewAuthor(Book b) + { + // there are NONE books by this author already found and cataloged + + var newBooksList = new List { b }; + var authorAndBooks = new BooksByAuthor(b.author, newBooksList); + + BooksByAuthorCatalog.Add(authorAndBooks); + } + + + private static void OutputBooksByAuthor() + { + foreach(var ba in BooksByAuthorCatalog) + { + Console.Write("Author: {0,-28} Books: ", ba.Author); + foreach (var book in ba.Books) + { + Console.Write(book.title + ", "); + } + Console.Write(Environment.NewLine); + } + } } + + } diff --git a/Demos/DemoFunc.cs b/Demos/DemoFunc.cs index 95e313c..830b59d 100644 --- a/Demos/DemoFunc.cs +++ b/Demos/DemoFunc.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Books.ConsoleApp; public class DemoFunc { @@ -22,7 +23,7 @@ public class DemoFunc Func helloWorldFunc = AsFunc("Hello World!"); var helloWorld = Exec(helloWorldFunc); // helloWorld == "Hello World!" - + intFunc = AsFunc(1); var one = Exec(intFunc); // one == 1); @@ -37,7 +38,8 @@ public class DemoFunc Func, string, BooksByAuthor> getGataloguedAuthor = - (booksByAuthor, authorName) => { + (booksByAuthor, authorName) => + { // can do complex stuff or help in debugging return booksByAuthor.FirstOrDefault(ba => ba.Author == authorName); }; diff --git a/Demos/Demos.csproj b/Demos/Demos.csproj index 967bb6b..3143839 100644 --- a/Demos/Demos.csproj +++ b/Demos/Demos.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.0 + netcoreapp3.0