Пример #1
0
    static void Main(string[] args)
    {
        var allAnimals = new List <Animal>();
        var allFood    = new List <Food>();
        var input      = Console.ReadLine();
        int inputCount = 2;

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

            try
            {
                if (inputCount % 2 == 0)
                {
                    var animalType = separated[0];
                    var name       = separated[1];
                    var weight     = double.Parse(separated[2]);
                    switch (animalType)
                    {
                    case "Owl":
                        var owl = (new Owl(name, weight, double.Parse(separated[3])));
                        owl.AskForFood();
                        allAnimals.Add(owl);

                        break;

                    case "Hen":
                        var hen = (new Hen(name, weight, double.Parse(separated[3])));
                        hen.AskForFood();
                        allAnimals.Add(hen);
                        break;

                    case "Mouse":
                        var mouse = new Mouse(name, weight, separated[3]);
                        mouse.AskForFood();
                        allAnimals.Add(mouse);
                        break;

                    case "Dog":
                        var dog = new Dog(name, weight, separated[3]);
                        dog.AskForFood();
                        allAnimals.Add(dog);
                        break;

                    case "Cat":
                        var cat = new Cat(name, weight, separated[3], separated[4]);
                        cat.AskForFood();
                        allAnimals.Add(cat);
                        break;

                    case "Tiger":
                        var tiger = new Tiger(name, weight, separated[3], separated[4]);
                        tiger.AskForFood();
                        allAnimals.Add(tiger);
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    var typeOfFood   = separated[0];
                    int foodQuantity = int.Parse(separated[1]);

                    switch (typeOfFood)
                    {
                    case "Meat":
                        var meat = new Meat(foodQuantity);
                        allAnimals.Last().Eat(meat);
                        break;

                    case "Vegetable":
                        var vegetable = new Vegetable(foodQuantity);
                        allAnimals.Last().Eat(vegetable);
                        break;

                    case "Fruit":
                        var fruit = new Fruit(foodQuantity);
                        allAnimals.Last().Eat(fruit);
                        break;

                    case "Seeds":
                        var seeds = new Seeds(foodQuantity);
                        allAnimals.Last().Eat(seeds);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
            inputCount++;
            input = Console.ReadLine();
        }

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

        while ((inputAnimal = Console.ReadLine()) != "End")
        {
            string[] dataAnimal = inputAnimal.Split(' ', StringSplitOptions.RemoveEmptyEntries);
            string[] dataFood   = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);

            switch (dataAnimal[0])
            {
            case "Owl":
                Owl owl = new Owl(dataAnimal[1], double.Parse(dataAnimal[2]), double.Parse(dataAnimal[3]));
                Console.WriteLine(owl.AskForFood());
                owl.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(owl);
                break;

            case "Hen":
                Hen hen = new Hen(dataAnimal[1], double.Parse(dataAnimal[2]), double.Parse(dataAnimal[3]));
                Console.WriteLine(hen.AskForFood());
                hen.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(hen);
                break;

            case "Mouse":
                Mouse mouse = new Mouse(dataAnimal[1], double.Parse(dataAnimal[2]), dataAnimal[3]);
                Console.WriteLine(mouse.AskForFood());
                mouse.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(mouse);
                break;

            case "Dog":
                Dog dog = new Dog(dataAnimal[1], double.Parse(dataAnimal[2]), dataAnimal[3]);
                Console.WriteLine(dog.AskForFood());
                dog.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(dog);
                break;

            case "Cat":
                Cat cat = new Cat(dataAnimal[1], double.Parse(dataAnimal[2]), dataAnimal[3], dataAnimal[4]);
                Console.WriteLine(cat.AskForFood());
                cat.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(cat);
                break;

            case "Tiger":
                Tiger tiger = new Tiger(dataAnimal[1], double.Parse(dataAnimal[2]), dataAnimal[3], dataAnimal[4]);
                Console.WriteLine(tiger.AskForFood());
                tiger.Eat(dataFood[0], int.Parse(dataFood[1]));
                animals.Add(tiger);
                break;
            }
        }

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