public static void Main() { var inputLine = Console.ReadLine(); while (inputLine != "End") { var animalInfo = inputLine.Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries); var foodInfo = Console.ReadLine() .Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries); Animal animal = AnimalFactory.GetAnimal(animalInfo); Food food = FoodFactory.GetFood(foodInfo); Console.WriteLine(animal.MakeSound()); try { animal.EatFood(food); } catch (ArgumentException e) { Console.WriteLine(e.Message); } Console.WriteLine(animal); inputLine = Console.ReadLine(); } }
public static void Main() { string inputLine; while ((inputLine = Console.ReadLine()) != "End") { var animalTokens = inputLine.Split(new [] { '\t', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); var foodTokens = Console.ReadLine() .Split(new[] { '\t', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); Animal animal = AnimalFactory.GetAnimal(animalTokens); Food food = FoodFactory.GetFood(foodTokens); Console.WriteLine(animal.MakeSound()); try { animal.Eat(food); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(animal); } }
private static void ReadAndParseInput(List <Animal> animals, List <Food> foods) { string input = Console.ReadLine(); while (input != "End") { string[] animalParts = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string[] foodParts = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Animal animal = AnimalFactory.GetAnimal(animalParts); Food food = FoodFactory.GetFood(foodParts); animals.Add(animal); foods.Add(food); input = Console.ReadLine(); } }
static void Main(string[] args) { var animals = new List <Animal>(); string inputLine; while ((inputLine = Console.ReadLine()) != "End") { try { var input = inputLine.Split(); if (input.Length == 2) { var foodName = input[0]; var quantity = int.Parse(input[1]); var food = FoodFactory.GetFood(foodName, quantity); var animal = animals.Last(); animal.Eate(food); continue; } var animalType = input[0]; var animalName = input[1]; var animalWeight = double.Parse(input[2]); var livingRegion = input[3]; string breed = null; if (input.Length == 5) { breed = input[4]; } animals.Add(AnimalFactory.GetAnimal(animalType, animalName, animalWeight, livingRegion, breed)); } catch (ArgumentException e) { Console.WriteLine(e.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }