public static void Main() { var animals = new List <Animal>(); string typeOfAnimal = Console.ReadLine(); while (typeOfAnimal != "Beast!") { var animalTokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); try { switch (typeOfAnimal.ToLower()) { case "cat": Animal cat = new Cat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]); animals.Add(cat); break; case "dog": Animal dog = new Dog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]); animals.Add(dog); break; case "frog": Animal frog = new Frog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]); animals.Add(frog); break; case "kitten": Animal kitten = new Kitten(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]); animals.Add(kitten); break; case "tomcat": Animal tomcat = new Tomcat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]); animals.Add(tomcat); break; default: Console.WriteLine("Invalid input!"); break; } } catch (Exception ex) { Console.WriteLine(ex.Message);; } typeOfAnimal = Console.ReadLine(); } foreach (var animal in animals) { Console.WriteLine(animal.IntroduceAnimal()); Console.WriteLine(animal.ProduceSound()); } }
public static void Main() { var animals = new List <Animal>(); var animalTypeName = Console.ReadLine(); while (animalTypeName != "Beast!") { try { var animalTypeArgs = Console.ReadLine().Split(); var name = animalTypeArgs[0]; var age = int.Parse(animalTypeArgs[1]); var gender = animalTypeArgs[2]; switch (animalTypeName) { case "Cat": var cat = new Cat(name, age, gender); animals.Add(cat); break; case "Dog": var dog = new Dog(name, age, gender); animals.Add(dog); break; case "Frog": var frog = new Frog(name, age, gender); animals.Add(frog); break; case "Kitten": var kittens = new Kitten(name, age, "Female"); animals.Add(kittens); break; case "Tomcat": var tomcat = new Tomcat(name, age, "Male"); animals.Add(tomcat); break; default: Console.WriteLine("Invalid input!"); break; } } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } catch (Exception) { Console.WriteLine("Invalid input!"); } animalTypeName = Console.ReadLine(); } PrintResult(animals); }
public static void Main(string[] args) { StringBuilder result = new StringBuilder(); string animalType; string[] validAnimals = new string[] { "Dog", "Cat", "Frog", "Tomcat", "Kitten" }; while ((animalType = Console.ReadLine()) != "Beast!") { try { string[] animalParams = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); string name = animalParams[0]; int age = int.Parse(animalParams[1]); string gender = string.Empty; if (animalParams.Length > 2) { gender = animalParams[2]; } if (animalType == "Dog") { Dog dog = new Dog(name, age, gender); result.AppendLine(dog.ToString()); } else if (animalType == "Frog") { Frog frog = new Frog(name, age, gender); result.AppendLine(frog.ToString()); } else if (animalType == "Cat") { Cat cat = new Cat(name, age, gender); result.AppendLine(cat.ToString()); } else if (animalType == "Tomcat") { Tomcat tomcat = new Tomcat(name, age); result.AppendLine(tomcat.ToString()); } else if (animalType == "Kitten") { Kitten kitten = new Kitten(name, age); result.AppendLine(kitten.ToString()); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException) { Console.WriteLine("Invalid input!"); } if (!validAnimals.Contains(animalType)) { Console.WriteLine("Invalid input!"); } } Console.Write(result); }