public static void CollectInput(Farm farm) { Console.WriteLine("1. Chicken"); Console.WriteLine("2. Cow"); Console.WriteLine("3. Duck"); Console.WriteLine("4. Pig"); Console.WriteLine("5. Goat"); Console.WriteLine("6. Ostrich"); Console.WriteLine("7. Sheep"); Console.WriteLine(); Console.WriteLine("What are you buying today?"); Console.Write("> "); string choice = Console.ReadLine(); try { if (int.Parse(choice) <= 7 && int.Parse(choice) >= 1) { switch (Int32.Parse(choice)) { case 1: ChooseChickenCoop.CollectInput(farm, new Chicken()); break; case 2: ChooseGrazingField.CollectInput(farm, new Cow()); break; case 3: ChooseDuckHouse.CollectInput(farm, new Duck()); break; case 4: ChooseGrazingField.CollectInput(farm, new Pig()); break; case 5: ChooseGrazingField.CollectInput(farm, new Goat()); break; case 6: ChooseGrazingField.CollectInput(farm, new Ostrich()); break; case 7: ChooseGrazingField.CollectInput(farm, new Sheep()); break; default: Console.WriteLine("Invalid option. Please try again."); Thread.Sleep(2000); break; } } else { Console.WriteLine("Invalid option. Please try again."); Thread.Sleep(2000); DisplayBanner(); PurchaseLivestock.CollectInput(farm); } } catch (FormatException) { Console.WriteLine("Invalid option. Please try again."); Thread.Sleep(2000); DisplayBanner(); PurchaseLivestock.CollectInput(farm); } }
public static void CollectInput(Farm farm, IGrazing animal) { Console.Clear(); List <IFacility <IGrazing> > openFields = new List <IFacility <IGrazing> >(); var sortedGrazingFields = farm.GrazingFields.Where(grazingField => (grazingField.Capacity - 1) >= grazingField.CurrentStock()).ToList(); for (int i = 0; i < sortedGrazingFields.Count; i++) { if ((sortedGrazingFields[i].Capacity - 1) >= sortedGrazingFields[i].CurrentStock()) { openFields.Add(sortedGrazingFields[i]); Console.WriteLine($"{i + 1}. Grazing Field (Current Stock: {sortedGrazingFields[i].CurrentStock()})"); sortedGrazingFields[i].ShowAnimalsByType(); } } Console.WriteLine(); if (sortedGrazingFields.Count > 0) { Console.WriteLine($"Place the animal where?"); Console.Write("> "); try { int choice = Int32.Parse(Console.ReadLine()); if (animal is IGrazing) { try { sortedGrazingFields[choice - 1].AddResource(animal); } catch (ArgumentOutOfRangeException) { Console.WriteLine("Invalid option. Please create a new animal and try again."); Thread.Sleep(2000); DisplayBanner(); PurchaseLivestock.CollectInput(farm); } } else { Console.WriteLine("There are no matching facilities available. Please create one first."); Thread.Sleep(2000); } } catch (FormatException) { Console.WriteLine("Invalid option. Please create a new animal and try again."); Thread.Sleep(2000); DisplayBanner(); PurchaseLivestock.CollectInput(farm); } } /* * Couldn't get this to work. Can you? * Stretch goal. Only if the app is fully functional. */ // farm.PurchaseResource<IGrazing>(animal, choice); }