public static void Main(string[] args) { string type; while (!(type = Console.ReadLine()).Equals("Beast!")) { var tokens = Console.ReadLine() .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); try { Animal animal = null; switch (type) { case "Cat": animal = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]); break; ; case "Dog": animal = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]); break; case "Frog": animal = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]); break; ; case "Kittens": animal = new Kitten(tokens[0], int.Parse(tokens[1]), tokens[2]); break; case "Tomcat": animal = new Tomcat(tokens[0], int.Parse(tokens[1]), tokens[2]); break; default: throw new ArgumentException("Invalid input!"); } Console.WriteLine(animal); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException) { Console.WriteLine("Invalid input!"); } } }
private static void ReadAndCreateAnimal(List <Animal> animals, string animalType) { string[] tokens = Console.ReadLine().Split(); string name = tokens[0]; int age = int.Parse(tokens[1]); string gender = null; if (tokens.Length == 3) { gender = tokens[2]; } switch (animalType) { case "Cat": Cat cat = new Cat(name, age, gender); animals.Add(cat); break; case "Dog": Dog dog = new Dog(name, age, gender); animals.Add(dog); break; case "Frog": Frog frog = new Frog(name, age, gender); animals.Add(frog); break; case "Tomcat": Tomcat tomcat = new Tomcat(name, age); animals.Add(tomcat); break; case "Kitten": Kitten kitten = new Kitten(name, age); animals.Add(kitten); break; default: throw new ArgumentException("Invalid input!"); } }
static void AddAnimal(string command, List <Animal> animals) { var animalType = command; var info = Console.ReadLine().Split(); var name = info[0]; var age = int.Parse(info[1]); var gender = info[2]; switch (animalType) { case "Cat": Cat cat = new Cat(name, age, gender, animalType); animals.Add(cat); break; case "Dog": Dog dog = new Dog(name, age, gender, animalType); animals.Add(dog); break; case "Frog": Frog frog = new Frog(name, age, gender, animalType); animals.Add(frog); break; case "Kitten": Kitten kitten = new Kitten(name, age, gender, animalType); animals.Add(kitten); break; case "Tomcat": Tomcat tomcat = new Tomcat(name, age, gender, animalType); animals.Add(tomcat); break; default: throw new ArgumentException("Invalid input!"); } }