Esempio n. 1
0
        private static Animal CreateAnimal(string[] animalParts)
        {
            string type   = animalParts[0];
            string name   = animalParts[1];
            double weight = double.Parse(animalParts[2]);
            Animal animal = null;

            if (type == nameof(Owl))
            {
                double wingSize = double.Parse(animalParts[3]);
                animal = new Owl(name, weight, wingSize);
            }
            else if (type == nameof(Hen))
            {
                double wingSize = double.Parse(animalParts[3]);
                animal = new Hen(name, weight, wingSize);
            }
            else if (type == nameof(Cat))
            {
                string livingRegion = animalParts[3];
                string breed        = animalParts[4];
                animal = new Cat(name, weight, livingRegion, breed);
            }
            else if (type == nameof(Tiger))
            {
                string livingRegion = animalParts[3];
                string breed        = animalParts[4];
                animal = new Tiger(name, weight, livingRegion, breed);
            }
            else if (type == nameof(Dog))
            {
                string livingRegion = animalParts[3];
                animal = new Dog(name, weight, livingRegion);
            }
            else if (type == nameof(Mouse))
            {
                string livingRegion = animalParts[3];
                animal = new Mouse(name, weight, livingRegion);
            }
            return(animal);
        }
Esempio n. 2
0
        public static Animal CreateAnimal(string[] animalInfo)
        {
            Animal animal;

            switch (animalInfo[0])
            {
            case "Cat":
                animal = new Cat(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3], animalInfo[4]);
                break;

            case "Tiger":
                animal = new Tiger(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3], animalInfo[4]);
                break;

            case "Owl":
                animal = new Owl(animalInfo[1], double.Parse(animalInfo[2]), double.Parse(animalInfo[3]));
                break;

            case "Hen":
                animal = new Hen(animalInfo[1], double.Parse(animalInfo[2]), double.Parse(animalInfo[3]));
                break;

            case "Mouse":
                animal = new Mouse(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3]);
                break;

            case "Dog":
                animal = new Dog(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3]);
                break;

            default:
                throw new ArgumentException("Invalid animal type!");
            }

            return(animal);
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        public static void Main()
        {
            string        command = string.Empty;
            List <Animal> animals = new List <Animal>();

            while ((command = Console.ReadLine()) != "End")
            {
                //TODO: try catch exeptions
                string[] args   = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   type   = args[0];
                string   name   = args[1];
                double   weight = double.Parse(args[2]);

                string[] foodInput   = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   foodType    = foodInput[0];
                int      foodQuanity = int.Parse(foodInput[1]);

                //TODO: if food is null
                Food food = null;
                switch (foodType)
                {
                case "Vegetable":
                    food = new Vegetable(foodQuanity);
                    break;

                case "Fruit":
                    food = new Fruit(foodQuanity);
                    break;

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

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

                default:
                    break;
                }

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

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

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

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

                case "Cat":
                    livingRegion = args[3];
                    string breed = args[4];
                    animal = new Cat(name, weight, livingRegion, breed);
                    break;

                case "Tiger":
                    livingRegion = args[3];
                    breed        = args[4];
                    animal       = new Tiger(name, weight, livingRegion, breed);
                    break;
                }

                animal.Eat(food);
                Console.WriteLine();
                animals.Add(animal);
            }
        }
Esempio n. 5
0
        public void Run()
        {
            while (true)
            {
                string input = Console.ReadLine();

                if (input == "End")
                {
                    break;
                }

                string[] inputArgs = input
                    .Split();

                string animalType = inputArgs[0];
                string name = inputArgs[1];
                double weight = double.Parse(inputArgs[2]);

                IAnimal currentAnimal = null;

                if(animalType == "Owl")
                {
                    double wingSize = double.Parse(inputArgs[3]);
                    currentAnimal = new Owl(name, weight, wingSize);
                }
                else if(animalType == "Hen")
                {
                    double wingSize = double.Parse(inputArgs[3]);
                    currentAnimal = new Hen(name, weight, wingSize);
                }
                else if(animalType == "Mouse")
                {
                    string livingRegion = inputArgs[3];
                    currentAnimal = new Mouse(name, weight, livingRegion);
                }
                else if(animalType == "Dog")
                {
                    string livingRegion = inputArgs[3];
                    currentAnimal = new Dog(name, weight, livingRegion);
                }
                else if(animalType == "Cat")
                {
                    string livingRegion = inputArgs[3];
                    string breed = inputArgs[4];
                    currentAnimal = new Cat(name, weight, livingRegion, breed);
                }
                else if(animalType == "Tiger")
                {
                    string livingRegion = inputArgs[3];
                    string breed = inputArgs[4];
                    currentAnimal = new Tiger(name, weight, livingRegion, breed);
                }

                animals.Add(currentAnimal);

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

                string foodType = foodArgs[0];
                int quantity = int.Parse(foodArgs[1]);

                Console.WriteLine(currentAnimal.ProduceSound());

                if (!currentAnimal.CurrentTypeEatFood.Any(x => x == Enum.Parse<TypeFood>(foodType)))
                {
                    Console.WriteLine($"{currentAnimal.GetType().Name} does not eat {foodType}!");
                }
                else
                {
                    currentAnimal.AddWeight(quantity);
                }
            }

            foreach (var item in animals)
            {
                Console.WriteLine(item);
            }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

            while (true)
            {
                var command = Console.ReadLine();

                if (command == "End")
                {
                    break;
                }

                var animalInfo = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                var foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                var foodType = foodInfo[0];
                var quantity = int.Parse(foodInfo[1]);

                if (animalInfo[0] == "Owl")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Owl(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Hen")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Hen(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Mouse")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Mouse(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Dog")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Dog(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Cat")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Cat(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Tiger")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Tiger(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string command = Console.ReadLine();

                if (command.Equals("End"))
                {
                    break;
                }

                string[] inputInfo = command.Split().ToArray();

                string animalType = inputInfo[0];
                string name       = inputInfo[1];
                double weight     = double.Parse(inputInfo[2]);

                string[] food     = Console.ReadLine().Split().ToArray();
                string   foodType = food[0];
                int      quantity = int.Parse(food[1]);

                IFood  currentFood = null;
                Animal animal      = null;

                if (foodType.Equals("Vegetable"))
                {
                    currentFood = new Vegetable(quantity);
                }
                else if (foodType.Equals("Fruit"))
                {
                    currentFood = new Fruit(quantity);
                }
                else if (foodType.Equals("Meat"))
                {
                    currentFood = new Meat(quantity);
                }
                else if (foodType.Equals("Seeds"))
                {
                    currentFood = new Seeds(quantity);
                }


                if (animalType.Equals("Hen"))
                {
                    double wingSize = double.Parse(inputInfo[3]);

                    animal = new Hen(name, weight, wingSize);
                }
                else if (animalType.Equals("Owl"))
                {
                    double wingSize = double.Parse(inputInfo[3]);

                    animal = new Owl(name, weight, wingSize);
                }
                else if (animalType.Equals("Mouse"))
                {
                    string livingRegion = inputInfo[3];

                    animal = new Mouse(name, weight, livingRegion);
                }
                else if (animalType.Equals("Dog"))
                {
                    string livingRegion = inputInfo[3];

                    animal = new Dog(name, weight, livingRegion);
                }
                else if (animalType.Equals("Cat"))
                {
                    string livingRegion = inputInfo[3];
                    string breed        = inputInfo[4];

                    animal = new Cat(name, weight, livingRegion, breed);
                }
                else if (animalType.Equals("Tiger"))
                {
                    string livingRegion = inputInfo[3];
                    string breed        = inputInfo[4];

                    animal = new Tiger(name, weight, livingRegion, breed);
                }

                animals.Add(animal);

                animal.ProduceSound();
                try
                {
                    animal.Eating(currentFood);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            string         input   = String.Empty;
            List <IAnimal> animals = new List <IAnimal>();

            while ((input = Console.ReadLine()) != "End")
            {
                var inputTokens = input.Split();


                //ParseAnimal start
                var     animalType   = inputTokens[0];
                var     animalName   = inputTokens[1];
                var     animalWeight = double.Parse(inputTokens[2]);
                double  wingSize     = 0;
                string  livingRegion = String.Empty;
                string  breed        = String.Empty;
                IAnimal animal       = null;

                switch (animalType)
                {
                case "Owl":
                    wingSize = double.Parse(inputTokens[3]);
                    animal   = new Owl(animalName, animalWeight, wingSize);
                    break;

                case "Hen":
                    wingSize = double.Parse(inputTokens[3]);
                    animal   = new Hen(animalName, animalWeight, wingSize);
                    break;

                case "Mouse":
                    livingRegion = inputTokens[3];
                    animal       = new Mouse(animalName, animalWeight, livingRegion);
                    break;

                case "Dog":
                    livingRegion = inputTokens[3];
                    animal       = new Dog(animalName, animalWeight, livingRegion);
                    break;

                case "Cat":
                    livingRegion = inputTokens[3];
                    breed        = inputTokens[4];
                    animal       = new Cat(animalName, animalWeight, livingRegion, breed);
                    break;

                case "Tiger":
                    livingRegion = inputTokens[3];
                    breed        = inputTokens[4];
                    animal       = new Tiger(animalName, animalWeight, livingRegion, breed);
                    break;
                }
                animals.Add(animal);
                //ParseAnimal end

                //ParseFood start
                var foodTokens   = Console.ReadLine().Split();
                var foodType     = foodTokens[0];
                var foodQuantity = int.Parse(foodTokens[1]);

                animal.ProduceSound();
                animal.Eat(animal, foodType, foodQuantity);

                //ParseFood end
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }
                string[] dataAnimal = input.Split();
                string[] dataFood   = Console.ReadLine().Split();
                string   type       = dataAnimal[0];
                string   name       = dataAnimal[1];
                double   weight     = double.Parse(dataAnimal[2]);
                Animal   animal     = null;
                if (type == nameof(Owl))
                {
                    animal = new Owl(name, weight, double.Parse(dataAnimal[3]));
                }
                else if (type == nameof(Hen))
                {
                    animal = new Hen(name, weight, double.Parse(dataAnimal[3]));
                }
                else if (type == nameof(Mouse))
                {
                    animal = new Mouse(name, weight, dataAnimal[3]);
                }
                else if (type == nameof(Dog))
                {
                    animal = new Dog(name, weight, dataAnimal[3]);
                }
                else if (type == nameof(Cat))
                {
                    animal = new Cat(name, weight, dataAnimal[3], dataAnimal[4]);
                }
                else if (type == nameof(Tiger))
                {
                    animal = new Tiger(name, weight, dataAnimal[3], dataAnimal[4]);
                }
                string foodType = dataFood[0];
                int    quantity = int.Parse(dataFood[1]);
                Food   food     = null;
                if (foodType == nameof(Meat))
                {
                    food = new Meat(quantity);
                }
                else if (foodType == nameof(Vegetable))
                {
                    food = new Vegetable(quantity);
                }
                else if (foodType == nameof(Fruit))
                {
                    food = new Fruit(quantity);
                }
                else if (foodType == nameof(Seeds))
                {
                    food = new Seeds(quantity);
                }
                Console.WriteLine(animal.ProduceSound());
                animal.EatingFood(food);
                animals.Add(animal);
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 10
0
        public static void Main()
        {
            var result = new StringBuilder();

            var animalList = new List <Animal>();

            var typeOfAnimals = new Dictionary <string, List <string> >();

            AddTypes(typeOfAnimals);

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

                string[] animalToken = input.Split(' ');

                string type = animalToken[0];

                input = Console.ReadLine();

                string[] token = input.Split(' ');

                string typeOfFood = token[0];
                int    quantity   = int.Parse(token[1]);

                try
                {
                    // Birds
                    if (typeOfAnimals["Bird"].Contains(type))
                    {
                        string name         = animalToken[1];
                        double animalWeight = double.Parse(animalToken[2]);
                        double wingSize     = double.Parse(animalToken[3]);

                        switch (type)
                        {
                        case "Owl":
                            Owl owl = new Owl(name, animalWeight, wingSize);
                            result.AppendLine(owl.ProduceSound());
                            animalList.Add(owl);
                            owl.Eat(typeOfFood, quantity);
                            break;

                        case "Hen":
                            Hen hen = new Hen(name, animalWeight, wingSize);
                            result.AppendLine(hen.ProduceSound());
                            animalList.Add(hen);
                            hen.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Mammals
                    if (typeOfAnimals["Mammal"].Contains(type))
                    {
                        string mammalName         = animalToken[1];
                        double mammalAnimalWeight = double.Parse(animalToken[2]);
                        string mammalLivingRegion = animalToken[3];

                        switch (type)
                        {
                        case "Mouse":
                            Mouse mouse = new Mouse(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            animalList.Add(mouse);
                            result.AppendLine(mouse.ProduceSound());
                            mouse.Eat(typeOfFood, quantity);
                            break;

                        case "Dog":
                            Dog dog = new Dog(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            result.AppendLine(dog.ProduceSound());
                            animalList.Add(dog);
                            dog.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Feline
                    if (typeOfAnimals["Feline"].Contains(type))
                    {
                        string felineName         = animalToken[1];
                        double felineWeight       = double.Parse(animalToken[2]);
                        string felineLivingRegion = animalToken[3];
                        string felineBreed        = animalToken[4];

                        switch (type)
                        {
                        case "Cat":
                            Cat cat = new Cat(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(cat);
                            result.AppendLine(cat.ProduceSound());
                            cat.Eat(typeOfFood, quantity);
                            break;

                        case "Tiger":
                            Tiger tiger = new Tiger(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(tiger);
                            result.AppendLine(tiger.ProduceSound());
                            tiger.Eat(typeOfFood, quantity);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    result.AppendLine(ex.Message);
                }
            }

            Console.Write(result.ToString());

            foreach (var animal in animalList)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string[] animalArgs = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   type       = animalArgs[0];

                if (type == "End")
                {
                    break;
                }
                string name   = animalArgs[1];
                double weight = double.Parse(animalArgs[2]);

                string[] foodArgs     = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   food         = foodArgs[0];
                int      foodQuantity = int.Parse(foodArgs[1]);

                if (animalArgs.Length == 5)
                {
                    string livingRegion = animalArgs[3];
                    string breed        = animalArgs[4];
                    if (type == "Cat")
                    {
                        Cat cat = new Cat(name, weight, livingRegion, breed);
                        Console.WriteLine(cat.ProduceSound());
                        animals.Add(cat);
                        try
                        {
                            cat.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else if (type == "Tiger")
                    {
                        Tiger tiger = new Tiger(name, weight, livingRegion, breed);
                        Console.WriteLine(tiger.ProduceSound());
                        animals.Add(tiger);
                        try
                        {
                            tiger.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                else if (double.TryParse(animalArgs[3], out double wingsSize))
                {
                    if (type == "Hen")
                    {
                        Hen hen = new Hen(name, weight, wingsSize);
                        Console.WriteLine(hen.ProduceSound());
                        animals.Add(hen);
                        try
                        {
                            hen.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else if (type == "Owl")
                    {
                        Owl owl = new Owl(name, weight, wingsSize);
                        Console.WriteLine(owl.ProduceSound());
                        animals.Add(owl);
                        try
                        {
                            owl.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                else
                {
                    string livingRegion = animalArgs[3];
                    if (type == "Dog")
                    {
                        Dog dog = new Dog(name, weight, livingRegion);
                        Console.WriteLine(dog.ProduceSound());
                        animals.Add(dog);
                        try
                        {
                            dog.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else if (type == "Mouse")
                    {
                        Mouse mouse = new Mouse(name, weight, livingRegion);
                        Console.WriteLine(mouse.ProduceSound());
                        animals.Add(mouse);
                        try
                        {
                            mouse.FeedAnimal(ReturnFood(food, foodQuantity));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Esempio n. 12
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());
            }
        }
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();
            List <Food>   foods   = new List <Food>();

            int lineCounter = -1;

            while (true)
            {
                lineCounter++;//N.B. += 1
                string cmd = Console.ReadLine();
                if (cmd == "End")
                {
                    break;
                }

                string[] cmdArgs = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                switch (cmdArgs[0])
                {
                case "Owl":
                    var owl = new Owl(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3]));
                    animals.Add(owl);
                    break;

                case "Hen":
                    var hen = new Hen(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3]));
                    animals.Add(hen);
                    break;

                case "Mouse":
                    var mouse = new Mouse(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]);
                    animals.Add(mouse);
                    break;

                case "Dog":
                    var dog = new Dog(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]);
                    animals.Add(dog);
                    break;

                case "Cat":
                    var cat = new Cat(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]);
                    animals.Add(cat);
                    break;

                case "Tiger":
                    var tiger = new Tiger(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]);
                    animals.Add(tiger);
                    break;

                case "Vegetable":
                    var vegetable = new Vegetable(int.Parse(cmdArgs[1]));
                    foods.Add(vegetable);
                    break;

                case "Fruit":
                    var fruit = new Fruit(int.Parse(cmdArgs[1]));
                    foods.Add(fruit);
                    break;

                case "Meat":
                    var meat = new Meat(int.Parse(cmdArgs[1]));
                    foods.Add(meat);
                    break;

                case "Seeds":
                    var seeds = new Seeds(int.Parse(cmdArgs[1]));
                    foods.Add(seeds);
                    break;
                }
            }

            for (int i = 0; i < animals.Count; i++)
            {
                try
                {
                    Console.WriteLine(animals[i].AskForFood());
                    animals[i].Eat(foods[i]);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            animals.ForEach(Console.WriteLine);
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

            while (true)
            {
                string cmd = Console.ReadLine();

                if (cmd == "End")
                {
                    break;
                }

                var    data       = cmd.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string animalType = data[0];
                string name       = data[1];
                double weight     = double.Parse(data[2]);

                Animal animal = null;

                switch (animalType)
                {
                case "Owl":
                {
                    int wingSize = int.Parse(data[3]);

                    animal = new Owl(name, weight, wingSize);
                    break;
                }

                case "Hen":
                {
                    int wingSize = int.Parse(data[3]);

                    animal = new Hen(name, weight, wingSize);
                    break;
                }

                case "Mouse":
                {
                    string region = data[3];

                    animal = new Mouse(name, weight, region);
                    break;
                }

                case "Dog":
                {
                    string region = data[3];

                    animal = new Dog(name, weight, region);
                    break;
                }

                case "Cat":
                {
                    string region = data[3];
                    string breed  = data[4];

                    animal = new Cat(name, weight, region, breed);
                    break;
                }

                case "Tiger":
                {
                    string region = data[3];
                    string breed  = data[4];

                    animal = new Tiger(name, weight, region, breed);
                    break;
                }

                default:
                    break;
                }

                Console.WriteLine(animal.ProduceSound());

                var foodData = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                string foodName = foodData[0];
                int    foodNum  = int.Parse(foodData[1]);

                Food food = null;

                switch (foodName)
                {
                case "Fruit":
                {
                    food = new Fruit(foodNum);

                    break;
                }

                case "Meat":
                {
                    food = new Meat(foodNum);
                    break;
                }

                case "Seeds":
                {
                    food = new Seeds(foodNum);
                    break;
                }

                case "Vegetable":
                {
                    food = new Vegetable(foodNum);
                    break;
                }

                default:
                    break;
                }

                animal.Feed(food);
                animals.Add(animal);
            }

            foreach (var beast in animals)
            {
                Console.WriteLine(beast.ToString());
            }
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }
                string[] currentAnimal = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string[] currentFood   = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                try
                {
                    Animal animal       = null;
                    string typeOfAnimal = currentAnimal[0];
                    string animalName   = currentAnimal[1];
                    double animalWeight = double.Parse(currentAnimal[2]);
                    double wingSize;
                    string livingRegion;
                    string breed;
                    switch (typeOfAnimal)
                    {
                    case "Hen":
                        wingSize = double.Parse(currentAnimal[3]);
                        animal   = new Hen(animalName, animalWeight, wingSize);
                        break;

                    case "Owl":
                        wingSize = double.Parse(currentAnimal[3]);
                        animal   = new Owl(animalName, animalWeight, wingSize);
                        break;

                    case "Dog":
                        livingRegion = currentAnimal[3];
                        animal       = new Dog(animalName, animalWeight, livingRegion);
                        break;

                    case "Mouse":
                        livingRegion = currentAnimal[3];
                        animal       = new Mouse(animalName, animalWeight, livingRegion);
                        break;

                    case "Cat":
                        livingRegion = currentAnimal[3];
                        breed        = currentAnimal[4];
                        animal       = new Cat(animalName, animalWeight, livingRegion, breed);
                        break;

                    case "Tiger":
                        livingRegion = currentAnimal[3];
                        breed        = currentAnimal[4];
                        animal       = new Tiger(animalName, animalWeight, livingRegion, breed);
                        break;
                    }
                    animals.Add(animal);

                    Food   food         = null;
                    string typeOfFood   = currentFood[0];
                    int    foodQuantity = int.Parse(currentFood[1]);
                    switch (typeOfFood)
                    {
                    case "Vegetable":
                        food = new Vegetable(foodQuantity);
                        break;

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

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

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

                    default:
                        break;
                    }

                    Console.WriteLine(animal.ProduceSound());
                    animal.ValidateFood(food);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            Queue <Animal> animals     = new Queue <Animal>();
            List <Animal>  listAnimals = new List <Animal>();

            var input = Console.ReadLine().Split(" ").ToArray();

            while (input[0] != "End")
            {
                if (input.Length > 2)
                {
                    switch (input[0])
                    {
                    case "Hen":
                    {
                        Hen hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3]));
                        hen.WantFood();

                        animals.Enqueue(hen);
                        break;
                    }

                    case "Owl":
                    {
                        Owl owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3]));
                        owl.WantFood();

                        animals.Enqueue(owl);
                        break;
                    }

                    case "Mouse":
                    {
                        Mouse mouse = new Mouse(input[1], double.Parse(input[2]), input[3]);
                        mouse.WantFood();

                        animals.Enqueue(mouse);
                        break;
                    }

                    case "Cat":
                    {
                        Cat cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]);
                        cat.WantFood();

                        animals.Enqueue(cat);
                        break;
                    }

                    case "Dog":
                    {
                        Dog dog = new Dog(input[1], double.Parse(input[2]), input[3]);
                        dog.WantFood();

                        animals.Enqueue(dog);
                        break;
                    }

                    case "Tiger":
                    {
                        Tiger tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]);
                        tiger.WantFood();

                        animals.Enqueue(tiger);
                        break;
                    }
                    }
                }
                else
                {
                    switch (input[0])
                    {
                    case "Vegetable":
                    {
                        Vegetable vegetable = new Vegetable(int.Parse(input[1]));

                        var animal = animals.Dequeue();
                        listAnimals.Add(animal);
                        animal.Eat(vegetable);
                        break;
                    }

                    case "Fruit":
                    {
                        Fruit fruit = new Fruit(int.Parse(input[1]));

                        var animal = animals.Dequeue();
                        listAnimals.Add(animal);
                        animal.Eat(fruit);
                        break;
                    }

                    case "Meat":
                    {
                        Meat meat = new Meat(int.Parse(input[1]));

                        var animal = animals.Dequeue();
                        listAnimals.Add(animal);
                        animal.Eat(meat);
                        break;
                    }

                    case "Seeds":
                    {
                        Seeds seeds = new Seeds(int.Parse(input[1]));

                        var animal = animals.Dequeue();
                        listAnimals.Add(animal);
                        animal.Eat(seeds);
                        break;
                    }
                    }
                }

                input = Console.ReadLine().Split(" ").ToArray();
            }

            foreach (var animal in listAnimals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Esempio n. 17
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());
            }
        }
Esempio n. 18
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();
            List <Food>   foods   = new List <Food>();

            int lineNum = 0;

            while (true)
            {
                string line = Console.ReadLine();
                if (line == "End")
                {
                    break;
                }

                Animal animal = null;
                Food   food   = null;
                if (lineNum % 2 == 0)
                {
                    string[] animalArgs = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    lineNum++;

                    string type   = animalArgs[0];
                    string name   = animalArgs[1];
                    double weight = double.Parse(animalArgs[2]);
                    switch (type)
                    {
                    // {LivingRegion} {Breed}
                    case "Cat":
                        string livingRegion = animalArgs[3];
                        string breed        = animalArgs[4];

                        animal = new Cat(name, weight, livingRegion, breed);
                        break;

                    case "Tiger":
                        livingRegion = animalArgs[3];
                        breed        = animalArgs[4];

                        animal = new Tiger(name, weight, livingRegion, breed);
                        break;

                    case "Owl":
                        double wingSize = double.Parse(animalArgs[3]);

                        animal = new Owl(name, weight, wingSize);
                        break;

                    case "Hen":
                        wingSize = double.Parse(animalArgs[3]);

                        animal = new Hen(name, weight, wingSize);
                        break;

                    case "Mouse":
                        livingRegion = animalArgs[3];

                        animal = new Mouse(name, weight, livingRegion);
                        break;

                    case "Dog":
                        livingRegion = animalArgs[3];

                        animal = new Dog(name, weight, livingRegion);
                        break;
                    }

                    animals.Add(animal);
                }
                else
                {
                    string[] foodArgs = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    lineNum++;

                    string foodType = foodArgs[0];
                    int    quantity = int.Parse(foodArgs[1]);
                    switch (foodType)
                    {
                    case "Vegetable":
                        food = new Vegetable(quantity);
                        break;

                    case "Fruit":
                        food = new Fruit(quantity);
                        break;

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

                    case "Seeds":
                        food = new Seeds(quantity);
                        break;
                    }

                    foods.Add(food);
                }
            }

            for (int i = 0; i < animals.Count; i++)
            {
                try
                {
                    Console.WriteLine(animals[i].AskForFood());
                    animals[i].Eat(foods[i]);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            animals.ForEach(Console.WriteLine);
        }
Esempio n. 19
0
        static void Main(string[] args)
        {
            int    counter = 0;
            string line    = string.Empty;

            List <Animal> animals = new List <Animal>();

            while ((line = Console.ReadLine()) != "End")
            {
                var information = line.Split(" ");

                counter++;

                if (counter % 2 != 0)
                {
                    string type   = information[0];
                    string name   = information[1];
                    double weight = double.Parse(information[2]);

                    Animal animal = null;

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

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

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

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

                    case "Cat":
                        string livingRegions = information[3];
                        string breed         = information[4];
                        animal = new Cat(name, weight, livingRegions, breed);
                        break;

                    case "Tiger":
                        string livingRegionse = information[3];
                        string breeds         = information[4];
                        animal = new Tiger(name, weight, livingRegionse, breeds);
                        break;
                    }

                    animals.Add(animal);
                }
                else
                {
                    string foodName = information[0];
                    int    quantity = int.Parse(information[1]);

                    Food food = null;

                    switch (foodName)
                    {
                    case "Vegetable": food = new Vegetable(quantity); break;

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

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

                    case "Fruit": food = new Fruit(quantity); break;
                    }

                    Animal currentAnimal = animals[(counter / 2) - 1];
                    Console.WriteLine(currentAnimal.AskFood());

                    try
                    {
                        currentAnimal.Feed(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 20
0
        static void Main(string[] args)
        {
            string input       = string.Empty;
            int    counter     = 0;
            var    listAnimals = new Stack <Animal>();
            var    listFood    = new Stack <Food>();

            while ((input = Console.ReadLine()) != "End")
            {
                if (counter % 2 == 0)
                {
                    var tokens = input
                                 .Split()
                                 .ToArray();

                    string animalType   = tokens[0];
                    string animalName   = tokens[1];
                    double animalWeight = double.Parse(tokens[2]);

                    if (animalType == "Owl")
                    {
                        double wingSize = double.Parse(tokens[3]);
                        Owl    owl      = new Owl(animalName, animalWeight, wingSize);
                        owl.Sound();
                        listAnimals.Push(owl);
                    }
                    else if (animalType == "Hen")
                    {
                        double wingSize = double.Parse(tokens[3]);
                        Hen    hen      = new Hen(animalName, animalWeight, wingSize);
                        hen.Sound();
                        listAnimals.Push(hen);
                    }
                    else if (animalType == "Mouse")
                    {
                        string livingRegion = tokens[3];
                        Mouse  mouse        = new Mouse(animalName, animalWeight, livingRegion);
                        mouse.Sound();
                        listAnimals.Push(mouse);
                    }
                    else if (animalType == "Dog")
                    {
                        string livingRegion = tokens[3];
                        Dog    dog          = new Dog(animalName, animalWeight, livingRegion);
                        dog.Sound();
                        listAnimals.Push(dog);
                    }
                    else if (animalType == "Cat")
                    {
                        string livingRegion = tokens[3];
                        string breed        = tokens[4];
                        Cat    cat          = new Cat(animalName, animalWeight, livingRegion, breed);
                        cat.Sound();
                        listAnimals.Push(cat);
                    }
                    else if (animalType == "Tiger")
                    {
                        string livingRegion = tokens[3];
                        string breed        = tokens[4];
                        Tiger  tiger        = new Tiger(animalName, animalWeight, livingRegion, breed);
                        tiger.Sound();
                        listAnimals.Push(tiger);
                    }
                }
                else if (counter % 2 != 0)
                {
                    var token = input
                                .Split()
                                .ToArray();

                    string foodType = token[0];
                    int    quantity = int.Parse(token[1]);

                    if (foodType == "Vegetable")
                    {
                        Vegetable vegetable = new Vegetable(quantity);
                        listFood.Push(vegetable);
                    }
                    else if (foodType == "Fruit")
                    {
                        Fruit fruit = new Fruit(quantity);
                        listFood.Push(fruit);
                    }
                    else if (foodType == "Meat")
                    {
                        Meat meat = new Meat(quantity);
                        listFood.Push(meat);
                    }
                    else if (foodType == "Seeds")
                    {
                        Seeds seeds = new Seeds(quantity);
                        listFood.Push(seeds);
                    }
                }

                if (counter % 2 != 0)
                {
                    listAnimals.Peek().Eat(listFood.Peek());
                }

                counter++;
            }

            while (listAnimals.Count > 0)
            {
                Console.WriteLine(listAnimals.Pop().ToString());
            }
        }
Esempio n. 21
0
        public void Run()
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                string[] tokens     = input.Split();
                string   typeAnimal = tokens[0];

                if (typeAnimal == "Cat")
                {
                    Animal   cat      = new Cat(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    cat.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        cat.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Cat does not eat {typeFood}");
                    }
                    animals.Add(cat);
                }
                else if (typeAnimal == "Dog")
                {
                    Animal   dog      = new Dog(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    dog.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        dog.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Dog does not eat {typeFood}");
                    }
                    animals.Add(dog);
                }
                else if (typeAnimal == "Tiger")
                {
                    Animal   tiger    = new Tiger(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    tiger.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        tiger.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Tiger does not eat {typeFood}");
                    }
                    animals.Add(tiger);
                }
                else if (typeAnimal == "Mouse")
                {
                    Animal   mouse    = new Mouse(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    mouse.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        mouse.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(mouse);
                }
                else if (typeAnimal == "Owl")
                {
                    Animal   owl      = new Owl(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    owl.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        owl.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(owl);
                }
                else if (typeAnimal == "Hen")
                {
                    Animal   hen      = new Hen(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    hen.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        hen.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(hen);
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 22
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);
            }
        }
Esempio n. 23
0
        static void Main(string[] args)
        {
            var listAnimals = new List <Animal>();
            var animalInput = Console.ReadLine().Split();

            while (animalInput[0] != "End")
            {
                var type      = animalInput[0];
                var name      = animalInput[1];
                var weight    = double.Parse(animalInput[2]);
                var food      = Console.ReadLine().Split();
                var foodType  = food[0];
                var foodCount = int.Parse(food[1]);
                if (animalInput.Count() == 4)
                {
                    if (double.TryParse(animalInput[3], out double wingSize))
                    {
                        if (type == "Hen")
                        {
                            var hen = new Hen(name, weight, wingSize);
                            Console.WriteLine(hen.MakeSound());
                            hen.EatFood(foodCount, hen.Multiplier);
                            listAnimals.Add(hen);
                        }
                        else
                        {
                            var owl = new Owl(name, weight, wingSize);
                            Console.WriteLine(owl.MakeSound());
                            if (foodType == "Meat")
                            {
                                owl.EatFood(foodCount, owl.Multiplier);
                            }
                            else
                            {
                                Console.WriteLine(owl.AnimalDoesntEat(foodType));
                            }
                            listAnimals.Add(owl);
                        }
                    }
                    else
                    {
                        var region = animalInput[3];
                        if (type == "Dog")
                        {
                            var dog = new Dog(name, weight, region);
                            Console.WriteLine(dog.MakeSound());
                            if (foodType == "Meat")
                            {
                                dog.EatFood(foodCount, dog.Multiplier);
                            }
                            else
                            {
                                Console.WriteLine(dog.AnimalDoesntEat(foodType));
                            }
                            listAnimals.Add(dog);
                        }
                        else
                        {
                            var mouse = new Mouse(name, weight, region);
                            Console.WriteLine(mouse.MakeSound());
                            if (foodType == "Vegetable" || foodType == "Fruit")
                            {
                                mouse.EatFood(foodCount, mouse.Multiplier);
                            }
                            else
                            {
                                Console.WriteLine(mouse.AnimalDoesntEat(foodType));
                            }
                            listAnimals.Add(mouse);
                        }
                    }
                }
                else
                {
                    var region = animalInput[3];
                    var breed  = animalInput[4];
                    if (type == "Cat")
                    {
                        var cat = new Cat(name, weight, region, breed);
                        Console.WriteLine(cat.MakeSound());
                        if (foodType == "Meat" || foodType == "Vegetable")
                        {
                            cat.EatFood(foodCount, cat.Multiplier);
                        }
                        else
                        {
                            Console.WriteLine(cat.AnimalDoesntEat(foodType));
                        }
                        listAnimals.Add(cat);
                    }
                    else
                    {
                        var tiger = new Tiger(name, weight, region, breed);
                        Console.WriteLine(tiger.MakeSound());
                        if (foodType == "Meat")
                        {
                            tiger.EatFood(foodCount, tiger.Multiplier);
                        }
                        else
                        {
                            Console.WriteLine(tiger.AnimalDoesntEat(foodType));
                        }
                        listAnimals.Add(tiger);
                    }
                }
                animalInput = Console.ReadLine().Split();
            }

            foreach (var item in listAnimals)
            {
                Console.WriteLine(item.ToString());
            }
        }
Esempio n. 24
0
        static void Main(string[] args)
        {
            var animalList = new List <Animal>();

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

                string animal = input[0];
                Food   food;
                switch (animal)
                {
                case nameof(Tiger):
                    var tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(tiger.ProduceSound());

                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        tiger.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(tiger);
                    break;

                case "Cat":
                    var cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(cat.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        cat.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(cat);

                    break;

                case "Dog":
                    var dog = new Dog(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(dog.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        dog.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(dog);

                    break;

                case "Mouse":
                    var mice = new Mouse(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(mice.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        mice.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(mice);

                    break;

                case "Hen":
                    var hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(hen.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        hen.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(hen);

                    break;

                case "Owl":
                    var owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(owl.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        owl.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(owl);

                    break;

                default:
                    break;
                }
            }
            animalList.ForEach(Console.WriteLine);

            //var hen = new Hen("GOGo", 2.5, 30);
            //Console.WriteLine(hen);
        }
Esempio n. 25
0
        public static void Main()
        {
            var animals = new List <Animal>();

            string inputLine;

            while ((inputLine = Console.ReadLine()) != "End")
            {
                var    input  = inputLine.Split();
                Animal animal = null;
                Food   food   = null;

                var animalType = input[0];
                var name       = input[1];
                var weight     = double.Parse(input[2]);

                switch (animalType)
                {
                case nameof(Owl):
                    animal = new Owl(name, weight, double.Parse(input[3]));
                    break;

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

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

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

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

                case nameof(Tiger):
                    animal = new Tiger(name, weight, input[3], input[4]);
                    break;
                }

                inputLine = Console.ReadLine();
                input     = inputLine.Split();

                var foodType = input[0];
                var quantity = int.Parse(input[1]);

                switch (foodType)
                {
                case nameof(Vegetable):
                    food = new Vegetable(quantity);
                    break;

                case nameof(Fruit):
                    food = new Fruit(quantity);
                    break;

                case nameof(Meat):
                    food = new Meat(quantity);
                    break;

                case nameof(Seeds):
                    food = new Seeds(quantity);
                    break;
                }

                Console.WriteLine(animal.ProduceSound());

                try
                {
                    animal.EatFood(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animals.Add(animal);
            }

            animals.ForEach(a => Console.WriteLine(a));
        }
Esempio n. 26
0
        public static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string animalInfo = string.Empty;

            while ((animalInfo = Console.ReadLine()) != "End")
            {
                string[] info = animalInfo.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                Animal animal = null;

                string animalType = info[0];
                string animalName = info[1];
                double weight     = double.Parse(info[2]);

                if (animalType == "Cat" || animalType == "Tiger")
                {
                    string livingRegion = info[3];
                    string breed        = info[4];

                    if (animalType == "Cat")
                    {
                        animal = new Cat(animalName, weight, livingRegion, breed);
                    }

                    else
                    {
                        animal = new Tiger(animalName, weight, livingRegion, breed);
                    }
                }

                else if (animalType == "Owl" || animalType == "Hen")
                {
                    double wingSize = double.Parse(info[3]);

                    if (animalType == "Owl")
                    {
                        animal = new Owl(animalName, weight, wingSize);
                    }

                    else
                    {
                        animal = new Hen(animalName, weight, wingSize);
                    }
                }

                else if (animalType == "Mouse" || animalType == "Dog")
                {
                    string livingRegion = info[3];

                    if (animalType == "Mouse")
                    {
                        animal = new Mouse(animalName, weight, livingRegion);
                    }
                    else
                    {
                        animal = new Dog(animalName, weight, livingRegion);
                    }
                }

                animals.Add(animal);

                Console.WriteLine(animal.ProduceSound());

                string[] foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                string foodType = foodInfo[0];
                int    quantity = int.Parse(foodInfo[1]);

                Food food = null;

                if (foodType == "Vegetable")
                {
                    food = new Vegetable(quantity);
                }

                else if (foodType == "Fruit")
                {
                    food = new Fruit(quantity);
                }

                else if (foodType == "Meat")
                {
                    food = new Meat(quantity);
                }

                else if (foodType == "Seeds")
                {
                    food = new Seeds(quantity);
                }

                Type animalT = animal.GetType();

                if (animalT.Name == "Hen")
                {
                    animal.Weight    += 0.35 * quantity;
                    animal.FoodEaten += quantity;
                }

                else if (animalT.Name == "Mouse")
                {
                    if (foodType == "Fruit" || foodType == "Vegetable")
                    {
                        animal.Weight    += 0.10 * quantity;
                        animal.FoodEaten += quantity;
                    }

                    else
                    {
                        Console.WriteLine($"{animalT.Name} does not eat {foodType}!");
                    }
                }

                else if (animalT.Name == "Cat")
                {
                    if (foodType == "Meat" || foodType == "Vegetable")
                    {
                        animal.Weight    += 0.30 * quantity;
                        animal.FoodEaten += quantity;
                    }

                    else
                    {
                        Console.WriteLine($"{animalT.Name} does not eat {foodType}!");
                    }
                }

                else if (animalT.Name == "Tiger" || animalT.Name == "Dog" || animalT.Name == "Owl")
                {
                    if (foodType == "Meat")
                    {
                        if (animalT.Name == "Tiger")
                        {
                            animal.Weight += 1.00 * quantity;
                        }

                        else if (animalT.Name == "Dog")
                        {
                            animal.Weight += 0.40 * quantity;
                        }

                        else if (animalT.Name == "Owl")
                        {
                            animal.Weight += 0.25 * quantity;
                        }

                        animal.FoodEaten += quantity;
                    }

                    else
                    {
                        Console.WriteLine($"{animalT.Name} does not eat {foodType}!");
                    }
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Esempio n. 27
0
        public static void Main(string[] args)
        {
            var input = Console.ReadLine();

            var list = new List <Animal>();

            while (input != "End")
            {
                var line = input.Split();

                if (line[0] == "Cat")
                {
                    Animal cat = new Cat(line[1], double.Parse(line[2]), 0, line[3], line[4]);

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(cat.ProduceSound());

                    if (food[0] == "Vegetable" || food[0] == "Meat")
                    {
                        cat.FoodEaten += int.Parse(food[1]);
                        cat.Weight    += 0.3 * int.Parse(food[1]);
                    }
                    else
                    {
                        Console.WriteLine($"Cat does not eat {food[0]}!");
                    }

                    list.Add(cat);
                }
                if (line[0] == "Tiger")
                {
                    Animal tiger = new Tiger(line[1], double.Parse(line[2]), 0, line[3], line[4]);

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(tiger.ProduceSound());

                    if (food[0] == "Meat")
                    {
                        tiger.FoodEaten += int.Parse(food[1]);
                        tiger.Weight    += 1 * int.Parse(food[1]);
                    }
                    else
                    {
                        Console.WriteLine($"Tiger does not eat {food[0]}!");
                    }

                    list.Add(tiger);
                }
                if (line[0] == "Dog")
                {
                    Animal dog = new Dog(line[1], double.Parse(line[2]), 0, line[3]);

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(dog.ProduceSound());

                    if (food[0] == "Meat")
                    {
                        dog.FoodEaten += int.Parse(food[1]);
                        dog.Weight    += 0.40 * int.Parse(food[1]);
                    }
                    else
                    {
                        Console.WriteLine($"Dog does not eat {food[0]}!");
                    }

                    list.Add(dog);
                }
                if (line[0] == "Owl")
                {
                    Animal owl = new Owl(line[1], double.Parse(line[2]), 0, double.Parse(line[3]));

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(owl.ProduceSound());

                    if (food[0] == "Meat")
                    {
                        owl.FoodEaten += int.Parse(food[1]);
                        owl.Weight    += 0.25 * int.Parse(food[1]);
                    }
                    else
                    {
                        Console.WriteLine($"Owl does not eat {food[0]}!");
                    }

                    list.Add(owl);
                }
                if (line[0] == "Mouse")
                {
                    Animal mouse = new Mouse(line[1], double.Parse(line[2]), 0, line[3]);

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(mouse.ProduceSound());

                    if (food[0] == "Fruit" || food[0] == "Vegetable")
                    {
                        mouse.FoodEaten += int.Parse(food[1]);
                        mouse.Weight    += 0.10 * int.Parse(food[1]);
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {food[0]}!");
                    }

                    list.Add(mouse);
                }
                if (line[0] == "Hen")
                {
                    Animal hen = new Hen(line[1], double.Parse(line[2]), 0, double.Parse(line[3]));

                    var food = Console.ReadLine().Split();

                    Console.WriteLine(hen.ProduceSound());

                    hen.FoodEaten += int.Parse(food[1]);
                    hen.Weight    += 0.35 * int.Parse(food[1]);
                    list.Add(hen);
                }

                input = Console.ReadLine();
            }

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
Esempio n. 28
0
        public void Run()
        {
            var animals = new List <Animal>();
            var counter = 0;

            while (true)
            {
                var command = Console.ReadLine().Split();

                if (command[0] == "End")
                {
                    break;
                }
                if (counter % 2 == 0)
                {
                    if (command[0] == "Hen")
                    {
                        var name     = command[1];
                        var weight   = double.Parse(command[2]);
                        var wingSize = double.Parse(command[3]);

                        var currHen = new Hen(name, weight, wingSize);
                        animals.Add(currHen);
                    }
                    else if (command[0] == "Owl")
                    {
                        var name     = command[1];
                        var weight   = double.Parse(command[2]);
                        var wingSize = double.Parse(command[3]);

                        var currOwl = new Owl(name, weight, wingSize);
                        animals.Add(currOwl);
                    }
                    else if (command[0] == "Mouse")
                    {
                        var name         = command[1];
                        var weight       = double.Parse(command[2]);
                        var livingRegion = command[3];

                        var currMouse = new Mouse(name, weight, livingRegion);
                        animals.Add(currMouse);
                    }
                    else if (command[0] == "Cat")
                    {
                        var name         = command[1];
                        var weight       = double.Parse(command[2]);
                        var livingRegion = command[3];
                        var breed        = command[4];

                        var currCat = new Cat(name, weight, livingRegion, breed);
                        animals.Add(currCat);
                    }
                    else if (command[0] == "Dog")
                    {
                        var name         = command[1];
                        var weight       = double.Parse(command[2]);
                        var livingRegion = command[3];

                        var currDog = new Dog(name, weight, livingRegion);
                        animals.Add(currDog);
                    }
                    else if (command[0] == "Tiger")
                    {
                        var name         = command[1];
                        var weight       = double.Parse(command[2]);
                        var livingRegion = command[3];
                        var breed        = command[4];

                        var currTiger = new Tiger(name, weight, livingRegion, breed);
                        animals.Add(currTiger);
                    }
                    Console.WriteLine(animals.Last().ProduceSound());
                }
                else
                {
                    var lastAnimal = animals.Last().GetType().Name;

                    if (command[0] == "Fruit")
                    {
                        var quantity  = int.Parse(command[1]);
                        var currFruit = new Fruit(quantity);

                        if (lastAnimal == "Hen" || lastAnimal == "Mouse")
                        {
                            HenEating(animals, quantity);
                            MouseEating(animals, quantity);
                        }
                        else
                        {
                            Console.WriteLine($"{lastAnimal} does not eat Fruit!");
                        }
                    }
                    else if (command[0] == "Meat")
                    {
                        var quantity = int.Parse(command[1]);
                        var currMeat = new Meat(quantity);

                        if (lastAnimal == "Hen" || lastAnimal == "Cat" ||
                            lastAnimal == "Tiger" || lastAnimal == "Dog" || lastAnimal == "Owl")
                        {
                            HenEating(animals, quantity);
                            CatEating(animals, quantity);
                            TigerEating(animals, quantity);
                            DogEating(animals, quantity);
                            OwlEating(animals, quantity);
                        }
                        else
                        {
                            Console.WriteLine($"{lastAnimal} does not eat Meat!");
                        }
                    }
                    else if (command[0] == "Seeds")
                    {
                        var quantity  = int.Parse(command[1]);
                        var currSeeds = new Seeds(quantity);

                        if (lastAnimal == "Hen")
                        {
                            HenEating(animals, quantity);
                        }
                        else
                        {
                            Console.WriteLine($"{lastAnimal} does not eat Seeds!");
                        }
                    }
                    else if (command[0] == "Vegetable")
                    {
                        var quantity      = int.Parse(command[1]);
                        var currVegetable = new Vegetable(quantity);

                        if (lastAnimal == "Mouse" || lastAnimal == "Hen" || lastAnimal == "Cat")
                        {
                            HenEating(animals, quantity);
                            MouseEating(animals, quantity);
                            CatEating(animals, quantity);
                        }
                        else
                        {
                            Console.WriteLine($"{lastAnimal} does not eat Vegetable!");
                        }
                    }
                }
                counter++;
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
        static void Main(string[] args)
        {
            var input       = Console.ReadLine();
            int countOfRows = 0;

            var listOfAnimals = new List <Animal>();
            var result        = new List <Animal>();


            while (input != "End")
            {
                try
                {
                    var command = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                    if (countOfRows % 2 == 0)
                    {
                        switch (command[0])
                        {
                        case "Owl":
                            Owl owl = new Owl(command[1], double.Parse(command[2]), double.Parse(command[3]));
                            listOfAnimals.Add(owl);
                            break;

                        case "Hen":
                            Hen hen = new Hen(command[1], double.Parse(command[2]), double.Parse(command[3]));
                            listOfAnimals.Add(hen);
                            break;

                        case "Cat":
                            Cat cat = new Cat(command[1], double.Parse(command[2]), command[3], command[4]);
                            listOfAnimals.Add(cat);
                            break;

                        case "Tiger":
                            Tiger tiger = new Tiger(command[1], double.Parse(command[2]), command[4], command[3]);
                            listOfAnimals.Add(tiger);
                            break;

                        case "Mouse":
                            Mouse mouse = new Mouse(command[1], double.Parse(command[2]), command[3]);
                            listOfAnimals.Add(mouse);
                            break;

                        case "Dog":
                            Dog dog = new Dog(command[1], double.Parse(command[2]), command[3]);
                            listOfAnimals.Add(dog);
                            break;

                        default:
                            throw new ArgumentException("Invalid Animal!");
                        }
                    }
                    else
                    {
                        var currentAnimal = listOfAnimals[listOfAnimals.Count - 1];
                        currentAnimal.ProduceSound();
                        switch (command[0])
                        {
                        case "Fruit":
                            Fruit fruit = new Fruit(int.Parse(command[1]));
                            if (currentAnimal.WhatCanEat.Contains(fruit.GetType().Name))
                            {
                                currentAnimal.Eat(fruit.Quantity);
                                result.Add(currentAnimal);
                            }
                            else
                            {
                                result.Add(currentAnimal);
                                throw new ArgumentException($"{currentAnimal.GetType().Name} does not eat {fruit.GetType().Name}!");
                            }
                            break;

                        case "Meat":
                            Meat meat = new Meat(int.Parse(command[1]));
                            if (currentAnimal.WhatCanEat.Contains(meat.GetType().Name))
                            {
                                currentAnimal.Eat(meat.Quantity);
                                result.Add(currentAnimal);
                            }
                            else
                            {
                                result.Add(currentAnimal);
                                throw new ArgumentException($"{currentAnimal.GetType().Name} does not eat {meat.GetType().Name}!");
                            }
                            break;

                        case "Vegetable":
                            Vegetable vegetable = new Vegetable(int.Parse(command[1]));
                            if (currentAnimal.WhatCanEat.Contains((vegetable.GetType().Name).ToString()))
                            {
                                currentAnimal.Eat(vegetable.Quantity);
                                result.Add(currentAnimal);
                            }
                            else
                            {
                                result.Add(currentAnimal);
                                throw new ArgumentException($"{currentAnimal.GetType().Name} does not eat {vegetable.GetType().Name}!");
                            }
                            break;

                        default:
                            throw new ArgumentException("Invalid Food!");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                input = Console.ReadLine();
                countOfRows++;
            }
            foreach (var animal in result)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Esempio n. 30
0
        static void Main(string[] args)
        {
            var animalsCollect = new List <Animal>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] animal = input.Split();
                string   type   = animal[0];
                string   name   = animal[1];
                double   weight = double.Parse(animal[2]);

                string[] foods    = Console.ReadLine().Split();
                string   typeFood = foods[0];
                int      quantity = int.Parse(foods[1]);

                Animal animals = null;
                switch (type)
                {
                case "Cat":
                    string livingRegionCat = animal[3];
                    string breedCat        = animal[4];
                    animals = new Cat(name, weight, livingRegionCat, breedCat);
                    break;

                case "Tiger":
                    string livingRegionTiger = animal[3];
                    string breedTiger        = animal[4];
                    animals = new Tiger(name, weight, livingRegionTiger, breedTiger);
                    break;

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

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

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

                case "Owl":
                    double wingSizeOwl = double.Parse(animal[3]);
                    animals = new Owl(name, weight, wingSizeOwl);
                    break;

                default:
                    break;
                }
                Console.WriteLine(animals.Sound());
                animalsCollect.Add(animals);

                Food food = null;
                try
                {
                    switch (typeFood)
                    {
                    case "Vegetable":
                        food = new Vegetable(quantity);
                        break;

                    case "Fruit":
                        food = new Fruit(quantity);
                        break;

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

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

                    default:
                        break;
                    }
                    animals.EatFood(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            foreach (var animal in animalsCollect)
            {
                Console.WriteLine(animal);
            }
        }