Esempio n. 1
0
        public static void Main()
        {
            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var animaArgs  = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var animalType = animaArgs[0];
                var animalName = animaArgs[1];
                var weight     = double.Parse(animaArgs[2]);
                var region     = animaArgs[3];

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

                    var foodArgs = Console.ReadLine()
                                   .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var vegQ = int.Parse(foodArgs[1]);
                    cat.Eat(vegQ);
                    Console.WriteLine(cat);
                }
                else
                {
                    var animal   = CreateAnimal(animalType, animalName, weight, region);
                    var foodArgs = Console.ReadLine()
                                   .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var type     = foodArgs[0];
                    var quantity = int.Parse(foodArgs[1]);
                    Console.WriteLine(animal.MakeSound());
                    try
                    {
                        animal.Eat(type, quantity);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine(animal);
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            string input;
            Animal beast = null;

            while ((input = Console.ReadLine()) != "End")
            {
                var animalInfo = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var foodInfo   = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    string animalType   = animalInfo[0];
                    string name         = animalInfo[1];
                    double weight       = double.Parse(animalInfo[2]);
                    string livingRegion = animalInfo[3];

                    switch (animalType)
                    {
                    case "Tiger":
                        beast = new Tiger(name, animalType, weight, livingRegion);
                        break;

                    case "Zebra":
                        beast = new Zebra(name, animalType, weight, livingRegion);
                        break;

                    case "Mouse":
                        beast = new Mouse(name, animalType, weight, livingRegion);
                        break;

                    case "Cat":
                        beast = new Cat(name, animalType, animalInfo[4], weight, livingRegion);
                        break;

                    default:
                        break;
                    }

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

                    Food food = null;

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

                    beast.MakeSound();
                    beast.Eat(food);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }


                Console.WriteLine(beast);
            }
        }