コード例 #1
0
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

            while (true)
            {
                var command = Console.ReadLine();

                if (command == "End")
                {
                    break;
                }

                var animalInfo = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                var foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                var foodType = foodInfo[0];
                var quantity = int.Parse(foodInfo[1]);

                if (animalInfo[0] == "Owl")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Owl(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Hen")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Hen(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Mouse")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Mouse(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Dog")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Dog(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Cat")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Cat(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Tiger")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Tiger(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #2
0
ファイル: Engine.cs プロジェクト: Yusmen/C-Sharp-OOP-Basics
        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);
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var animalList = new List <Animal>();

            while (true)
            {
                string[] input = Console.ReadLine().Split();
                if (input[0] == "End")
                {
                    break;
                }

                string animal = input[0];
                Food   food;
                switch (animal)
                {
                case nameof(Tiger):
                    var tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(tiger.ProduceSound());

                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        tiger.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(tiger);
                    break;

                case "Cat":
                    var cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(cat.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        cat.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(cat);

                    break;

                case "Dog":
                    var dog = new Dog(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(dog.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        dog.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(dog);

                    break;

                case "Mouse":
                    var mice = new Mouse(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(mice.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        mice.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(mice);

                    break;

                case "Hen":
                    var hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(hen.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        hen.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(hen);

                    break;

                case "Owl":
                    var owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(owl.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        owl.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(owl);

                    break;

                default:
                    break;
                }
            }
            animalList.ForEach(Console.WriteLine);

            //var hen = new Hen("GOGo", 2.5, 30);
            //Console.WriteLine(hen);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: delbusque/SoftUni
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string[] animalInput = Console.ReadLine().Split();
            string[] foodInput   = Console.ReadLine().Split();

            while (animalInput[0] != "End")
            {
                string type     = animalInput[0];
                string name     = animalInput[1];
                double weight   = double.Parse(animalInput[2]);
                string foodType = foodInput[0];
                int    quantity = int.Parse(foodInput[1]);

                if (type == nameof(Hen))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird hen = new Hen(name, weight, wingSize);
                    Console.WriteLine(hen.AskForFood());
                    hen.Eat(foodType, quantity);
                    animals.Add(hen);
                }
                else if (type == nameof(Owl))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird owl = new Owl(name, weight, wingSize);
                    Console.WriteLine(owl.AskForFood());
                    owl.Eat(foodType, quantity);
                    animals.Add(owl);
                }
                else if (type == nameof(Cat))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline cat = new Cat(name, weight, livingRegion, breed);
                    Console.WriteLine(cat.AskForFood());
                    cat.Eat(foodType, quantity);
                    animals.Add(cat);
                }
                else if (type == nameof(Tiger))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline tiger = new Tiger(name, weight, livingRegion, breed);
                    Console.WriteLine(tiger.AskForFood());
                    tiger.Eat(foodType, quantity);
                    animals.Add(tiger);
                }
                else if (type == nameof(Mouse))
                {
                    string livingRegion = animalInput[3];

                    Mammal mouse = new Mouse(name, weight, livingRegion);
                    Console.WriteLine(mouse.AskForFood());
                    mouse.Eat(foodType, quantity);
                    animals.Add(mouse);
                }
                else if (type == nameof(Dog))
                {
                    string livingRegion = animalInput[3];

                    Mammal dog = new Dog(name, weight, livingRegion);
                    Console.WriteLine(dog.AskForFood());
                    dog.Eat(foodType, quantity);
                    animals.Add(dog);
                }


                animalInput = Console.ReadLine().Split();
                if (animalInput[0] == "End")
                {
                    break;
                }
                foodInput = Console.ReadLine().Split();
            }

            foreach (var item in animals)
            {
                Console.WriteLine(item.ToString());
            }
        }
コード例 #5
0
        public static void Main()
        {
            var result = new StringBuilder();

            var animalList = new List <Animal>();

            var typeOfAnimals = new Dictionary <string, List <string> >();

            AddTypes(typeOfAnimals);

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                string[] animalToken = input.Split(' ');

                string type = animalToken[0];

                input = Console.ReadLine();

                string[] token = input.Split(' ');

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

                try
                {
                    // Birds
                    if (typeOfAnimals["Bird"].Contains(type))
                    {
                        string name         = animalToken[1];
                        double animalWeight = double.Parse(animalToken[2]);
                        double wingSize     = double.Parse(animalToken[3]);

                        switch (type)
                        {
                        case "Owl":
                            Owl owl = new Owl(name, animalWeight, wingSize);
                            result.AppendLine(owl.ProduceSound());
                            animalList.Add(owl);
                            owl.Eat(typeOfFood, quantity);
                            break;

                        case "Hen":
                            Hen hen = new Hen(name, animalWeight, wingSize);
                            result.AppendLine(hen.ProduceSound());
                            animalList.Add(hen);
                            hen.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Mammals
                    if (typeOfAnimals["Mammal"].Contains(type))
                    {
                        string mammalName         = animalToken[1];
                        double mammalAnimalWeight = double.Parse(animalToken[2]);
                        string mammalLivingRegion = animalToken[3];

                        switch (type)
                        {
                        case "Mouse":
                            Mouse mouse = new Mouse(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            animalList.Add(mouse);
                            result.AppendLine(mouse.ProduceSound());
                            mouse.Eat(typeOfFood, quantity);
                            break;

                        case "Dog":
                            Dog dog = new Dog(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            result.AppendLine(dog.ProduceSound());
                            animalList.Add(dog);
                            dog.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Feline
                    if (typeOfAnimals["Feline"].Contains(type))
                    {
                        string felineName         = animalToken[1];
                        double felineWeight       = double.Parse(animalToken[2]);
                        string felineLivingRegion = animalToken[3];
                        string felineBreed        = animalToken[4];

                        switch (type)
                        {
                        case "Cat":
                            Cat cat = new Cat(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(cat);
                            result.AppendLine(cat.ProduceSound());
                            cat.Eat(typeOfFood, quantity);
                            break;

                        case "Tiger":
                            Tiger tiger = new Tiger(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(tiger);
                            result.AppendLine(tiger.ProduceSound());
                            tiger.Eat(typeOfFood, quantity);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    result.AppendLine(ex.Message);
                }
            }

            Console.Write(result.ToString());

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