private Animal castAnimal(int speciesIndex, CategoryType type) { //Checking which object to create: if (type == CategoryType.Bird) { BirdSpecies speciestype = (BirdSpecies)speciesIndex; animalis = BirdFactory.CreateBird(speciestype); } else if (type == CategoryType.Insect) { InsectSpecies insectSpec = (InsectSpecies)speciesIndex; animalis = InsectFactory.CreateInsect(insectSpec); } else if (type == CategoryType.Mammal) { MammalSpecies mammalSpec = (MammalSpecies)speciesIndex; animalis = MammalFactory.CreateMammal(mammalSpec); } else if (type == CategoryType.Marine) { MarineSpecies marineSpec = (MarineSpecies)speciesIndex; animalis = MarineFactory.CreateMarine(marineSpec); } else if (type == CategoryType.Reptile) { ReptileSpecies reptileSpec = (ReptileSpecies)speciesIndex; animalis = ReptileFactory.CreateReptile(reptileSpec); } return(animalis); }
public void Run() { string command = ""; while ((command = Console.ReadLine()) != "End") { string[] animalInfo = command.Split(); string[] foodInfo = Console.ReadLine().Split(); string animalType = animalInfo[0].ToLower(); string name = animalInfo[1]; double weight = double.Parse(animalInfo[2]); if (animalType == "cat" || animalType == "tiger") { string livingRegion = animalInfo[3]; string breed = animalInfo[4]; animal = (IAnimal)felineFactory.CreateFeline(animalType, name, weight, livingRegion, breed); } else if (animalType == "owl" || animalType == "hen") { double wingSize = double.Parse(animalInfo[3]); animal = (IAnimal)birdFactory.CreateBird(animalType, name, weight, wingSize); } else if (animalType == "mouse" || animalType == "dog") { string livingRegion = animalInfo[3]; animal = (IAnimal)mammalFactory.CreateMammal(animalType, name, weight, livingRegion); } animals.Add(animal); animal.ProduceSound(); try { string foodType = foodInfo[0]; double quantity = double.Parse(foodInfo[1]); IFood food = foodFactory.CreateFood(foodType, quantity); animal.Eat(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } foreach (var animal in this.animals) { Console.WriteLine(animal); } }
public void Run() { while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] animalInfo = input.Split(); string[] foodInfo = Console.ReadLine().Split(); string animalType = animalInfo[0]; string name = animalInfo[1]; double weight = double.Parse(animalInfo[2]); if (animalType == "Hen" || animalType == "Owl") { double wingSize = double.Parse(animalInfo[3]); animal = this.birdFactory.CreateBird(animalType, name, weight, wingSize); } else if (animalType == "Mouse" || animalType == "Dog") { string livingRegion = animalInfo[3]; animal = mammalFactory.CreateMammal(animalType, name, weight, livingRegion); } else if (animalType == "Cat" || animalType == "Tiger") { string livingRegion = animalInfo[3]; string breed = animalInfo[4]; animal = this.felineFactory.CreateFeline(animalType, name, weight, livingRegion, breed); } string foodType = foodInfo[0]; int quantity = int.Parse(foodInfo[1]); var food = this.foodFactory.CreateFood(foodType, quantity); animal.ProduceSound(); animal.Eat(food); animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void Run() { string input = string.Empty; while ((input = Console.ReadLine()) != "End") { string[] animalInfo = input.Split(); string animalType = animalInfo[0]; string animalName = animalInfo[1]; double animalWeight = double.Parse(animalInfo[2]); if (animalType == "Hen" || animalType == "Owl") { double wingSize = double.Parse(animalInfo[3]); animal = birdFactory.CreateBird(animalType, animalName, animalWeight, wingSize); } else if (animalType == "Dog" || animalType == "Mouse") { string livingregion = animalInfo[3]; animal = mammalFactory.CreateMammal(animalType, animalName, animalWeight, livingregion); } else if (animalType == "Cat" || animalType == "Tiger") { string livingregion = animalInfo[3]; string breed = animalInfo[4]; animal = felineFactory.CreateFeline(animalType, animalName, animalWeight, livingregion, breed); } string[] foodInfo = Console.ReadLine().Split(); string foodType = foodInfo[0]; int foodQuantity = int.Parse(foodInfo[1]); Food food = foodFactory.CreateFood(foodType, foodQuantity); animal.ProduceSound(); animal.Eat(food); animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void Run() { var input = Console.ReadLine(); while (input != "End") { try { var animalArgs = input.Split(); var foodInfo = Console.ReadLine().Split(); var animalType = animalArgs[0]; var name = animalArgs[1]; var weight = double.Parse(animalArgs[2]); if (animalType == "Hen" || animalType == "Owl") { var wingSize = double.Parse(animalArgs[3]); animal = birdFactory.CreateBird(animalType, name, weight, wingSize); } else if (animalType == "Cat" || animalType == "Tiger") { var region = animalArgs[3]; var breed = animalArgs[4]; animal = felinesFactory.CreateFeline(animalType, name, weight, region, breed); } if (animalType == "Dog" || animalType == "Mouse") { var region = animalArgs[3]; animal = mammalFactory.CreateMammal(animalType, name, weight, region); } var foodType = foodInfo[0]; var foodQuantity = int.Parse(foodInfo[1]); var food = foodFactory.CreateFood(foodType, foodQuantity); animal.ProduceSound(); animal.Eat(food); animals.Add(animal); Console.WriteLine(animal); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } input = Console.ReadLine(); } }
public void Run() { string input = Console.ReadLine(); while (input != "End") { string[] animalArguments = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string typeAnimal = animalArguments[0].ToLower(); string name = animalArguments[1]; double weight = double.Parse(animalArguments[2]); if (typeAnimal == "hen" || typeAnimal == "owl") { double wingSize = double.Parse(animalArguments[3]); animal = birdFactory.CreateBird(typeAnimal, name, weight, wingSize); } else if (typeAnimal == "mouse" || typeAnimal == "dog") { string livingRegion = animalArguments[3]; animal = mammalFactory.CreateMammal(typeAnimal, name, weight, livingRegion); } else if (typeAnimal == "cat" || typeAnimal == "tiger") { string livingRegion = animalArguments[3]; string breed = animalArguments[4]; animal = felineFactory.CreateFeline(typeAnimal, name, weight, livingRegion, breed); } animal.ProduceSound(); string[] foodArguments = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); string typeFood = foodArguments[0]; int quantity = int.Parse(foodArguments[1]); var food = foodFactory.CreateFood(typeFood, quantity); animal.Eat(food); this.animals.Add(animal); input = Console.ReadLine(); } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
public void Run() { BirdFactory birdFactory = new BirdFactory(); MammalFactory mammalFactory = new MammalFactory(); FelineFactory felineFactory = new FelineFactory(); FoodFactory foodFactory = new FoodFactory(); List <Animal> animals = new List <Animal>(); while (true) { string[] currentAnimal = Console.ReadLine().Split(); if (currentAnimal[0] == "End") { break; } string[] currentFood = Console.ReadLine().Split(); string animalType = currentAnimal[0]; string animalName = currentAnimal[1]; double animalWeight = double.Parse(currentAnimal[2]); string foodType = currentFood[0]; int foodQuantity = int.Parse(currentFood[1]); Food food = foodFactory.CreateFood(foodType, foodQuantity); try { if (animalType == "Hen" || animalType == "Owl") { double wingSize = double.Parse(currentAnimal[3]); Animal animal = birdFactory.CreateBird(animalType, animalName, animalWeight, wingSize); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } else if (animalType == "Mouse" || animalType == "Dog") { string livingRegion = currentAnimal[3]; Animal animal = mammalFactory.CreateMammal(animalType, animalName, animalWeight, livingRegion); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } else if (animalType == "Cat" || animalType == "Tiger") { string livingRegion = currentAnimal[3]; string breed = currentAnimal[4]; Animal animal = felineFactory.CreateFeline(animalType, animalName, animalWeight, livingRegion, breed); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (Animal animal in animals) { Console.WriteLine(animal); } }