Add project files.

This commit is contained in:
2019-11-24 22:48:24 +01:00
parent 3695ce8514
commit d732ec5812
4 changed files with 227 additions and 0 deletions

25
Books.ConsoleApp.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29509.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Books.ConsoleApp", "Books.ConsoleApp\Books.ConsoleApp.csproj", "{A09D7905-08C5-41F3-ADE6-2037471A2403}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A09D7905-08C5-41F3-ADE6-2037471A2403}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A09D7905-08C5-41F3-ADE6-2037471A2403}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A09D7905-08C5-41F3-ADE6-2037471A2403}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A09D7905-08C5-41F3-ADE6-2037471A2403}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F1B272A-118E-4F00-8E83-C7C02A973BD2}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>

109
Books.ConsoleApp/Program.cs Normal file
View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Books.ConsoleApp
{
class Program
{
List<BooksByAuthor> BooksByAuthorCatalogue = null;
static void Main(string[] args)
{
Book[] books = BookSource.Read();
BooksByAuthorCatalogue = new List<BooksByAuthor>();
for (int i = 0; i < books.Length; i++)
{
var book = books[i];
if (AuthorIsAlreadyCataloged(book.author))
{
// 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
for (int j = 0; j < BooksByAuthorCatalogue.Count; j++)
{
var entry = BooksByAuthorCatalogue[j];
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;
}
}
}
private static void CatalogueNewAuthor(Book b)
{
// 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()
{
for (int i = 0; i < BooksByAuthorCatalogue.Count; i++)
{
BooksByAuthor ba = BooksByAuthorCatalogue[i];
Console.Write("Author: {0, -28} Books: ", ba.Author);
for (int j = 0; j < ba.Books.Count; j++)
{
Console.Write(ba.Books[j].title + ", ");
}
Console.Write(Environment.NewLine);
}
}
}
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,85 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
namespace Books.ConsoleApp
{
public static class SearchByAuthor
{
public static IEnumerable<string> MatchBy(
IEnumerable<Book> books, string searchCriteria)
{
return books
.ExtractAuthors()
.ListAuthorsMatching(searchCriteria);
}
public static IEnumerable<string> SuggestAuthors(
IEnumerable<Book> books, int suggestionLength = 5)
{
return books
.ExtractAuthors()
.RandomizeOrder()
.Take(suggestionLength);
}
public static IEnumerable<Book> ListBooks(IEnumerable<Book> books, string author)
{
return books
.CatalogueByAuthor()
.BooksBy(author);
}
public static AuthorCatalogue CatalogueByAuthor(this IEnumerable<Book> books)
{
return books
.ToLookup(b => b.author)
// Mark Twain : {The Adventures of Huckleberry Finn},
// Shakespeare: {Othello, Hamlet, ...}
// ...
;
}
public static IEnumerable<Book> BooksBy(this AuthorCatalogue catalogue, string author)
{
return catalogue.Contains(author)
? catalogue[author]
: Enumerable.Empty<Book>();
}
public static IEnumerable<string> ExtractAuthors(this IEnumerable<Book> books)
{
return books.Select(b =>
{
//do stuff here
return b.author;
}).Distinct();
}
public static IEnumerable<string> ListAuthorsMatching(
this IEnumerable<string> authors, string searchCriteria)
{
var partOfNameLower = searchCriteria.ToLower();
return authors.Where(a => a.ToLower().Contains(partOfNameLower));
}
private static IOrderedEnumerable<string> RandomizeOrder(this IEnumerable<string> authors)
{
return authors.OrderBy(_ => Guid.NewGuid());
}
}
public class AuthorCatalogue
{
}
public class Book
{
}
}