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": 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 "Tomcat": var tomcat = new Tomcat(name, age); animals.Add(tomcat); break; case "Kitten": var kitten = new Kitten(name, age); animals.Add(kitten); break; default: throw new ArgumentException("Invalid input!"); } }
private static void ParseInput(string input) { string[] tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (input) { case "Cat": var cat = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(cat); Console.WriteLine(cat.ProduceSound()); break; case "Dog": var dog = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(dog); Console.WriteLine(dog.ProduceSound()); break; case "Frog": var frog = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(frog); Console.WriteLine(frog.ProduceSound()); break; case "Kitten": var kitten = new Kitten(tokens[0], int.Parse(tokens[1])); Console.WriteLine(kitten); Console.WriteLine(kitten.ProduceSound()); break; case "Tomcat": var tomcat = new Tomcat(tokens[0], int.Parse(tokens[1])); Console.WriteLine(tomcat); Console.WriteLine(tomcat.ProduceSound()); break; default: Console.WriteLine("Not implemented!"); break; } }
static void CreateAnimal(string animalData, string animalType, List <Animal> animals) { Animal currAnimal = null; string[] tokens = animalData.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()) .ToArray(); if (tokens.Length < 2) { throw new ArgumentException("Invalid input!"); } if (animalType.ToLower() == "dog") { currAnimal = new Dog(tokens[0], tokens[1], tokens[2]); } else if (animalType.ToLower() == "cat") { currAnimal = new Cat(tokens[0], tokens[1], tokens[2]); } else if (animalType.ToLower() == "frog") { currAnimal = new Frog(tokens[0], tokens[1], tokens[2]); } else if (animalType.ToLower() == "kitten") { currAnimal = new Kitten(tokens[0], tokens[1], tokens.Length == 3 ? tokens[2] : null); } else if (animalType.ToLower() == "tomcat") { currAnimal = new Tomcat(tokens[0], tokens[1], tokens.Length == 3 ? tokens[2] : null); } else { throw new ArgumentException("Invalid input!"); } if (currAnimal != null) { animals.Add(currAnimal); } }
private static void AddValidAnimal(List <Animal> animals, string animalType, string name, int age, string gender) { Animal currentAnimal; switch (animalType) { case "Cat": currentAnimal = new Cat(name, age, gender); animals.Add(currentAnimal); break; case "Kitten": currentAnimal = new Kitten(name, age, gender); animals.Add(currentAnimal); break; case "Tomcat": currentAnimal = new Tomcat(name, age, gender); animals.Add(currentAnimal); break; case "Dog": currentAnimal = new Dog(name, age, gender); animals.Add(currentAnimal); break; case "Frog": currentAnimal = new Frog(name, age, gender); animals.Add(currentAnimal); break; default: Console.WriteLine("Invalid input!"); break; } }
static void Main(string[] args) { var data = Console.ReadLine(); while (data != "Beast!") { try { var animal = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); string animalName; int animalAge; string animalGender; if (animal.Length != 3) { throw new ArgumentException("Invalid input!"); } animalName = animal[0]; if (!int.TryParse(animal[1], out animalAge)) { throw new ArgumentException("Invalid input!"); } animalGender = animal[2]; switch (data) { case "Dog": var dog = new Dog(animalName, animalAge, animalGender); Console.WriteLine(dog.ProduceSound()); break; case "Cat": var cat = new Cat(animalName, animalAge, animalGender); Console.WriteLine(cat.ProduceSound()); break; case "Frog": var frog = new Frog(animalName, animalAge, animalGender); Console.WriteLine(frog.ProduceSound()); break; case "Kitten": var kitten = new Kitten(animalName, animalAge); Console.WriteLine(kitten.ProduceSound()); break; case "Tomcat": var tomcat = new Tomcat(animalName, animalAge); Console.WriteLine(tomcat.ProduceSound()); break; default: throw new ArgumentException("Invalid input!"); } } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } data = Console.ReadLine(); } }
public static void Main() { string input; while ((input = Console.ReadLine()) != "Beast!") { try { string[] animalElements = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string name = animalElements[0]; int age = int.Parse(animalElements[1]); string gender = animalElements[2]; Animal animal; if (input.ToLower() == "cat") { animal = new Cat(animalElements[0], age, gender); Console.WriteLine(animal); } else if (input.ToLower() == "tomcat") { animal = new Tomcat(name, age, gender); Console.WriteLine(animal); } else if (input.ToLower() == "kitten") { animal = new Kitten(name, age, gender); Console.WriteLine(animal); } else if (input.ToLower() == "dog") { animal = new Dog(name, age, gender); Console.WriteLine(animal); } else if (input.ToLower() == "frog") { animal = new Frog(name, age, gender); Console.WriteLine(animal); } else { animal = new Animal(name, age, gender); Console.WriteLine(animal); } } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } catch (Exception) { Console.WriteLine("Invalid input!"); } } }
static void Main() { string input; while ((input = Console.ReadLine()) != "Beast!") { string classType = input; var tokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); try { if (tokens.Length < 3) { throw new ArgumentException("Invalid input!"); } string name = tokens[0]; int age; string gender = tokens[2]; if (int.TryParse(tokens[1], out age) == false) { throw new ArgumentException("Invalid input!"); } switch (classType) { case "Cat": { var cat = new Cat(name, age, gender); Console.WriteLine(cat); break; } case "Dog": { var dog = new Dog(name, age, gender); Console.WriteLine(dog); break; } case "Frog": { var frog = new Frog(name, age, gender); Console.WriteLine(frog); break; } case "Kitten": { var kitten = new Kitten(name, age, "Female"); Console.WriteLine(kitten); break; } case "Tomcat": { var tomcat = new Tomcat(name, age, "Male"); Console.WriteLine(tomcat); break; } default: { throw new ArgumentException("Invalid input!"); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }