static void Main(string[] args) { List <Animal> animalCollection = new List <Animal>(); while (true) { try { string firstLine = Console.ReadLine().Trim(); if (firstLine == "Beast!") { break; } string secondLine = Console.ReadLine().Trim(); List <string> animalParameters = secondLine.Split(new char[] { ' ' }).ToList(); Animal newAnimal = null; string name = animalParameters[0]; int age = int.Parse(animalParameters[1]); if (animalParameters.Count == 3) { string gender = animalParameters[2]; newAnimal = AnimalFactory.ProduceAnimal(firstLine, name, age, gender); } else { newAnimal = AnimalFactory.ProduceAnimal(firstLine, name, age); } animalCollection.Add(newAnimal); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } foreach (var element in animalCollection) { Console.WriteLine(element); Console.WriteLine(element.ProduceSound()); } }
public static void Main() { while (true) { var animal = Console.ReadLine(); if (animal == "Beast!") { break; } var tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var name = tokens[0]; int age; var isParsable = int.TryParse(tokens[1], out age); if (!isParsable) { Console.WriteLine("Invalid input!"); } try { if (animal != "Tomcat" && animal != "Kitten") { var gender = tokens[2]; Animal currentAnimal = AnimalFactory.GetAnimal(animal, name, age, gender); Console.WriteLine(currentAnimal.ToString()); } else { Animal currentAnimal = AnimalFactory.GetAnimal(animal, name, age); Console.WriteLine(currentAnimal.ToString()); } } catch (Exception e) { Console.WriteLine(e.Message); } } }
private static void ReadInput() { animals = new List <Animal>(); string inputLine; while ((inputLine = Console.ReadLine()) != "Beast!") { var info = Console.ReadLine(); try { var currentAnimal = AnimalFactory.CreateAnimal(inputLine, info); animals.Add(currentAnimal); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
static void Main(string[] args) { AnimalFactory factory = new AnimalFactory(); List <Animal> animals = new List <Animal>(); while (true) { try { string line = Console.ReadLine(); if (line == "Beast!") { break; } string typeAnimal = line; string[] characteristics = Console.ReadLine().Split(); string name = characteristics[0]; int age = int.Parse(characteristics[1]); string gender = characteristics[2]; Animal animal = factory.GetAnimal(typeAnimal, name, age, gender); animals.Add(animal); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
private static void AddNewAnimal(List <Animal> animals, string type, string name, int age, string gender) { var animal = AnimalFactory.GetAnimal(type, name, age, gender); animals.Add(animal); }