Wrong version of .NET Core in one of the projects
This commit is contained in:
23
Books.ConsoleApp/Books.Console.csproj
Normal file
23
Books.ConsoleApp/Books.Console.csproj
Normal file
@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RootNamespace>Books.ConsoleApp</RootNamespace>
|
||||
<AssemblyName>Books.ConsoleApp</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="books.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="books1.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
16
Books.ConsoleApp/BooksByAuthor.cs
Normal file
16
Books.ConsoleApp/BooksByAuthor.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Books.ConsoleApp
|
||||
{
|
||||
public class BooksByAuthor
|
||||
{
|
||||
public readonly string Author;
|
||||
public readonly List<Book> Books;
|
||||
|
||||
public BooksByAuthor(string author, List<Book> books)
|
||||
{
|
||||
Author = author;
|
||||
Books = books;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Books.ConsoleApp/IEnumerableAndIEnumerator.cs
Normal file
18
Books.ConsoleApp/IEnumerableAndIEnumerator.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace Books.Examples
|
||||
{
|
||||
|
||||
public interface IEnumerable<out T>
|
||||
{
|
||||
IEnumerator<T> GetEnumerator();
|
||||
}
|
||||
|
||||
public interface IEnumerator<out T>
|
||||
{
|
||||
T Current { get; }
|
||||
|
||||
bool MoveNext();
|
||||
|
||||
void Reset();
|
||||
}
|
||||
|
||||
}
|
||||
@ -65,16 +65,4 @@ namespace Books.ConsoleApp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BooksByAuthor
|
||||
{
|
||||
public readonly string Author;
|
||||
public readonly List<Book> Books;
|
||||
|
||||
public BooksByAuthor(string author, List<Book> books)
|
||||
{
|
||||
Author = author;
|
||||
Books = books;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<BooksByAuthor> BooksByAuthorCatalog = null;
|
||||
static void Main(string[] args)
|
||||
|
||||
// we will use this as global state
|
||||
private static List<BooksByAuthor> BooksByAuthorCatalog;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
IEnumerable<Book> books = BooksSource.Read();
|
||||
|
||||
BooksByAuthorCatalog = new List<BooksByAuthor>();
|
||||
|
||||
|
||||
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<Book> { 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -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<string> 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<IEnumerable<BooksByAuthor>, string, BooksByAuthor> getGataloguedAuthor =
|
||||
(booksByAuthor, authorName) => {
|
||||
(booksByAuthor, authorName) =>
|
||||
{
|
||||
// can do complex stuff or help in debugging
|
||||
return booksByAuthor.FirstOrDefault(ba => ba.Author == authorName);
|
||||
};
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user