Esempio n. 1
0
        static void Main(string[] args)
        {
            string input       = string.Empty;
            int    counter     = 0;
            var    listAnimals = new Stack <Animal>();
            var    listFood    = new Stack <Food>();

            while ((input = Console.ReadLine()) != "End")
            {
                if (counter % 2 == 0)
                {
                    var tokens = input
                                 .Split()
                                 .ToArray();

                    string animalType   = tokens[0];
                    string animalName   = tokens[1];
                    double animalWeight = double.Parse(tokens[2]);

                    if (animalType == "Owl")
                    {
                        double wingSize = double.Parse(tokens[3]);
                        Owl    owl      = new Owl(animalName, animalWeight, wingSize);
                        owl.Sound();
                        listAnimals.Push(owl);
                    }
                    else if (animalType == "Hen")
                    {
                        double wingSize = double.Parse(tokens[3]);
                        Hen    hen      = new Hen(animalName, animalWeight, wingSize);
                        hen.Sound();
                        listAnimals.Push(hen);
                    }
                    else if (animalType == "Mouse")
                    {
                        string livingRegion = tokens[3];
                        Mouse  mouse        = new Mouse(animalName, animalWeight, livingRegion);
                        mouse.Sound();
                        listAnimals.Push(mouse);
                    }
                    else if (animalType == "Dog")
                    {
                        string livingRegion = tokens[3];
                        Dog    dog          = new Dog(animalName, animalWeight, livingRegion);
                        dog.Sound();
                        listAnimals.Push(dog);
                    }
                    else if (animalType == "Cat")
                    {
                        string livingRegion = tokens[3];
                        string breed        = tokens[4];
                        Cat    cat          = new Cat(animalName, animalWeight, livingRegion, breed);
                        cat.Sound();
                        listAnimals.Push(cat);
                    }
                    else if (animalType == "Tiger")
                    {
                        string livingRegion = tokens[3];
                        string breed        = tokens[4];
                        Tiger  tiger        = new Tiger(animalName, animalWeight, livingRegion, breed);
                        tiger.Sound();
                        listAnimals.Push(tiger);
                    }
                }
                else if (counter % 2 != 0)
                {
                    var token = input
                                .Split()
                                .ToArray();

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

                    if (foodType == "Vegetable")
                    {
                        Vegetable vegetable = new Vegetable(quantity);
                        listFood.Push(vegetable);
                    }
                    else if (foodType == "Fruit")
                    {
                        Fruit fruit = new Fruit(quantity);
                        listFood.Push(fruit);
                    }
                    else if (foodType == "Meat")
                    {
                        Meat meat = new Meat(quantity);
                        listFood.Push(meat);
                    }
                    else if (foodType == "Seeds")
                    {
                        Seeds seeds = new Seeds(quantity);
                        listFood.Push(seeds);
                    }
                }

                if (counter % 2 != 0)
                {
                    listAnimals.Peek().Eat(listFood.Peek());
                }

                counter++;
            }

            while (listAnimals.Count > 0)
            {
                Console.WriteLine(listAnimals.Pop().ToString());
            }
        }
Esempio n. 2
0
        public void Run()
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                string[] tokens     = input.Split();
                string   typeAnimal = tokens[0];

                if (typeAnimal == "Cat")
                {
                    Animal   cat      = new Cat(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    cat.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        cat.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Cat does not eat {typeFood}");
                    }
                    animals.Add(cat);
                }
                else if (typeAnimal == "Dog")
                {
                    Animal   dog      = new Dog(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    dog.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        dog.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Dog does not eat {typeFood}");
                    }
                    animals.Add(dog);
                }
                else if (typeAnimal == "Tiger")
                {
                    Animal   tiger    = new Tiger(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    tiger.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        tiger.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Tiger does not eat {typeFood}");
                    }
                    animals.Add(tiger);
                }
                else if (typeAnimal == "Mouse")
                {
                    Animal   mouse    = new Mouse(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    mouse.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        mouse.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(mouse);
                }
                else if (typeAnimal == "Owl")
                {
                    Animal   owl      = new Owl(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    owl.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        owl.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(owl);
                }
                else if (typeAnimal == "Hen")
                {
                    Animal   hen      = new Hen(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    hen.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        hen.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(hen);
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var animalsCollect = new List <Animal>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] animal = input.Split();
                string   type   = animal[0];
                string   name   = animal[1];
                double   weight = double.Parse(animal[2]);

                string[] foods    = Console.ReadLine().Split();
                string   typeFood = foods[0];
                int      quantity = int.Parse(foods[1]);

                Animal animals = null;
                switch (type)
                {
                case "Cat":
                    string livingRegionCat = animal[3];
                    string breedCat        = animal[4];
                    animals = new Cat(name, weight, livingRegionCat, breedCat);
                    break;

                case "Tiger":
                    string livingRegionTiger = animal[3];
                    string breedTiger        = animal[4];
                    animals = new Tiger(name, weight, livingRegionTiger, breedTiger);
                    break;

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

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

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

                case "Owl":
                    double wingSizeOwl = double.Parse(animal[3]);
                    animals = new Owl(name, weight, wingSizeOwl);
                    break;

                default:
                    break;
                }
                Console.WriteLine(animals.Sound());
                animalsCollect.Add(animals);

                Food food = null;
                try
                {
                    switch (typeFood)
                    {
                    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;

                    default:
                        break;
                    }
                    animals.EatFood(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            foreach (var animal in animalsCollect)
            {
                Console.WriteLine(animal);
            }
        }