Changied to foreach

This commit is contained in:
2019-11-24 23:14:41 +01:00
parent d732ec5812
commit 0943935228

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
namespace Books.ConsoleApp
@ -9,12 +10,11 @@ namespace Books.ConsoleApp
List<BooksByAuthor> BooksByAuthorCatalogue = null;
static void Main(string[] args)
{
Book[] books = BookSource.Read();
IEnumerable<Book> books = BookSource.Read();
BooksByAuthorCatalogue = new List<BooksByAuthor>();
for (int i = 0; i < books.Length; i++)
foreach (var book in books)
{
var book = books[i];
if (AuthorIsAlreadyCataloged(book.author))
{
// there are some(1 or more) books by this author already found and catalogued
@ -37,9 +37,9 @@ namespace Books.ConsoleApp
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++)
foreach (var entry in BooksByAuthorCatalogue)
{
var entry = BooksByAuthorCatalogue[j];
if (entry.author == author)
{
authorAlreadyCatalogued = true;
@ -72,7 +72,7 @@ namespace Books.ConsoleApp
{
// there are NONE books by this author already found and cataloged
var newBookList = new List<Book> {b};
var newBookList = new List<Book> { b };
var authorAndBooks = new BooksByAuthor(b.Author, newBookList);
BooksByAuthorCatalogue.Add(authorAndBooks);
@ -80,13 +80,12 @@ namespace Books.ConsoleApp
private static void OutputBooksByAuthor()
{
for (int i = 0; i < BooksByAuthorCatalogue.Count; i++)
foreach (var ba in BooksByAuthorCatalogue)
{
BooksByAuthor ba = BooksByAuthorCatalogue[i];
Console.Write("Author: {0, -28} Books: ", ba.Author);
for (int j = 0; j < ba.Books.Count; j++)
foreach (var book in ba.Books)
{
Console.Write(ba.Books[j].title + ", ");
Console.Write(book.title + ", ");
}
Console.Write(Environment.NewLine);
}