Beispiel #1
0
        static void Main(string[] args)
        {
            Animal animal = null;
            Food   food   = null;
            string input  = Console.ReadLine();

            //string foods = Console.ReadLine();
            while (input != "End")
            {
                string[] line   = input.Split(new[] { ' ', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                string[] str    = Console.ReadLine().Split(' ');
                string   type   = line[0];
                string   name   = line[1];
                double   weight = double.Parse(line[2]);
                string   region = line[3];
                switch (type.ToLower())
                {
                case "cat":
                    string breed = line[4];
                    animal = new Cat(name, type, weight, region, breed); break;

                case "mouse":
                    animal = new Mouse(name, type, weight, region); break;

                case "tiger":
                    animal = new Tiger(name, type, weight, region); break;

                case "zebra":
                    animal = new Zebra(name, type, weight, region); break;
                }

                var vg = str[0];
                int r  = int.Parse(str[1]);
                if (vg == "Meat")
                {
                    food = new Meat(r);
                }
                else
                {
                    food = new Vegetable(r);
                }
                animal.MakeSound();
                animal.Eat(food);
                Console.WriteLine(animal);
                input = Console.ReadLine();
            }
        }
        static void Main()
        {
            var animalInfo = Console.ReadLine()
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

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

                var animalName         = animalInfo[1];
                var animalWeight       = double.Parse(animalInfo[2]);
                var animalLivingRegion = animalInfo[3];

                var foodType     = foodInfo[0];
                var foodQuantity = int.Parse(foodInfo[1]);

                Food food;
                if (foodType.Equals("Vegetable"))
                {
                    food = new Vegetable(foodQuantity);
                }
                else
                {
                    food = new Meat(foodQuantity);
                }

                switch (animalInfo[0])
                {
                case "Cat":
                    var catBreed = animalInfo[4];

                    var cat = new Cat(animalName, animalWeight, animalLivingRegion, catBreed);
                    cat.MakeSound();
                    cat.Eat(food);
                    Console.WriteLine(cat);
                    break;

                case "Tiger":
                    var tiger = new Tiger(animalName, animalWeight, animalLivingRegion);
                    tiger.MakeSound();
                    tiger.Eat(food);
                    Console.WriteLine(tiger);
                    break;

                case "Zebra":
                    var zebra = new Zebra(animalName, animalWeight, animalLivingRegion);
                    zebra.MakeSound();
                    zebra.Eat(food);
                    Console.WriteLine(zebra);
                    break;

                case "Mouse":
                    var mouse = new Mouse(animalName, animalWeight, animalLivingRegion);
                    mouse.MakeSound();
                    mouse.Eat(food);
                    Console.WriteLine(mouse);
                    break;
                }

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