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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization; using System.Globalization;
namespace Books.ConsoleApp namespace Books.ConsoleApp
@ -9,12 +10,11 @@ namespace Books.ConsoleApp
List<BooksByAuthor> BooksByAuthorCatalogue = null; List<BooksByAuthor> BooksByAuthorCatalogue = null;
static void Main(string[] args) static void Main(string[] args)
{ {
Book[] books = BookSource.Read(); IEnumerable<Book> books = BookSource.Read();
BooksByAuthorCatalogue = new List<BooksByAuthor>(); 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)) if (AuthorIsAlreadyCataloged(book.author))
{ {
// there are some(1 or more) books by this author already found and catalogued // there are some(1 or more) books by this author already found and catalogued
@ -37,9 +37,9 @@ namespace Books.ConsoleApp
var authorAlreadyCatalogued = false; var authorAlreadyCatalogued = false;
// we'll iterate over the catalogue to find the author - if athors's already been cataloged // 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) if (entry.author == author)
{ {
authorAlreadyCatalogued = true; authorAlreadyCatalogued = true;
@ -80,13 +80,12 @@ namespace Books.ConsoleApp
private static void OutputBooksByAuthor() 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); 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); Console.Write(Environment.NewLine);
} }