Пример #1
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string[] animalInput = Console.ReadLine().Split();
            string[] foodInput   = Console.ReadLine().Split();

            while (animalInput[0] != "End")
            {
                string type     = animalInput[0];
                string name     = animalInput[1];
                double weight   = double.Parse(animalInput[2]);
                string foodType = foodInput[0];
                int    quantity = int.Parse(foodInput[1]);

                if (type == nameof(Hen))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird hen = new Hen(name, weight, wingSize);
                    Console.WriteLine(hen.AskForFood());
                    hen.Eat(foodType, quantity);
                    animals.Add(hen);
                }
                else if (type == nameof(Owl))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird owl = new Owl(name, weight, wingSize);
                    Console.WriteLine(owl.AskForFood());
                    owl.Eat(foodType, quantity);
                    animals.Add(owl);
                }
                else if (type == nameof(Cat))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline cat = new Cat(name, weight, livingRegion, breed);
                    Console.WriteLine(cat.AskForFood());
                    cat.Eat(foodType, quantity);
                    animals.Add(cat);
                }
                else if (type == nameof(Tiger))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline tiger = new Tiger(name, weight, livingRegion, breed);
                    Console.WriteLine(tiger.AskForFood());
                    tiger.Eat(foodType, quantity);
                    animals.Add(tiger);
                }
                else if (type == nameof(Mouse))
                {
                    string livingRegion = animalInput[3];

                    Mammal mouse = new Mouse(name, weight, livingRegion);
                    Console.WriteLine(mouse.AskForFood());
                    mouse.Eat(foodType, quantity);
                    animals.Add(mouse);
                }
                else if (type == nameof(Dog))
                {
                    string livingRegion = animalInput[3];

                    Mammal dog = new Dog(name, weight, livingRegion);
                    Console.WriteLine(dog.AskForFood());
                    dog.Eat(foodType, quantity);
                    animals.Add(dog);
                }


                animalInput = Console.ReadLine().Split();
                if (animalInput[0] == "End")
                {
                    break;
                }
                foodInput = Console.ReadLine().Split();
            }

            foreach (var item in animals)
            {
                Console.WriteLine(item.ToString());
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

            while (true)
            {
                string[] animalInput = Console.ReadLine().Split();
                if (animalInput[0] == "End")
                {
                    break;
                }

                string[] foodInput = Console.ReadLine().Split();

                Animal animal = null;
                Food   food   = null;

                string type   = animalInput[0];
                string name   = animalInput[1];
                double weight = double.Parse(animalInput[2]);

                switch (type)
                {
                case "Owl":
                    animal = new Owl(name, weight, double.Parse(animalInput[3]));
                    break;

                case "Hen":
                    animal = new Hen(name, weight, double.Parse(animalInput[3]));
                    break;

                case "Mouse":
                    animal = new Mouse(name, weight, animalInput[3]);
                    break;

                case "Dog":
                    animal = new Dog(name, weight, animalInput[3]);
                    break;

                case "Cat":
                    animal = new Cat(name, weight, animalInput[3], animalInput[4]);
                    break;

                case "Tiger":
                    animal = new Tiger(name, weight, animalInput[3], animalInput[4]);
                    break;

                default:
                    break;
                }

                string foodType     = foodInput[0];
                int    foodQuantity = int.Parse(foodInput[1]);

                switch (foodType)
                {
                case "Fruit":
                    food = new Fruit(foodQuantity);
                    break;

                case "Vegetable":
                    food = new Vegetable(foodQuantity);
                    break;

                case "Meat":
                    food = new Meat(foodQuantity);
                    break;

                case "Seeds":
                    food = new Seeds(foodQuantity);
                    break;

                default:
                    break;
                }

                Console.WriteLine(animal.AskForFood());
                animal.Feed(food);

                animals.Add(animal);
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();
            string        input   = Console.ReadLine();

            while (input.ToLower() != "end")
            {
                var      tokens = input.Split(" ");
                string[] foods  = Console.ReadLine().Split(" ");

                var animalType = tokens[0];
                var foodType   = foods[0];
                int food       = int.Parse(foods[1]);

                switch (animalType)
                {
                case "Cat":
                    Cat cat = new Cat(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    animals.Add(cat);
                    cat.AskForFood();
                    if (foodType == "Vegetable" || foodType == "Meat")
                    {
                        cat.EatFood(food);
                        cat.IncreaseWeight();
                    }
                    else
                    {
                        Console.WriteLine($"Cat does not eat {foodType}!");
                    }
                    break;

                case "Tiger":
                    Tiger tiger = new Tiger(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    animals.Add(tiger);
                    tiger.AskForFood();
                    if (foodType == "Meat")
                    {
                        tiger.EatFood(food);
                        tiger.IncreaseWeight();
                    }
                    else
                    {
                        Console.WriteLine($"Tiger does not eat {foodType}!");
                    }
                    break;

                case "Dog":
                    Dog dog = new Dog(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    animals.Add(dog);
                    dog.AskForFood();
                    if (foodType == "Meat")
                    {
                        dog.EatFood(food);
                        dog.IncreaseWeight();
                    }
                    else
                    {
                        Console.WriteLine($"Dog does not eat {foodType}!");
                    }
                    break;

                case "Mouse":
                    Mouse mouse = new Mouse(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    animals.Add(mouse);
                    mouse.AskForFood();
                    if (foodType == "Vegetable" || foodType == "Fruit")
                    {
                        mouse.EatFood(food);
                        mouse.IncreaseWeight();
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {foodType}!");
                    }
                    break;

                case "Hen":
                    Hen hen = new Hen(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    animals.Add(hen);
                    hen.AskForFood();
                    hen.EatFood(food);
                    hen.IncreaseWeight();
                    break;

                case "Owl":
                    Owl owl = new Owl(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    animals.Add(owl);
                    owl.AskForFood();
                    if (foodType == "Meat")
                    {
                        owl.EatFood(food);
                        owl.IncreaseWeight();
                    }
                    else
                    {
                        Console.WriteLine($"Owl does not eat {foodType}!");
                    }
                    break;
                }

                input = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            var rowCounter = 0;
            var animals    = new List <IAnimal>();

            while (true)
            {
                var input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                if (rowCounter % 2 == 0)
                {
                    var animalInfo   = input.Split();
                    var animalType   = animalInfo[0];
                    var animalName   = animalInfo[1];
                    var animalWeight = decimal.Parse(animalInfo[2]);

                    var foodInfo = Console.ReadLine().Split();
                    var foodName = foodInfo[0];
                    //var vegetable = new Vegetable(4);
                    //var typePath = vegetable.GetType().FullName;
                    //var foodType = Type.GetType(typePath);
                    var foodType     = Type.GetType($"WildFarm.Foods.{foodName}");
                    var foodQuantity = int.Parse(foodInfo[1]);
                    var food         = Activator.CreateInstance(foodType, new Object[] { foodQuantity });
                    //var food = new Vegetable(int.Parse(foodInfo[1]));


                    if (animalType == "Tiger")
                    {
                        var livingRegion = animalInfo[3];
                        var breed        = animalInfo[4];
                        var tiger        = new Tiger(animalName, animalWeight, livingRegion, breed);
                        Console.WriteLine(tiger.AskForFood());
                        TryEatFood(tiger, (IFood)food);
                        animals.Add(tiger);
                    }
                    else if (animalType == "Cat")
                    {
                        var livingRegion = animalInfo[3];
                        var breed        = animalInfo[4];
                        var cat          = new Cat(animalName, animalWeight, livingRegion, breed);
                        Console.WriteLine(cat.AskForFood());
                        TryEatFood(cat, (IFood)food);
                        animals.Add(cat);
                    }
                    else if (animalType == "Owl")
                    {
                        var wingSize = decimal.Parse(animalInfo[3]);
                        var owl      = new Owl(animalName, animalWeight, wingSize);
                        Console.WriteLine(owl.AskForFood());
                        TryEatFood(owl, (IFood)food);
                        animals.Add(owl);
                    }
                    else if (animalType == "Hen")
                    {
                        var wingSize = decimal.Parse(animalInfo[3]);
                        var hen      = new Hen(animalName, animalWeight, wingSize);
                        Console.WriteLine(hen.AskForFood());
                        TryEatFood(hen, (IFood)food);
                        animals.Add(hen);
                    }
                    else if (animalType == "Mouse")
                    {
                        var livingRegion = animalInfo[3];
                        var mouse        = new Mouse(animalName, animalWeight, livingRegion);
                        Console.WriteLine(mouse.AskForFood());
                        TryEatFood(mouse, (IFood)food);
                        animals.Add(mouse);
                    }
                    else if (animalType == "Dog")
                    {
                        var livingRegion = animalInfo[3];
                        var dog          = new Dog(animalName, animalWeight, livingRegion);
                        Console.WriteLine(dog.AskForFood());
                        TryEatFood(dog, (IFood)food);
                        animals.Add(dog);
                    }
                }

                rowCounter += 2;
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }