private void SellFromGenre(Genre genre, Market market, List<Book>[] printedBooks, List<Book>[] eBooks, Player[] players, Random rand) { List<Book> printedOfGenre = new List<Book>(); List<Book> digitalOfGenre = new List<Book>(); //Gets the demands of the specific genre from the Market class int printedGenreDemand = market.GetDemand(genre, false); int digitalGenreDemand = market.GetDemand(genre, true); //Collects all the books of the specific genre from all players for (int i = 0; i < printedBooks.Length; i++) printedOfGenre.AddRange(printedBooks[i].Where(b => b.GetGenre() == genre)); for (int i = 0; i < eBooks.Length; i++) digitalOfGenre.AddRange(eBooks[i].Where(b => b.GetGenre() == genre)); foreach (Book book in printedOfGenre) book.CalcProfitablity(); //Sell digital books for (int i = 0; i < digitalGenreDemand; i++) { if (digitalOfGenre.Count == 0) break; int index = rand.Next(0, digitalOfGenre.Count); SellBook(genre, index, true, digitalOfGenre, players, market, ref digitalGenreDemand); } //Leftover demand for ebooks is sold printed books for (int i = 0; i < digitalGenreDemand; i++) { if (printedOfGenre.Count == 0) break; int index = rand.Next(0, printedOfGenre.Count); SellBook(genre, index, true, printedOfGenre, players, market, ref digitalGenreDemand); } //Sell printed books for (int i = 0; i < printedGenreDemand; i++) { if (printedOfGenre.Count == 0) break; int index = rand.Next(0, printedOfGenre.Count); SellBook(genre, index, false, printedOfGenre, players, market, ref printedGenreDemand); } }