private static Animal GetAnimal(string animalInput, List <Animal> animals) { var animalTokens = animalInput .Split(); var type = animalTokens[0]; var name = animalTokens[1]; var weight = double.Parse(animalTokens[2]); Animal animal = null; switch (type) { case "Owl": var owlWingSize = double.Parse(animalTokens[3]); animal = new Owl(name, weight, owlWingSize); break; case "Hen": var henWingSize = double.Parse(animalTokens[3]); animal = new Hen(name, weight, henWingSize); break; case "Mouse": var mouseLivingRegion = animalTokens[3]; animal = new Mouse(name, weight, mouseLivingRegion); break; case "Dog": var dogLivingRegion = animalTokens[3]; animal = new Dog(name, weight, dogLivingRegion); break; case "Cat": var catLivingRegion = animalTokens[3]; var catBreed = animalTokens[4]; animal = new Cat(name, weight, catLivingRegion, catBreed); break; case "Tiger": var tigerLivingRegion = animalTokens[3]; var tigerBreed = animalTokens[4]; animal = new Tiger(name, weight, tigerLivingRegion, tigerBreed); break; default: throw new InvalidOperationException("Invalid animal type!"); } animals.Add(animal); return(animal); }
private static Animal CreateAnimal(string[] information) { string type = information[0]; string name = information[1]; double weight = double.Parse(information[2]); switch (type) { case "Owl": double wingSize = double.Parse(information[3]); Animal animal = new Owl(name, weight, wingSize); return(animal); case "Hen": wingSize = double.Parse(information[3]); animal = new Hen(name, weight, wingSize); return(animal); case "Mouse": string livingRegion = information[3]; animal = new Mouse(name, weight, livingRegion); return(animal); case "Dog": livingRegion = information[3]; animal = new Dog(name, weight, livingRegion); return(animal); case "Cat": livingRegion = information[3]; string breed = information[4]; animal = new Cat(name, weight, livingRegion, breed); return(animal); case "Tiger": livingRegion = information[3]; breed = information[4]; animal = new Tiger(name, weight, livingRegion, breed); return(animal); default: return(null); } }
private static Animal AddFeline(string[] animalArgs) { var type = animalArgs[0]; var name = animalArgs[1]; var weight = double.Parse(animalArgs[2]); var region = animalArgs[3]; var breed = animalArgs[4]; Animal feline; if (type == "Cat") { feline = new Cat(name, weight, region, breed); } else { feline = new Tiger(name, weight, region, breed); } return(feline); }
static void Main(string[] args) { var animals = new List <Animal>(); var command = Console.ReadLine(); while (command != "End") { var tokens = command.Split(); var animalType = tokens[0]; var name = tokens[1]; var weight = double.Parse(tokens[2]); if (Mammals.Contains(animalType)) { var livingRegion = tokens[3]; switch (animalType) { case "Cat": var catBreed = tokens[4]; var cat = new Cat(name, weight, 0, livingRegion, catBreed); animals.Add(cat); FeedAnimal(cat); break; case "Tiger": var tigerBreed = tokens[4]; var tiger = new Tiger(name, weight, 0, livingRegion, tigerBreed); animals.Add(tiger); FeedAnimal(tiger); break; case "Dog": var dog = new Dog(name, weight, 0, livingRegion); animals.Add(dog); FeedAnimal(dog); break; case "Mouse": var mouse = new Mouse(name, weight, 0, livingRegion); animals.Add(mouse); FeedAnimal(mouse); break; default: break; } } else { var wingSize = double.Parse(tokens[3]); switch (animalType) { case "Owl": var owl = new Owl(name, weight, 0, wingSize); animals.Add(owl); FeedAnimal(owl); break; case "Hen": var hen = new Hen(name, weight, 0, wingSize); animals.Add(hen); FeedAnimal(hen); break; default: break; } } command = Console.ReadLine(); } foreach (var animal in animals) { Console.WriteLine(animal); } }