示例#1
0
        static void Main(string[] args)
        {
            string animalType = Console.ReadLine();

            while (animalType != "Beast!")
            {
                string[] animalInfo = Console.ReadLine().Split();
                string   name       = animalInfo[0];
                int      age        = int.Parse(animalInfo[1]);
                string   gender     = animalInfo[2];

                try
                {
                    switch (animalType)
                    {
                    case "Cat": Cat cat = new Cat(name, age, gender);
                        Console.WriteLine(cat);
                        cat.MakeSound();
                        break;

                    case "Dog":
                        Dog dog = new Dog(name, age, gender);
                        Console.WriteLine(dog);
                        dog.MakeSound();
                        break;

                    case "Frog":
                        Frog frog = new Frog(name, age, gender);
                        Console.WriteLine(frog);
                        frog.MakeSound();
                        break;

                    case "Kittens":
                        Kittens kittens = new Kittens(name, age, gender);
                        Console.WriteLine(kittens);
                        kittens.MakeSound();
                        break;

                    case "Tomcat":
                        Tomcat tomcat = new Tomcat(name, age, gender);
                        Console.WriteLine(tomcat);
                        tomcat.MakeSound();
                        break;

                    default:
                        break;
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animalType = Console.ReadLine();
            }
        }
        static void Main(string[] args)
        {
            while (true)
            {
                string   command = Console.ReadLine();
                string[] animals = Console.ReadLine().Split(" ").ToArray();
                string   name    = animals[0];
                int      age     = int.Parse(animals[1]);
                string   gender  = animals[2];

                if (command == "Beast!")
                {
                    break;
                }

                if (string.IsNullOrEmpty(name) || age < 0 || string.IsNullOrEmpty(gender))
                {
                    Console.WriteLine("Invalid input!");
                    continue;
                }


                if (command == "Cat")
                {
                    var cat = new Cat(name, age, gender);
                    Console.WriteLine(cat);
                    Console.WriteLine(cat.ProduceSound());
                }

                else if (command == "Dog")
                {
                    var dog = new Dog(name, age, gender);
                    Console.WriteLine(dog);
                    Console.WriteLine(dog.ProduceSound());
                }
                else if (command == "Frog")
                {
                    var frog = new Frog(name, age, gender);
                    Console.WriteLine(frog);
                    Console.WriteLine(frog.ProduceSound());
                }
                else if (command == "Kittens")
                {
                    var kittens = new Kittens(name, age, gender);
                    Console.WriteLine(kittens);
                    Console.WriteLine(kittens.ProduceSound());
                }
                else if (command == "TomCat")
                {
                    var tomCat = new TomCat(name, age, gender);
                    Console.WriteLine(tomCat);
                    Console.WriteLine(tomCat.ProduceSound());
                }
            }
        }
        static void Main()
        {
            //Create a hierarchy Dog, Frog, Cat, Kitten, Tomcat and define useful constructors and methods.
            //Dogs, frogs and cats are Animals. All animals can produce sound (specified by the ISound interface). All animals are described by age, name and sex.
            //Kittens and tomcats are cats. Kittens can be only female and tomcats can be only male.
            //Each animal produces a specific sound.

            //Create arrays of different kinds of animals and calculate the average age of each kind of animal using a static method (you may use LINQ).
            Dog sharo = new Dog("Sharo", 5, "Male");
            Dog jony  = new Dog("Jony", 3, "Male");
            //sharo.Sound();
            Cats maumau = new Cats("Maumau", 4, "Female");
            //maumau.Sound();
            Kittens smallMeau = new Kittens("Lili", 1);
            Tomcats tomy      = new Tomcats("Tomy", 1);
            Frogs   greeny    = new Frogs("Greeny", 7, "Male");
            //Age averege try 1 (that was missunderstanding) calculate average to all animals
            //Console.WriteLine(Animal.AvgAge());

            ////GetType testing (zaigravka)
            //Console.WriteLine(sharo.GetType().BaseType); //Animals.Animal
            //Console.WriteLine(sharo.GetType().FullName); //Animals.Dog
            //Console.WriteLine(tomy.GetType().BaseType); //Animals.Cats
            //Console.WriteLine(tomy.GetType().BaseType.Name); //Cats
            //Console.WriteLine(sharo.GetType().BaseType.Name); //Animal
            //Console.WriteLine(sharo.GetType().Name); //Dog

            ////GetType testing (zaigravka) prodaljenito
            //Console.WriteLine(sharo.Kind);
            //Console.WriteLine(tomy.Kind);
            //Console.WriteLine(smallMeau.Kind);
            //Console.WriteLine(maumau.Kind);

            //Dogs 4
            //Cats 5
            //Frogs 6
            //var byAnimalType =
            //    from anim in Animal.AllAnimals
            //    group anim by anim.Kind into grouped
            //    select new { animType = grouped.Key, avgAge = grouped.Average(x => x.Age) };

            //foreach (var item in byAnimalType)
            //{
            //    Console.Write(item.animType + " ");
            //    Console.WriteLine(item.avgAge);
            //}

            //Problem 3. Animal hierarchy
            string a = Animal.avgAgeOfAnimals(Animal.AllAnimals);

            Console.WriteLine(a);
        }
示例#4
0
        public static void Main(string[] args)
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "Beast!")
                {
                    break;
                }
                string[] data   = Console.ReadLine().Split();
                string   name   = data[0];
                int      age    = int.Parse(data[1]);
                string   gender = data[2];
                if (string.IsNullOrEmpty(name) || age < 0 || string.IsNullOrEmpty(gender))
                {
                    Console.WriteLine("Invalid input!");
                    continue;
                }

                if (input == "Cat")
                {
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat.ProduceSound());
                }
                else if (input == "Dog")
                {
                    Dog dog = new Dog(name, age, gender);
                    Console.WriteLine(dog.ProduceSound());
                }
                else if (input == "Frog")
                {
                    Frog frog = new Frog(name, age, gender);
                    Console.WriteLine(frog.ProduceSound());
                }
                else if (input == "Kitten")
                {
                    Kittens kitten = new Kittens(name, age);
                    Console.WriteLine(kitten.ProduceSound());
                }
                else if (input == "Tomcat")
                {
                    Tomcat tomcat = new Tomcat(name, age);
                    Console.WriteLine(tomcat.ProduceSound());
                }
            }
        }