Beispiel #1
0
        static void Main(string[] args)
        {
            // create some animals from each class and print them
            Dog dog = new Dog("Sharo", 3, Genders.Male);
            Console.WriteLine(dog);
            dog.FetchStick();

            Console.WriteLine();

            Frog frog = new Frog("Kermit", 33, Genders.Male);
            Console.WriteLine(frog);
            frog.Jump();

            Console.WriteLine();

            Kitten kitty = new Kitten("Kitty", 1);
            Console.WriteLine(kitty);
            kitty.Cry();

            Console.WriteLine();

            Tomcat tomcat = new Tomcat("Tom", 12);
            Console.WriteLine(tomcat);
            tomcat.Piss();

            Console.WriteLine();

            // create an array of Animals
            Animal[] animals = new Animal[]
            {
                new Dog("Rex",3, Genders.Male),
                new Frog("Kekerica", 1, Genders.Female),
                new Kitten("Pisi", 1),
                new Tomcat("Tom",2),
                new Dog("Erik", 4, Genders.Male),
                new Frog("Penka", 1, Genders.Female),
                new Kitten("Jasmin", 2),
                new Tomcat("Kolio",6),
                new Dog("Bender",2, Genders.Male),
                new Frog("Ginka", 6, Genders.Female),
                new Kitten("Tedy", 1),
                new Tomcat("Muncho",4),
            };

            // calculate the aveage age of each animal and print them
            var averageAge =
                from an in animals
                group an by new { GroupName = an.GetType().Name } into gr
                select new
                {
                    GroupName = gr.Key.GroupName,
                    AvarageAge = gr.Average(an => an.Age)
                };

            foreach (var animal in averageAge)
            {
                Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge));
            }
        }
Beispiel #2
0
        public static void Main()
        {
            Tomcat tomcat1 = new Tomcat("Tomcat", 13);
            Tomcat tomcat2 = new Tomcat("Tomcat", 3);
            Tomcat tomcat3 = new Tomcat("Tomcat", 10);

            Kitten kitten1 = new Kitten("Kitten", 1);
            Kitten kitten2 = new Kitten("Kitten", 1);
            Kitten kitten3 = new Kitten("Kitten", 9);

            Dog dog1 = new Dog("Dog", 15, Gender.Female);
            Dog dog2 = new Dog("Dog", 3, Gender.Male);
            Dog dog3 = new Dog("Dog", 7, Gender.Female);

            Frog frog1 = new Frog("Frog", 4, Gender.Female);
            Frog frog2 = new Frog("Frog", 0, Gender.Male);
            Frog frog3 = new Frog("Frog", 2, Gender.Female);

            IList<Animal> animals = new List<Animal>
            {
                tomcat1, tomcat2, tomcat3, kitten1, kitten2, kitten3, dog1, dog2, dog3, frog1, frog2, frog3
            };

            tomcat1.ProduceSound();
            kitten1.ProduceSound();
            dog1.ProduceSound();
            frog1.ProduceSound();
            
            double catsAverage = animals
                .Where(c => c is Cat)
                .Average(c => c.Age);

            double dogsAverage = animals
                .Where(c => c is Dog)
                .Average(c => c.Age);

            double frogsAverage = animals
                .Where(c => c is Frog)
                .Average(c => c.Age);

            Console.WriteLine("Average cats age: {0:F2}", catsAverage);
            Console.WriteLine("Average dogs age: {0:F2}", dogsAverage);
            Console.WriteLine("Average frogs age: {0:F2}", frogsAverage);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            string animal = Console.ReadLine();

            try
            {

                 while (animal != "Beast!")
                {
                    string line = Console.ReadLine();

                    string[] input = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
                    string name = "";
                    int age = 0;
                    string gender = "";
                    Animal newAnimal = null;
                    if (input.Length == 3)
                    {
                        name = input[0];
                        age = int.Parse(input[1]);
                        gender = input[2];
                    }
                    else if (input.Length == 2)
                    {
                        name = input[0];
                        age = int.Parse(input[1]);
                        if (name=="kitten" || name=="Kitten")
                        {
                            gender = "Female";
                        }
                        else
                        {
                            gender = "Male";
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    switch (animal.ToLower())
                    {
                        case "animal":
                             newAnimal = new Animal(name, age, gender); break;
                        case "cat":
                             newAnimal = new Cat(name, age, gender); break;
                        case "dog":
                            newAnimal = new Dog(name, age, gender); break;
                        case "frog":
                            newAnimal = new Frog(name, age, gender); break;
                        case "kitten":
                            newAnimal = new Kitten(name, age, gender); break;
                        case "tomcat":
                            newAnimal = new Tomcat(name, age, gender); break;
                    }

                    animal = Console.ReadLine();
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string animal = Console.ReadLine();

            try
            {
                while (animal != "Beast!")
                {
                    string line = Console.ReadLine();

                    string[] input     = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
                    string   name      = "";
                    int      age       = 0;
                    string   gender    = "";
                    Animal   newAnimal = null;
                    if (input.Length == 3)
                    {
                        name   = input[0];
                        age    = int.Parse(input[1]);
                        gender = input[2];
                    }
                    else if (input.Length == 2)
                    {
                        name = input[0];
                        age  = int.Parse(input[1]);
                        if (name == "kitten" || name == "Kitten")
                        {
                            gender = "Female";
                        }
                        else
                        {
                            gender = "Male";
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    switch (animal.ToLower())
                    {
                    case "animal":
                        newAnimal = new Animal(name, age, gender); break;

                    case "cat":
                        newAnimal = new Cat(name, age, gender); break;

                    case "dog":
                        newAnimal = new Dog(name, age, gender); break;

                    case "frog":
                        newAnimal = new Frog(name, age, gender); break;

                    case "kitten":
                        newAnimal = new Kitten(name, age, gender); break;

                    case "tomcat":
                        newAnimal = new Tomcat(name, age, gender); break;
                    }


                    animal = Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }