示例#1
0
        //Chek if the name exist and do ProduceSound();
        #region Chek if the name exist in our list and print the produce sound
        static void CheckForTheNameInLists(List <Dog> dogs, List <Cat> cats, List <Snake> snakes, string name)
        {
            foreach (var dog in dogs)
            {
                if (dog.Name == name)
                {
                    Dog.ProduceSound();
                    break;
                }
            }

            foreach (var cat in cats)
            {
                if (cat.Name == name)
                {
                    Cat.ProduceSound();
                    break;
                }
            }

            foreach (var snake in snakes)
            {
                if (snake.Name == name)
                {
                    Snake.ProduceSound();
                    break;
                }
            }
        }
示例#2
0
        private static void SoundProduce(string name)
        {
            foreach (var dog in dogs)
            {
                if (dog.Name == name)
                {
                    Dog.ProduceSound();
                    break;
                }
            }

            foreach (var cat in cats)
            {
                if (cat.Name == name)
                {
                    Cat.ProduceSound();
                    break;
                }
            }

            foreach (var snake in snakes)
            {
                if (snake.Name == name)
                {
                    Snake.ProduceSound();
                    break;
                }
            }
            #endregion
        }
示例#3
0
        static void Main(string[] args)
        {
            Dog    sharo = new Dog("Sharo", 12, "Male");
            Dog    laika = new Dog("Laika", 20, "Female");
            Cat    maci  = new Cat("Maci", 5, "male");
            Cat    cezar = new Cat("Cezar", 2, "male");
            Frog   niki  = new Frog("Niki", 4, "male");
            Kitten qia   = new Kitten("Qia", 1);
            Kitten misha = new Kitten("Misho", 2);
            Tomcat gosho = new Tomcat("Gosho", 4);

            List <Animal> zoo = new List <Animal>()
            {
                sharo, laika, maci, cezar, niki, qia, misha, gosho
            };

            sharo.ProduceSound();
            qia.ProduceSound();
            niki.ProduceSound();

            var groupedZoo =
                from a in zoo
                group a by a.GetType().Name into g
                select new { GROUP = g.Key, AVAGE = g.Average(x => x.Age) };

            foreach (var animal in groupedZoo)
            {
                Console.WriteLine("Average age of {0} is {1}", animal.GROUP, animal.AVAGE);
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            Dog dog = new Dog("sharo", AnimalSex.male, 18);

            Console.WriteLine(dog.ToString());

            Frog frog = new Frog("Kermit", AnimalSex.male, 20);

            Console.WriteLine(frog.ToString());

            Cat regularCat = new Cat("CAT", AnimalSex.female, 4);

            Console.WriteLine(regularCat.ToString());

            Kitten kitten = new Kitten("kittie", AnimalSex.female, 1);

            //Kitten kitten = new Kitten("kittie", AnimalSex.male, 1); - this will explode
            Console.WriteLine(kitten.ToString());

            Tomcat tomcat = new Tomcat("Topcat", AnimalSex.male, 2);

            //Tomcat tomcat = new Tomcat("Topcat", AnimalSex.female, 2); - this too will explode
            Console.WriteLine(tomcat.ToString());


            //sounds
            Console.WriteLine();
            Console.WriteLine(dog.ProduceSound());
            Console.WriteLine(frog.ProduceSound());
            Console.WriteLine(regularCat.ProduceSound());
            Console.WriteLine(kitten.ProduceSound());
            Console.WriteLine(tomcat.ProduceSound());
            Console.WriteLine();

            List <Dog> dogs = new List <Dog>();

            dogs.Add(dog);
            dogs.Add(new Dog("me4o", AnimalSex.female, 69));
            dogs.Add(new Dog("FU", AnimalSex.female, 96));
            double averageAge = dogs.Average((x) => x.Age);

            Console.WriteLine("Dogs average age: {0}", averageAge);

            List <Frog> frogs = new List <Frog>();

            frogs.Add(frog);
            frogs.Add(new Frog("Big Toad", AnimalSex.male, 2));
            frogs.Add(new Frog("Lesser Toad", AnimalSex.female, 2));
            averageAge = frogs.Average((x) => x.Age);
            Console.WriteLine("Frogs average age: {0}", averageAge);

            List <Kitten> kittens = new List <Kitten>();

            kittens.Add(kitten);
            kittens.Add(new Kitten("little kittenn", AnimalSex.female, 2));
            kittens.Add(new Kitten("big kitten", AnimalSex.female, 2));
            averageAge = kittens.Average((x) => x.Age);
            Console.WriteLine("Kittens average age: {0}", averageAge);
        }
示例#5
0
文件: Animals.cs 项目: Risorm/OOP
        static void Main()
        {
            Kitten molly = new Kitten("Molly", 13);

            Console.WriteLine(molly);
            molly.ProduceSound();
            Frog kiro = new Frog("Kiro", 1, Gender.Female);

            Console.WriteLine(kiro);
            kiro.ProduceSound();
            Dog sharo = new Dog("Sharo", 5, Gender.Male);

            Console.WriteLine(sharo);
            sharo.ProduceSound();
            Tomcat pesho = new Tomcat("Pesho", 3);

            Console.WriteLine(pesho);
            pesho.ProduceSound();
            Cat pena = new Cat("Pena", 2, Gender.Female);

            Console.WriteLine(pena);
            pena.ProduceSound();

            Kitten dolly = new Kitten("Dolly", 2);
            Frog   biro  = new Frog("Biro", 3, Gender.Male);
            Dog    paro  = new Dog("Paro", 10, Gender.Female);
            Tomcat mesho = new Tomcat("Mesho", 1);
            Cat    mena  = new Cat("Mena", 1, Gender.Male);

            Animal[] animals = new Animal[]
            {
                molly, kiro, sharo, pesho, pena,
                dolly, biro, paro, mesho, mena
            };

            var averageAge = from o in animals
                             group o by new { GroupName = o.GetType().Name } into gr
                select new
            {
                gr.Key.GroupName,
                AvarageAge = gr.Average(o => o.Age)
            };

            foreach (var animal in averageAge)
            {
                Console.WriteLine(String.Format("Type: {0}, Average age: {1:0.##}", animal.GroupName, animal.AvarageAge));
            }
        }
示例#6
0
        static void Main()
        {
            Tomcat cat  = new Tomcat("jonny", 4);
            Kitten cat2 = new Kitten("Holly", 2);
            Frog   frog = new Frog("Jwey", 6, "male");
            Dog    dog  = new Dog("Sharo", 3, "male");

            cat.ProduceSound();
            cat2.ProduceSound();
            frog.ProduceSound();
            dog.ProduceSound();

            Animal[] animals =
            {
                cat,
                cat2,
                frog,
                dog,
                new Dog("Polly",    12, "female"),
                new Frog("Kolio",    2, "male"),
                new Tomcat("Nuni",  3),
                new Kitten("Loly",  9),
                new Frog("Josh",     1, "male"),
                new Kitten("Jenny", 5),
                new Dog("Bond",      5, "male"),
                new Tomcat("Josh", 14)
            };

            var dogs = from animal in animals
                       where animal is Dog
                       select animal;

            var frogs = from animal in animals
                        where animal is Frog
                        select animal;

            var cats = from animal in animals
                       where animal is Cat
                       select animal;

            Console.WriteLine("Dogs avg age: " + ((double)dogs.Sum(a => a.Age)) / dogs.Count());

            Console.WriteLine("Frogs avg age: " + ((double)frogs.Sum(a => a.Age)) / frogs.Count());

            Console.WriteLine("Cats avg age: " + ((double)cats.Sum(a => a.Age)) / cats.Count());
        }
示例#7
0
        private static void ProduceSound(Dictionary <string, List <int> > dogs, Dictionary <string, List <int> > cats, Dictionary <string, List <int> > snakes, Dog dog, Cat cat, Snake snake, string talkName)
        {
            if (dogs.ContainsKey(talkName))
            {
                dog.ProduceSound();
            }

            if (cats.ContainsKey(talkName))
            {
                cat.ProduceSound();
            }

            if (snakes.ContainsKey(talkName))
            {
                snake.ProduceSound();
            }
        }
示例#8
0
        static void Main()
        {
            Tomcat cat = new Tomcat("jonny", 4);
            Kitten cat2 = new Kitten("Holly", 2);
            Frog frog = new Frog("Jwey", 6, "male");
            Dog dog = new Dog("Sharo", 3, "male");
            cat.ProduceSound();
            cat2.ProduceSound();
            frog.ProduceSound();
            dog.ProduceSound();

            Animal[] animals = {
                cat, 
                cat2, 
                frog, 
                dog,
                new Dog("Polly", 12, "female"),
                new Frog("Kolio", 2, "male"),
                new Tomcat("Nuni", 3),
                new Kitten("Loly", 9),
                new Frog("Josh", 1, "male"),
                new Kitten("Jenny", 5),
                new Dog("Bond", 5, "male"),
                new Tomcat("Josh", 14)
            };

            var dogs = from animal in animals
                where animal is Dog
                select animal;

            var frogs = from animal in animals
                where animal is Frog
                select animal;

            var cats = from animal in animals
                where animal is Cat
                select animal;

            Console.WriteLine("Dogs avg age: " + ((double)dogs.Sum(a => a.Age)) / dogs.Count());

            Console.WriteLine("Frogs avg age: " + ((double)frogs.Sum(a => a.Age)) / frogs.Count());

            Console.WriteLine("Cats avg age: " + ((double)cats.Sum(a => a.Age)) / cats.Count());
        }
        static void Main()
        {
            Dog rex  = new Dog("Rex", 3, "male");
            Dog max  = new Dog("Max", 1, "male");
            Dog sara = new Dog("Sara", 6, "female");

            Frog frog   = new Frog("Frog", 7, "male");
            Frog jaba   = new Frog("Jaba", 4, "female");
            Frog kermit = new Frog("Kermit", 10, "male");

            Cat kitty    = new Kitten("Kitty", 2);
            Cat snowball = new Tomcat("Snowball", 5);

            List <Animal> animals = new List <Animal>()
            {
                rex,
                max,
                sara,
                frog,
                jaba,
                kermit,
                kitty,
                snowball
            };

            var groupedAnimals =
                from animal in animals
                group animal by animal.GetType().Name into g
                select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) };

            foreach (var animal in groupedAnimals)
            {
                Console.WriteLine("{0}s - average age: {1:N2}", animal.GroupName, animal.AverageAge);
            }

            rex.ProduceSound();
            kermit.ProduceSound();
            snowball.ProduceSound();
        }
        public static void Main()
        {
            var dogs   = new List <Dog>();
            var cats   = new List <Cat>();
            var snakes = new List <Snake>();

            var entered = Console.ReadLine();

            while (entered != "I'm your Huckleberry")
            {
                var parameters = entered.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();

                var kind     = parameters[0];
                var name     = parameters[1];
                var age      = string.Empty;
                var property = string.Empty;;

                if (parameters.Count > 2)
                {
                    age      = parameters[2];
                    property = parameters[3];
                }

                if (kind == "Dog")
                {
                    var newDog = new Dog();
                    {
                        newDog.Class = kind;
                        newDog.Name  = name;
                        newDog.Age   = age;
                        newDog.Legs  = property;
                    }
                    dogs.Add(newDog);
                }
                else if (kind == "Cat")
                {
                    var newCat = new Cat();
                    {
                        newCat.Class = kind;
                        newCat.Name  = name;
                        newCat.Age   = age;
                        newCat.IQ    = property;
                    }
                    cats.Add(newCat);
                }
                else if (kind == "Snake")
                {
                    var newSnake = new Snake();
                    {
                        newSnake.Class   = kind;
                        newSnake.Name    = name;
                        newSnake.Age     = age;
                        newSnake.Cruelty = property;
                    }
                    snakes.Add(newSnake);
                }
                else
                {
                    foreach (var item in dogs)
                    {
                        if (item.Name == name)
                        {
                            Console.WriteLine(Dog.ProduceSound());
                        }
                    }
                    foreach (var item in cats)
                    {
                        if (item.Name == name)
                        {
                            Console.WriteLine(Cat.ProduceSound());
                        }
                    }
                    foreach (var item in snakes)
                    {
                        if (item.Name == name)
                        {
                            Console.WriteLine(Snake.ProduceSound());
                        }
                    }
                }

                entered = Console.ReadLine();
            }

            foreach (var item in dogs)
            {
                Console.WriteLine($"{item.Class}: {item.Name}, Age: {item.Age}, Number Of Legs: {item.Legs}");
            }
            foreach (var item in cats)
            {
                Console.WriteLine($"{item.Class}: {item.Name}, Age: {item.Age}, IQ: {item.IQ}");
            }
            foreach (var item in snakes)
            {
                Console.WriteLine($"{item.Class}: {item.Name}, Age: {item.Age}, Cruelty: {item.Cruelty}");
            }
        }
        static void Main(string[] args)
        {
            List <Dog>   dogs   = new List <Dog>();
            List <Cat>   cats   = new List <Cat>();
            List <Snake> snakes = new List <Snake>();

            string command = Console.ReadLine();

            while (command != "I'm your Huckleberry")
            {
                string[] tokens = command.Split();
                string   type   = tokens[0];

                if (type == "Dog")
                {
                    string name      = tokens[1];
                    int    age       = int.Parse(tokens[2]);
                    int    numOFLegs = int.Parse(tokens[3]);
                    Dog    dog       = new Dog(name, age, numOFLegs);
                    dogs.Add(dog);
                }
                else if (type == "Cat")
                {
                    string name = tokens[1];
                    int    age  = int.Parse(tokens[2]);
                    int    iq   = int.Parse(tokens[3]);
                    Cat    cat  = new Cat(name, age, iq);
                    cats.Add(cat);
                }
                else if (type == "Snake")
                {
                    string name = tokens[1];
                    int    age  = int.Parse(tokens[2]);
                    int    crueltyCoefficient = int.Parse(tokens[3]);
                    Snake  snake = new Snake(name, age, crueltyCoefficient);
                    snakes.Add(snake);
                }
                else
                {
                    if (cats.Any(x => x.Name == tokens[1]))
                    {
                        Cat.ProduceSound();
                    }
                    else if (dogs.Any(x => x.Name == tokens[1]))
                    {
                        Dog.ProduceSound();
                    }
                    else
                    {
                        Snake.ProduceSound();
                    }
                }
                command = Console.ReadLine();
            }

            foreach (var dog in dogs)
            {
                Console.WriteLine($"Dog: {dog.Name}, Age: {dog.Age}, Number Of Legs: {dog.NumberOfLegs}");
            }

            foreach (var cat in cats)
            {
                Console.WriteLine($"Cat: {cat.Name}, Age: {cat.Age}, IQ: {cat.IQ}");
            }
            foreach (var snake in snakes)
            {
                Console.WriteLine($"Snake: {snake.Name}, Age: {snake.Age}, Cruelty: {snake.CrueltyCoefficient}");
            }
        }
示例#12
0
        static void Main(string[] args)
        {
            Dog dog = new Dog("sharo", AnimalSex.male, 18);
            Console.WriteLine(dog.ToString());

            Frog frog = new Frog("Kermit", AnimalSex.male, 20);
            Console.WriteLine(frog.ToString());

            Cat regularCat = new Cat("CAT", AnimalSex.female, 4);
            Console.WriteLine(regularCat.ToString());

            Kitten kitten = new Kitten("kittie", AnimalSex.female, 1);
            //Kitten kitten = new Kitten("kittie", AnimalSex.male, 1); - this will explode
            Console.WriteLine(kitten.ToString());

            Tomcat tomcat = new Tomcat("Topcat", AnimalSex.male, 2);
            //Tomcat tomcat = new Tomcat("Topcat", AnimalSex.female, 2); - this too will explode
            Console.WriteLine(tomcat.ToString());

            //sounds
            Console.WriteLine();
            Console.WriteLine(dog.ProduceSound());
            Console.WriteLine(frog.ProduceSound());
            Console.WriteLine(regularCat.ProduceSound());
            Console.WriteLine(kitten.ProduceSound());
            Console.WriteLine(tomcat.ProduceSound());
            Console.WriteLine();

            List<Dog> dogs = new List<Dog>();
            dogs.Add(dog);
            dogs.Add(new Dog("me4o", AnimalSex.female, 69));
            dogs.Add(new Dog("FU", AnimalSex.female, 96));
            double averageAge = dogs.Average((x) => x.Age);
            Console.WriteLine("Dogs average age: {0}", averageAge);

            List<Frog> frogs = new List<Frog>();
            frogs.Add(frog);
            frogs.Add(new Frog("Big Toad", AnimalSex.male, 2));
            frogs.Add(new Frog("Lesser Toad", AnimalSex.female, 2));
            averageAge = frogs.Average((x) => x.Age);
            Console.WriteLine("Frogs average age: {0}", averageAge);

            List<Kitten> kittens = new List<Kitten>();
            kittens.Add(kitten);
            kittens.Add(new Kitten("little kittenn", AnimalSex.female, 2));
            kittens.Add(new Kitten("big kitten", AnimalSex.female, 2));
            averageAge = kittens.Average((x) => x.Age);
            Console.WriteLine("Kittens average age: {0}", averageAge);
        }