Wrong version of .NET Core in one of the projects

This commit is contained in:
2019-11-26 21:17:07 +01:00
parent 1e60f78192
commit 56b44a63a5
7 changed files with 144 additions and 60 deletions

View 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>

View 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;
}
}
}

View 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();
}
}

View File

@ -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;
}
}
} }

View File

@ -1,71 +1,108 @@
using System; 
#region imports
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq; using System.Linq;
using Books.ConsoleApp; using System.Threading.Tasks;
#endregion
namespace Books.ConsoleApp namespace Books.ConsoleApp
{ {
class Program class Program
{ {
private static IBooksSource BooksSource = new BooksJsonSource(); 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(); IEnumerable<Book> books = BooksSource.Read();
BooksByAuthorCatalog = new List<BooksByAuthor>(); BooksByAuthorCatalog = new List<BooksByAuthor>();
foreach(var book in books)
while (true)
{ {
System.Console.WriteLine("\nActions available:"); if (AuthorIsAlreadyCataloged(book.author))
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; // there are some(1 or more) books by this author already found and catalogued
case '2': DoSearch(); break; var authorCatalogIndex = LocateAuthorAlreadyCataloged(book.author);
default: return;
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(); var authorAlreadyCatalogued = false;
while (true)
// 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 entry = BooksByAuthorCatalog[j];
var searchTerm = System.Console.ReadLine(); if (entry.Author == author)
if (searchTerm == "exit")
{ {
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);
}
}
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Books.ConsoleApp;
public class DemoFunc public class DemoFunc
{ {
@ -22,7 +23,7 @@ public class DemoFunc
Func<string> helloWorldFunc = AsFunc("Hello World!"); Func<string> helloWorldFunc = AsFunc("Hello World!");
var helloWorld = Exec(helloWorldFunc); var helloWorld = Exec(helloWorldFunc);
// helloWorld == "Hello World!" // helloWorld == "Hello World!"
intFunc = AsFunc(1); intFunc = AsFunc(1);
var one = Exec(intFunc); var one = Exec(intFunc);
// one == 1); // one == 1);
@ -37,7 +38,8 @@ public class DemoFunc
Func<IEnumerable<BooksByAuthor>, string, BooksByAuthor> getGataloguedAuthor = Func<IEnumerable<BooksByAuthor>, string, BooksByAuthor> getGataloguedAuthor =
(booksByAuthor, authorName) => { (booksByAuthor, authorName) =>
{
// can do complex stuff or help in debugging // can do complex stuff or help in debugging
return booksByAuthor.FirstOrDefault(ba => ba.Author == authorName); return booksByAuthor.FirstOrDefault(ba => ba.Author == authorName);
}; };

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>