예제 #1
0
    static void Main(string[] args)
    {
        List <Animal> farm  = new List <Animal>();
        List <Food>   foods = new List <Food>();
        string        input;

        while ((input = Console.ReadLine()) != "End")
        {
            string[] animalArgs = input.Split();
            string[] foodArgs   = Console.ReadLine().Split();

            string animalType   = animalArgs[0];
            string animalName   = animalArgs[1];
            double animalWeight = double.Parse(animalArgs[2]);
            Animal animal       = null;

            string foodType     = foodArgs[0];
            int    foodQuantity = int.Parse(foodArgs[1]);
            Food   food         = null;

            switch (animalType)
            {
            case "Hen":
                double henWingSize = double.Parse(animalArgs[3]);
                animal = new Hen(animalName, animalWeight, henWingSize);
                break;

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

            case "Mouse":
                string mouseLivingRegion = animalArgs[3];
                animal = new Mouse(animalName, animalWeight, mouseLivingRegion);
                break;

            case "Cat":
                string catLivingRegion = animalArgs[3];
                string catBreed        = animalArgs[4];
                animal = new Cat(animalName, animalWeight, catLivingRegion, catBreed);
                break;

            case "Dog":
                string dogLivingRegion = animalArgs[3];
                animal = new Dog(animalName, animalWeight, dogLivingRegion);
                break;

            case "Tiger":
                string tigerLivingRegion = animalArgs[3];
                string tigerBreed        = animalArgs[4];
                animal = new Tiger(animalName, animalWeight, tigerLivingRegion, tigerBreed);
                break;
            }

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

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

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

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

            farm.Add(animal);
            foods.Add(food);
        }

        int foodIndex = 0;

        foreach (var animal in farm)
        {
            try
            {
                animal.AskForFood();
                animal.Eat(foods[foodIndex++]);
            }
            catch (WrongFoodException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        farm.ForEach(a => Console.WriteLine(a));
    }
예제 #2
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());
        }
    }
예제 #3
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);
        }
    }
    static void Main()
    {
        List <Animal> animals = new List <Animal>();
        int           i       = 0;

        string input = Console.ReadLine();

        while (input != "End")
        {
            string[] inputTokens = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            if (i % 2 == 0)
            {
                Animal animal = null;
                string name   = inputTokens[1];
                double weight = double.Parse(inputTokens[2]);

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

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

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

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

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

                case "Tiger":
                    animal = new Tiger(name, weight, inputTokens[3], inputTokens[4]);
                    break;
                }
                Console.WriteLine(animal.AskForFood());
                animals.Add(animal);
            }
            else
            {
                Animal animal   = animals.Last();
                Food   food     = null;
                int    quantity = int.Parse(inputTokens[1]);

                switch (inputTokens[0])
                {
                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;
                }
                animal.EatFood(food);
            }
            i++;
            input = Console.ReadLine();
        }

        foreach (Animal animal in animals)
        {
            Console.WriteLine(animal);
        }
    }
        public void Run()
        {
            List <IAnimal> animals = new List <IAnimal>();
            string         input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] animalTokens = input.Split();
                string   animalType   = animalTokens[0];
                string   name         = animalTokens[1];
                double   weight       = double.Parse(animalTokens[2]);
                IAnimal  currAnimal   = null;
                switch (animalType)
                {
                case nameof(Owl):
                    double wingSize = double.Parse(animalTokens[3]);
                    currAnimal = new Owl(name, weight, wingSize);
                    break;

                case nameof(Hen):
                    wingSize   = double.Parse(animalTokens[3]);
                    currAnimal = new Hen(name, weight, wingSize);
                    break;

                case nameof(Mouse):
                    string livingRegion = animalTokens[3];
                    currAnimal = new Mouse(name, weight, livingRegion);
                    break;

                case nameof(Dog):
                    livingRegion = animalTokens[3];
                    currAnimal   = new Dog(name, weight, livingRegion);
                    break;

                case nameof(Cat):
                    livingRegion = animalTokens[3];
                    string breed = animalTokens[4];
                    currAnimal = new Cat(name, weight, livingRegion, breed);
                    break;

                case nameof(Tiger):
                    livingRegion = animalTokens[3];
                    breed        = animalTokens[4];
                    currAnimal   = new Tiger(name, weight, livingRegion, breed);
                    break;

                default:
                    break;
                }

                string[] foodTokens = Console.ReadLine().Split();
                string   foodType   = foodTokens[0];
                int      quantity   = int.Parse(foodTokens[1]);
                IFood    food       = null;
                switch (foodType)
                {
                case nameof(Meat):
                    food = new Meat(quantity);
                    break;

                case nameof(Vegetable):
                    food = new Vegetable(quantity);

                    break;

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

                    break;

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

                    break;

                default:
                    break;
                }

                Console.WriteLine(currAnimal.AskForFood());
                bool hasEaten = currAnimal.EatFood(food);
                if (!hasEaten)
                {
                    Console.WriteLine($"{animalType} does not eat {foodType}!");
                }
                animals.Add(currAnimal);
            }

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