public IFoodable CreatFood(string[] animalsArgs) { string foodType = animalsArgs[0]; int quantity = int.Parse(animalsArgs[1]); IFoodable food = null; if (foodType == "Fruit") { food = new Fruit(quantity); } else if (foodType == "Meat") { food = new Meat(quantity); } else if (foodType == "Seeds") { food = new Seeds(quantity); } else if (foodType == "Vegetable") { food = new Vegetable(quantity); } return(food); }
public void Run() { string command; while ((command = reader.ReadLine()) != "End") { string[] animalArgs = command.Split(); animal = animalFactory.CreatAnimal(animalArgs); if (animal != null) { animals.Add(animal); } string[] foodArgs = reader.ReadLine().Split(); food = foodFactory.CreatFood(foodArgs); writer.WriteLine(animal.ProduceSound()); try { animal.FeedTheAnimal(animal, food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } } foreach (var animal in animals) { writer.WriteLine(animal.ToString()); } }
public void FeedTheAnimal(IAnimal animal, IFoodable food) { string animalType = animal.GetType().Name; string foodType = food.GetType().Name; int quantity = food.Quantity; if (IsEatable(animalType, foodType)) { switch (animalType) { case "Hen": Weight += quantity * WeightIncrease.Hen; FoodEaten = quantity; break; case "Owl": Weight += quantity * WeightIncrease.Owl; FoodEaten = quantity; break; case "Mouse": Weight += quantity * WeightIncrease.Mouse; FoodEaten = quantity; break; case "Cat": Weight += quantity * WeightIncrease.Cat; FoodEaten = quantity; break; case "Dog": Weight += quantity * WeightIncrease.Dog; FoodEaten = quantity; break; case "Tiger": Weight += quantity * WeightIncrease.Tiger; FoodEaten = quantity; break; } } else { throw new ArgumentException(string.Format(ExceptionMessages.NotEatableMsg, animalType, foodType)); } }