コード例 #1
0
        public void Run()
        {
            int    index = 0;
            string input;
            string dataTypeAnimal = string.Empty;
            string dataName       = string.Empty;

            while ((input = Console.ReadLine()) != "End")
            {
                try
                {
                    string[] argument = input.Split();
                    if (index % 2 == 0)
                    {
                        AddedAnimal(argument);

                        //Saves in variables type and name for check to the foods!
                        dataTypeAnimal = argument[0];
                        dataName       = argument[1];
                    }
                    else if (index % 2 != 0)
                    {
                        string foodName     = argument[0];
                        double foodQuantity = double.Parse(argument[1]);

                        IAnimal currentAnimal = this.animals
                                                .FirstOrDefault(a => a.Name == dataName && a.GetType().Name == dataTypeAnimal);
                        if (currentAnimal != null && this.typeOFFoodsFromAnimals.ContainsKey(dataTypeAnimal))
                        {
                            if (this.typeOFFoodsFromAnimals[dataTypeAnimal].Contains(foodName))
                            {
                                currentAnimal.EatFood(foodQuantity);
                            }
                            else
                            {
                                throw new ArgumentException($"{dataTypeAnimal} does not eat {foodName}!");
                            }
                        }
                    }

                    index++;
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                    index++;
                }
            }

            PrintAnimals();
        }