コード例 #1
0
 public override void Feed(Food food)
 {
     if (food.GetType().Name == "Vegetable" || food.GetType().Name == "Meat")
     {
         double totalgain = (double)food.Quantity * WeightGain;
         this.Weight    += totalgain;
         this.FoodEaten += food.Quantity;
     }
     else
     {
         throw new Exception(string.Format(WrongFoodExeption.Exp_Msg, GetType().Name, food.GetType().Name));
     }
 }
コード例 #2
0
 public override void Feed(Food food)
 {
     if (food.GetType().Name == "Vegetable" || food.GetType().Name == "Fruit")
     {
         double totalweightgain = Weight_Gain * (double)food.Quantity;
         this.Weight    += totalweightgain;
         this.FoodEaten += food.Quantity;
     }
     else
     {
         string msg = string.Format($"{WrongFoodExeption.Exp_Msg}", GetType().Name, food.GetType().Name);
         throw new Exception(msg);
     }
 }
コード例 #3
0
ファイル: Animal.cs プロジェクト: svetlinst/Polymorphism
        public virtual void Eat(Food food)
        {
            var foodType = food.GetType().Name;

            if (this.NotEatbleFood.Contains(foodType))
            {
                throw new ArgumentException($"{this.GetType().Name} does not eat {food.GetType().Name}!");
            }
            else
            {
                this.FoodEaten += food.Quantity;
                this.Weight    += food.Quantity * this.WeightModifier;
            }
        }
コード例 #4
0
ファイル: Tiger.cs プロジェクト: TrifShterev/Polymorphism
 protected override void ValidateFood(Food food)
 {
     if (food.GetType().Name != nameof(Meat))
     {
         Throw(food);
     }
 }
コード例 #5
0
 public override void Eat(Food food)
 {
     if (food.GetType().Name == "Meat")
     {
         throw new ArgumentException("Zebras are not eating that type of food!");
     }
     this.FoodEaten += food.Quantity;
 }
コード例 #6
0
 public virtual void Eat(Food food)
 {
     if (!LikesFood)
     {
         throw new Exception($"{GetType().Name} does not eat {food.GetType().Name}!");
     }
     Weight    += WeightGain * food.Quantity;
     FoodEaten += food.Quantity;
 }
コード例 #7
0
ファイル: Cat.cs プロジェクト: TrifShterev/Polymorphism
        protected override void ValidateFood(Food food)
        {
            var type = food.GetType().Name;

            if (type != nameof(Meat) && type != nameof(Vegetable))
            {
                Throw(food);
            }
        }
コード例 #8
0
        protected override void ValidateFood(Food food)
        {
            string type = food.GetType().Name;

            if (type != nameof(Vegetable) && type != nameof(Fruit))
            {
                Throw(food);
            }
        }
コード例 #9
0
        public void Feed(Food food)
        {
            if (!foodsEaten.Contains(food.GetType()))
            {
                throw new InvalidOperationException(string.Format(ExceptionMessages.InvalidFoodType, this.GetType().Name, food.GetType().Name));
            }

            this.Weight += food.Quantity * this.FoodMultiplier;
            FoodEaten   += food.Quantity;
        }
コード例 #10
0
        public void Eat(Food food)
        {
            if (!Eats(food))
            {
                Console.WriteLine($"{GetType().Name} does not eat {food.GetType().Name}!");
                return;
            }

            FoodEaten += food.Quantity;
            Weight    += WeightIncrease * food.Quantity;
        }
コード例 #11
0
 public override void Eat(Food food)
 {
     if (food is Vegetable || food is Fruit)
     {
         base.Eat(food);
         this.Weight += 0.1 * food.Quantity;
     }
     else
     {
         throw new ArgumentException($"Mouse does not eat {food.GetType().Name}!");
     }
 }
コード例 #12
0
        protected void BaseEat(Food food, List <string> eatableFood, double gainValue)
        {
            string typeFood = food.GetType().Name;

            if (!eatableFood.Contains(typeFood))
            {
                throw new ArgumentException($"{this.GetType().Name} does not eat {typeFood}!");
            }

            this.Weight    += food.Quantity * gainValue;
            this.FoodEaten += food.Quantity;
        }
コード例 #13
0
 public override void Eat(Food food)
 {
     if (food is Meat)
     {
         base.Eat(food);
         this.Weight += 0.4 * food.Quantity;
     }
     else
     {
         throw new ArgumentException($"Dog does not eat {food.GetType().Name}!");
     }
 }
コード例 #14
0
 public override void EatFood(Food food)
 {
     if (food is Meat)
     {
         this.Weight    += food.Quantity * foodIncrease;
         this.FoodEaten += food.Quantity;
     }
     else
     {
         throw new ArgumentException($"{GetType().Name} does not eat {food.GetType().Name}!");
     }
 }
コード例 #15
0
 protected void Throw(Food food)
 {
     throw new ArgumentException($"{this.GetType().Name} does not eat {food.GetType().Name}!");
 }
コード例 #16
0
ファイル: Tiger.cs プロジェクト: RositsaRuseva/CSharp
 protected override bool IsFoodValid(Food food) => food.GetType().Name == "Meat" ? true : false;
コード例 #17
0
 protected override bool IsFoodValid(Food food) => food.GetType().Name == "Vegetable" || food.GetType().Name == "Fruit" ? true : false;