示例#1
0
        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);
        }
示例#2
0
        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());
            }
        }
示例#3
0
        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));
            }
        }