Inheritance: Animal
Exemple #1
3
        public static void Main()
        {
            Cat[] cats = new Cat[]
            {
                new Cat("Pussi", 3, false),
                new Cat("Pesho", 6, true),
                new Cat("Ginka", 7, false),
            };
            Dog[] dogs = new Dog[]
            {
                new Dog("Misho", 5, true, "Ovcharka"),
                new Dog("Bull", 6, true, "Buldog"),
                new Dog("Rocko", 7, true, "Sharpei"),
            };
            Frog[] frogs = new Frog[]
            {
                new Frog("Furi", 2, true),
                new Frog("Mani", 3, false),
                new Frog("Perr", 2, false),
            };
            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Stupi", 1, "Persian"),
                new Kitten("Lili", 1, "Persian"),
                new Kitten("Plupi", 2, "Angora"),
            };
            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Gunko", 4),
                new Tomcat("Porfavor", 3),
                new Tomcat("Kalin", 5),
            };

            double dogsAvAge = Animal.AverageAge(dogs);
            double catsAvAge = Animal.AverageAge(cats);
            double frogsAvAge = Animal.AverageAge(frogs);
            double kittensAvAge = Animal.AverageAge(kittens);
            double tomcatsAvAge = Animal.AverageAge(tomcats);

            Console.WriteLine("Average age: \nDogs: {0:F1};\nCats: {1:F1};\nFrogs: {2:F1};\nKittens: {3:F1};\nTomcats: {4:F1};",
                dogsAvAge, catsAvAge, frogsAvAge, kittensAvAge, tomcatsAvAge);
            Console.WriteLine();

            Console.WriteLine("Different animals sounds:");
            Console.Write("Cats: ");
            cats[0].ProduceSound();
            Console.Write("Dogs: ");
            dogs[1].ProduceSound();
            Console.Write("Frogs: ");
            frogs[2].ProduceSound();
            Console.WriteLine();

            Console.WriteLine("Different animals character activities:");
            Console.WriteLine(dogs[2].SpinTail());
            Console.WriteLine(cats[1].Tease());
            Console.WriteLine(frogs[2].Jump());
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            var jaba = new Frog("baba jaba", 1, Genders.Female);
            var kekerica = new Frog("kekerica", 3, Genders.Female);

            var sharo = new Dog("sharo", 3, Genders.Male);
            var sara = new Dog("sara", 2, Genders.Female);
            var oldy = new Dog("oldy", 12, Genders.Male);

            var puhi = new Kitten("puhi", 2);
            var tommy = new Tomcat("tommy", 4);
            var mouseKiller = new Cat("mousy", 5, Genders.Male);

            var animals = new List<Animal>()
            {
                jaba, 
                kekerica,
                sharo,
                sara,
                puhi,
                tommy,
                mouseKiller,
                oldy
            };
            
            var avredgeAgeOfKinds = from a in animals
                                    group a by a.GetType().Name into g
                                    select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) };
            foreach (var animal in avredgeAgeOfKinds)
            {
                Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName, animal.AverageAge);
            }
        }
Exemple #3
0
        public static void Main()
        {
            var animals = new List<Animal>();

            var kitten = new Kitten("Kitty", 2);

            SayAngAddToList(kitten, animals);

            var tomcat = new Tomcat("Tom", 1);

            SayAngAddToList(tomcat, animals);

            var frog = new Frog("Froggy", 5, Sex.Male);

            SayAngAddToList(frog, animals);

            var dog = new Dog("Sharo", 4, Sex.Male);

            SayAngAddToList(dog, animals);

            var anotherDog = new Dog("Lassy", 7, Sex.Female);

            SayAngAddToList(anotherDog, animals);

            Console.WriteLine("The average age of all animal is {0}", animals.Average(x => x.Age));
        }
Exemple #4
0
        static void Main()
        {
            Animal max = new Dog("Max", 3, Gender.Male);
            Animal duke = new Dog("Duke", 1, Gender.Male);
            Animal fluffy = new Dog("Fluffy", 6, Gender.Male);
            Animal wink = new Dog("Wink", 3, Gender.Male);
            Animal abby = new Frog("Abby", 1, Gender.Female);
            Animal boogy = new Frog("Boogy", 2, Gender.Male);
            Animal pookie = new Frog("Pookie", 6, Gender.Female);
            Animal hermit = new Frog("Hermit", 2, Gender.Male);
            Animal abbey = new Kitten("Abbey", 1, Gender.Female);
            Animal panda = new Kitten("Panda", 1, Gender.Female);
            Animal ravi = new Tomcat("Ravi", 3, Gender.Male);
            Animal lexus = new Tomcat("Lexus", 9, Gender.Male);
            Animal biggie = new Cat("Biggie", 7, Gender.Male);
            Animal fig = new Cat("Fig", 7, Gender.Male);

            Animal[] animals = new Animal[] { max, duke, fluffy, wink, abby, boogy, pookie, hermit, abbey, panda, ravi, lexus, biggie, fig };

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

            foreach (var animal in groupedAnimals)
            {
                Console.WriteLine("Group: {0} | Average Age: {1:F2}", animal.GroupName, animal.AverageAge);
            }
        }
Exemple #5
0
        static void Main()
        {
            var scooby = new Dog("Scooby", 2, 'm');
            var frogy = new Frog("Frogy", 0.5, 'm');
            var tommy = new Cat("Tommy", 1, 'm');
            var kitty = new Kitten("Kitty", 0.8, 'f');
            var speedy = new Tomcat("Speedy", 1.5, 'f');
            var spark = new Dog("Spark", 3, 'm');
            var carmit = new Frog("Carmit", 1, 'm');
            var jenny = new Kitten("Jenny", 1.2, 'f');

            var animals = new Animal[] { scooby, spark, frogy, tommy, kitty, speedy };
            var differentAnimals = new Animal[] { tommy, kitty, carmit, jenny };

            foreach (var animal in animals)
            {
                Console.Write(animal);
                animal.ProduceSound();
                Console.WriteLine();
            }

            var groupedAnimals =
                from anim in animals
                group anim by (anim is Cat) ? typeof (Cat) : anim.GetType()
                into g
                select new {GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age)};

            foreach (var animal in groupedAnimals)
            {
                Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge);
            }
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Frog[] frogs = new Frog[5];
            frogs[0] = new Frog("Kermit", 5, 'M');
            frogs[1] = new Frog("Piggy", 8, 'F');
            frogs[2] = new Frog("Cookie Monster", 3, 'F');
            frogs[3] = new Frog("Oscar the grouch", 15, 'M');
            frogs[4] = new Frog("Kermit's double", 5, 'M');

            Dog[] dogs = new Dog[5];
            dogs[0] = new Dog("Rex", 1, 'M');
            dogs[1] = new Dog("Sergeant Dirty", 6, 'F');
            dogs[2] = new Dog("Lucky", 7, 'F');
            dogs[3] = new Dog("Unlucky", 2, 'M');
            dogs[4] = new Dog("Tramp", 4, 'M');

            Cat[] cats = new Cat[5];
            cats[0] = new Cat("Tom", 6, 'M');
            cats[1] = new Kitten("Princess", 1);
            cats[2] = new Cat("Jerry", 2, 'F');
            cats[3] = new Tomcat("Behemoth", 1);
            cats[4] = new Cat("Pluto", 7, 'M');

            Console.WriteLine("The average age of the frogs is " + Animal.AverageAge(frogs));
            Console.WriteLine("The average age of the dogs is " + Animal.AverageAge(dogs));
            Console.WriteLine("The average age of the cats is " + Animal.AverageAge(cats));
        }
        static void Main()
        {
            // create some animals from each class and print them
            Dog dog = new Dog("Sharo", 3, Gender.Male);
            Console.WriteLine(dog);
            dog.FetchStick();

            Console.WriteLine();

            Frog frog = new Frog("Kermit", 33, Gender.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, Gender.Male),
                new Frog("Kekerica", 1, Gender.Female),
                new Kitten("Pisi", 1),
                new Tomcat("Tom",2),
                new Dog("Erik", 4, Gender.Male),
                new Frog("Penka", 1, Gender.Female),
                new Kitten("Jasmin", 2),
                new Tomcat("Kolio",6),
                new Dog("Bender",2, Gender.Male),
                new Frog("Ginka", 6, Gender.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));
            }
        }
        static void Main()
        {
            // Attention! - No animals were harmed during these tests

            Console.Title = "Animals";

            var dog = new Dog("Gosho", 3, Gender.Male);
            Console.WriteLine(dog);
            dog.FetchStick();
            Console.Write(Environment.NewLine);

            var frog = new Frog("Masha", 16, Gender.Female);
            Console.WriteLine(frog);
            frog.Transform();
            Console.Write(Environment.NewLine);

            var kitty = new Kitten("Maca", 0);
            Console.WriteLine(kitty);
            kitty.MeltYourHeart();
            Console.Write(Environment.NewLine);

            var tomcat = new Tomcat("Tom", 4);
            Console.WriteLine(tomcat);
            tomcat.Piss();
            Console.Write(Environment.NewLine);

            var animals = new Animal[]
            {
                new Dog("Freya", 1, Gender.Female),
                new Dog("Sharo", 4, Gender.Male),
                new Frog("Penka", 20, Gender.Female),
                new Frog("Fred", 32, Gender.Male),
                new Kitten("Merlin", 1),
                new Kitten("Shusia", 0),
                new Tomcat("Felix",6),
                new Tomcat("Silvester", 5),
            };

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

            foreach (var animal in averageAge)
            {
                Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge));
            }

            // And what does the Fox say? Oh, no foxes here...
        }
        static void Main()
        {
            Dog dog = new Dog("Sharo", 5, Gender.Male);
            Console.WriteLine(dog);

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();


            List<Animal> animals = new List<Animal>()
            {
                new Dog("Rex",3, Gender.Male),
                new Frog("Kekerica", 1, Gender.Female),
                new Kitten("Pisi", 1),
                new Tomcat("Tom",2),
                new Dog("Erik", 4, Gender.Male),
                new Frog("Penka", 1, Gender.Female),
                new Kitten("Jasmin", 2),
                new Tomcat("Kolio",6),
                new Dog("Bender",2, Gender.Male),
                new Frog("Ginka", 6, Gender.Female),
                new Kitten("Tedy", 1),
                new Tomcat("Muncho",4),
            };

            var avgGradeForCurrentAnimal = animals
                .GroupBy(x => x.GetType().Name)
                .Select(a => new
                {
                    Name = a.Key,
                    AvgAge = a.Average(g => g.Age)
                });

            foreach (var currAnimal in avgGradeForCurrentAnimal)
            {
                Console.WriteLine(currAnimal);
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            Dog pinchi = new Dog("Pinchi", 2, "male", "bauuuuuuuu");
            Dog suzi = new Dog("Pinchi", 4, "male", "bauuuuuuuu");
            Dog djeimi = new Dog("Pinchi", 4, "male", "bauuuuuuuu");

            Kitten mimi = new Kitten("kitten", 5, "female", "miiiauuu");
            Kitten kuki = new Kitten("kitten", 5, "female", "miiiauuu");

            Tomcat tom = new Tomcat("Top4o", 15, "male", "myuuuu");

            Frog crazy = new Frog("Crazy Frog", 10, "male", "Kvaaa");
            Console.WriteLine(Frog.AverageFrog().Count());
        }
Exemple #11
0
        static void Main(string[] args)
        {
            var frog1 = new Frog(25, "jaba1", true);
            var frog2 = new Frog(20, "jaba2", false);
            var frog3 = new Frog(25, "jaba3", true);
            var frog4 = new Frog(20, "jaba4", false);
            var allFrogs=new Frog[]{frog1,frog2,frog3,frog4};

            var dog1 = new Dog(30, "kuche1", true);
            var dog2 = new Dog(30, "kuche2", true);
            var dog3 = new Dog(30, "kuche3", true);
            var allDogs=new Dog[]{dog1,dog2,dog3};

            var randomCat1 = new Cat(5, "random kotka1", false);
            var randomCat2 = new Cat(13, "random kotka2", false);
            var randomCat3 = new Cat(5, "random kotka3",true);
            var allCats=new Cat[]{randomCat1,randomCat2,randomCat3};

            var femaleCat1 = new Kitten(5, "jenska kotka1");
            var femaleCat2 = new Kitten(43, "jenska kotka2");
            var femaleCat3 = new Kitten(56, "jenska kotka3");
            var allFemaleCats=new Kitten[]{femaleCat1,femaleCat2,femaleCat3};

            var maleCat1 = new TomCat(5, "mujka kotka1");
            var maleCat2 = new TomCat(6, "mujka kotka2");
            var maleCat3 = new TomCat(200, "mujka kotka3");
            var allMaleCats=new TomCat[]{maleCat1,maleCat2,maleCat3};

            var frogs = allFrogs.Average(x => x.Age);
            Console.WriteLine("The avergae age of frogs is: {0}",frogs);

            var dogs = allDogs.Average(x => x.Age);
            Console.WriteLine("The avergae age of dogs is: {0}", dogs);

            var cats = allCats.Average(x => x.Age);
            Console.WriteLine("The avergae age of  random cats is: {0}", cats);

            var femaleCats = allFemaleCats.Average(x => x.Age);
            Console.WriteLine("The avergae age of female cats(Kittens) is: {0}", femaleCats);

            var maleCats = allMaleCats.Average(x => x.Age);
            Console.WriteLine("The avergae age of male cats(Tomcats) is: {0}", maleCats);
            Console.WriteLine(randomCat1.MakeSound());
        }
Exemple #12
0
        private static void CreateAnimal(string animalType)
        {
            string[] input  = Console.ReadLine().Split();
            string   name   = input[0];
            int      age    = int.Parse(input[1]);
            string   gender = input[2];

            switch (animalType)
            {
            case "Dog":
                Dog dog = new Dog(name, age, gender);
                Console.WriteLine(dog);
                dog.ProduceSound();
                break;

            case "Cat":
                Cat cat = new Cat(name, age, gender);
                Console.WriteLine(cat);
                cat.ProduceSound();
                break;

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

            case "Kitten":
                Kitten kitten = new Kitten(name, age, gender);
                Console.WriteLine(kitten);
                kitten.ProduceSound();
                break;

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

            default:
                throw new ArgumentException(Animal.ERROR_MESSAGE);
            }
        }
Exemple #13
0
        public static void CreateAnimals(List <Animals> list, string type)
        {
            var data = Console.ReadLine()
                       .Split(" ", StringSplitOptions.RemoveEmptyEntries)
                       .ToArray();
            var name   = data[0];
            var age    = int.Parse(data[1]);
            var gender = string.Empty;

            if (data.Length == 3)
            {
                gender = data[2];
            }
            if (type == "Cat")
            {
                Cat cat = new Cat(name, age, gender);
                list.Add(cat);
            }
            else if (type == "Dog")
            {
                Dog dog = new Dog(name, age, gender);
                list.Add(dog);
            }
            else if (type == "Frog")
            {
                Frog frog = new Frog(name, age, gender);
                list.Add(frog);
            }
            else if (type == "Kitten")
            {
                Kitten kitten = new Kitten(name, age);
                list.Add(kitten);
            }
            else if (type == "Tomcat")
            {
                Tomcat tomcat = new Tomcat(name, age);
                list.Add(tomcat);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }
        }
Exemple #14
0
        static void Main()
        {
            Cat someCat  = new Cat("Kotka", 10);
            Cat someCat1 = new Cat("Kotka1", 5);

            Tomcat tom  = new Tomcat("Tom", 4, Gender.Male);
            Tomcat tom1 = new Tomcat("Tom1", 3, Gender.Male);

            Kitten maca  = new Kitten("Maca", 2, Gender.Female);
            Kitten maca1 = new Kitten("Maca1", 6, Gender.Female);

            Dog sharo  = new Dog("Sharo", 3);
            Dog sharo1 = new Dog("Sharo1", 1);

            Frog frogy  = new Frog("Frogy", 8);
            Frog frogy1 = new Frog("Frogy1", 2);

            Console.WriteLine(someCat);
            Console.WriteLine(tom);
            Console.WriteLine(maca);
            Console.WriteLine(sharo);
            Console.WriteLine(frogy);

            IList <Animal> allAnimal = new List <Animal>();

            allAnimal.Add(someCat);
            allAnimal.Add(someCat1);
            allAnimal.Add(tom);
            allAnimal.Add(tom1);
            allAnimal.Add(maca);
            allAnimal.Add(maca1);
            allAnimal.Add(sharo);
            allAnimal.Add(sharo1);
            allAnimal.Add(frogy);
            allAnimal.Add(frogy1);

            foreach (var kind in allAnimal.GroupBy(x => x.GetType().Name))
            {
                double averageAge = kind.Select(x => x.Age).Average();
                Console.WriteLine("Animal : {0}, AverageAge: {1}", kind.Key, averageAge);
            }
        }
Exemple #15
0
        private Animal GetAnimal(string type, string[] animalArgs)
        {
            string name = animalArgs[0];
            int    age  = int.Parse(animalArgs[1]);

            string gender = null;

            if (animalArgs.Length >= 3)
            {
                gender = animalArgs[2];
            }

            Animal animal = null;

            if (type == "Dog")
            {
                animal = new Dog(name, age, gender);
            }
            else if (type == "Cat")
            {
                animal = new Cat(name, age, gender);
            }
            else if (type == "Frog")
            {
                animal = new Frog(name, age, gender);
            }
            else if (type == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (type == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }


            return(animal);
        }
        private Animal GetAnimal(string type, string[] animalArg)
        {
            string name   = animalArg[0];
            int    age    = int.Parse(animalArg[1]);
            string gender = null;

            if (animalArg.Length == 3)
            {
                gender = animalArg[2];
            }

            Animal animal = null;

            switch (type)
            {
            case "Dog":
                animal = new Dog(name, age, gender);
                break;

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

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

            case "Tomacat":
                animal = new Tomcat(name, age);
                break;

            case "Kitten":
                animal = new Kitten(name, age);
                break;

            default:
                throw new ArgumentException("Invalid input!");
            }

            return(animal);
        }
Exemple #17
0
        public static void Main(string[] args)
        {
            string command = Console.ReadLine();

            while (command != "Beast!")
            {
                string[] animalInfo = Console.ReadLine().Split();

                string animalType   = command;
                string animalName   = animalInfo[0];
                int    animalAge    = int.Parse(animalInfo[1]);
                string animalGender = animalInfo[2];

                Animal animal = null;

                if (animalType == "Cat")
                {
                    animal = new Cat(animalName, animalAge, animalGender);
                }
                else if (animalType == "Dog")
                {
                    animal = new Dog(animalName, animalAge, animalGender);
                }
                else if (animalType == "Frog")
                {
                    animal = new Frog(animalName, animalAge, animalGender);
                }
                else if (animalType == "Kittens")
                {
                    animal = new Kitten(animalName, animalAge);
                }
                else if (animalType == "Tomcat")
                {
                    animal = new Tomcat(animalName, animalAge);
                }

                Console.WriteLine(animal.ToString());

                command = Console.ReadLine();
            }
        }
        static void Main()
        {
            Cat someCat = new Cat("Kotka", 10);
            Cat someCat1 = new Cat("Kotka1", 5);

            Tomcat tom = new Tomcat("Tom", 4, Gender.Male);
            Tomcat tom1 = new Tomcat("Tom1", 3, Gender.Male);

            Kitten maca = new Kitten("Maca",2,Gender.Female);
            Kitten maca1 = new Kitten("Maca1", 6, Gender.Female);

            Dog sharo = new Dog("Sharo", 3);
            Dog sharo1 = new Dog("Sharo1", 1);

            Frog frogy = new Frog("Frogy", 8);
            Frog frogy1 = new Frog("Frogy1", 2);

            Console.WriteLine(someCat);
            Console.WriteLine(tom);
            Console.WriteLine( maca);
            Console.WriteLine(sharo);
            Console.WriteLine(frogy);

            IList<Animal> allAnimal = new List<Animal>();
            allAnimal.Add(someCat);
            allAnimal.Add(someCat1);
            allAnimal.Add(tom);
            allAnimal.Add(tom1);
            allAnimal.Add(maca);
            allAnimal.Add(maca1);
            allAnimal.Add(sharo);
            allAnimal.Add(sharo1);
            allAnimal.Add(frogy);
            allAnimal.Add(frogy1);

            foreach (var kind in allAnimal.GroupBy(x => x.GetType().Name))
            {
                double averageAge = kind.Select(x => x.Age).Average();
                Console.WriteLine("Animal : {0}, AverageAge: {1}", kind.Key, averageAge);
            }
        }
Exemple #19
0
        private static void ReadInputs(string animalType, List <Animal> animals)
        {
            var    inputAnimal = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
            var    name        = inputAnimal[0];
            var    age         = int.Parse(inputAnimal[1]);
            string gender      = null;

            if (inputAnimal.Length == 3)
            {
                gender = inputAnimal[2];
            }
            switch (animalType)
            {
            case "Dog":
                var dog = new Dog(name, age, gender);
                animals.Add(dog);
                break;

            case "Cat":
                var cat = new Cat(name, age, gender);
                animals.Add(cat);
                break;

            case "Frog":
                var frog = new Frog(name, age, gender);
                animals.Add(frog);
                break;

            case "Tomcat":
                var tomcat = new Tomcat(name, age);
                animals.Add(tomcat);
                break;

            case "Kitten":
                var kittens = new Kitten(name, age);
                animals.Add(kittens);
                break;

            default: throw new ArgumentException($"Invalid input!");
            }
        }
Exemple #20
0
        static void Main()
        {
            Random rand = new Random();
            Dog[] dogs = new Dog[5];
            Frog[] frogs = new Frog[5];
            Cat[] cats = new Cat[5];
            Kitten[] kittens = new Kitten[5];
            Tomcat[] tomcats = new Tomcat[5];
            for (int i = 0; i < 5; i++)
            {
                int age=rand.Next(0,30);
                Dog dog = new Dog(age, Convert.ToString((char)(age + 65)), "male");
                dogs[i] = dog;

                age = rand.Next(0, 30);
                Frog frog = new Frog(age, Convert.ToString((char)(age + 65)), "male");
                frogs[i] = frog;

                age = rand.Next(0, 30);
                Kitten kiten = new Kitten(age, Convert.ToString((char)(age + 65)), "male");
                kittens[i] = kiten;

                age = rand.Next(0, 30);
                Cat cat = new Cat(age, Convert.ToString((char)(age + 65)), "male");
                cats[i] = cat;

                age = rand.Next(0, 30);
                Tomcat tomcat = new Tomcat(age, Convert.ToString((char)(age + 65)), "male");
                tomcats[i] = tomcat;
            }
            Print(dogs);
            Print(cats);
            Print(kittens);
            Print(frogs);
            Print(tomcats);
            Console.WriteLine("The average age of the dogs is {0}", AverageAge(dogs));
            Console.WriteLine("The average age of the cats is {0}", AverageAge(cats));
            Console.WriteLine("The average age of the kittens is {0}", AverageAge(kittens));
            Console.WriteLine("The average age of the frogs is {0}", AverageAge(frogs));
            Console.WriteLine("The average age of the tomcats is {0}", AverageAge(tomcats));
        }
Exemple #21
0
        public static void Main(string[] args)
        {
            var animalType = Console.ReadLine();

            while (animalType != "Beast!")
            {
                var parameters = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                var name       = parameters[0];
                var age        = int.Parse(parameters[1]);
                var gender     = parameters[2];

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

                case "Cat":
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat);
                    break;

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

                case "Kitten":
                    Kitten kitten = new Kitten(name, age);
                    Console.WriteLine(kitten);
                    break;

                case "Frog":
                    Frog frog = new Frog(name, age, gender);
                    Console.WriteLine(frog);
                    break;
                }
                animalType = Console.ReadLine();
            }
        }
Exemple #22
0
        static void Main(string[] args)
        {
            Animal[] animals = new Animal[5];
            animals[0]=new Cat("Mac",10,Gender.Male);
            animals[1]=new Kitten("Max",2);
            animals[2]=new Kitten("Missi",1);
            animals[3]=new Frog("Kermit",3,Gender.Male);
            animals[4]=new Frog("Tom",5,Gender.Male);
            animals.ToList();

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

             animals.GroupBy(t=>t.GetType().Name)
                .Select(group=>new {AnimalName=group.Key,AvarageAge=group.Average(a=>a.Age)})
                .ToList()
                .ForEach(group=>Console.WriteLine("{0}{1}",group.AnimalName,group.AvarageAge));
        }
Exemple #23
0
        public static void Main(string[] args)
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "Beast!")
                {
                    break;
                }

                string[] arguments = Console.ReadLine().Split();
                string   name      = arguments[0];
                int      age       = int.Parse(arguments[1]);
                string   gender    = arguments[2];

                Animal animal;
                if (input == "Dog")
                {
                    animal = new Dog(name, age, gender);
                }
                else if (input == "Cat")
                {
                    animal = new Cat(name, age, gender);
                }
                else if (input == "Frog")
                {
                    animal = new Frog(name, age, gender);
                }
                else if (input == "Kitten")
                {
                    animal = new Kitten(name, age);
                }
                else // input == "Tomcat"
                {
                    animal = new Tomcat(name, age);
                }

                Console.WriteLine(animal);
            }
        }
Exemple #24
0
        public static void Main(string[] args)
        {
            string command;

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

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

                case "Cat":
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat);
                    break;

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

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

                case "Kitten":
                    Kitten kitten = new Kitten(name, age);
                    Console.WriteLine(kitten);
                    break;
                }
            }
        }
Exemple #25
0
        private Animal GetAnimal(string type, string[] command)
        {
            string name = command[0];
            int    age  = int.Parse(command[1]);


            string gender = null;

            gender = GetGender(command, gender);



            if (type == "Dog")
            {
                animal = new Dog(name, age, gender);
            }
            else if (type == "Cat")
            {
                animal = new Cat(name, age, gender);
            }
            else if (type == "Frog")
            {
                animal = new Frog(name, age, gender);
            }
            else if (type == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (type == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }

            return(animal);
        }
Exemple #26
0
        private static Animal CreateAnimal(string animalType, string[] animalInfo)
        {
            Animal animal;
            string name   = animalInfo[0];
            int    age    = int.Parse(animalInfo[1]);
            string gender = null;

            if (animalInfo.Length >= 3)
            {
                gender = animalInfo[2];
            }

            if (animalType == "Cat")
            {
                animal = new Cat(name, age, gender);
            }
            else if (animalType == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (animalType == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else if (animalType == "Dog")
            {
                animal = new Dog(name, age, gender);
            }
            else if (animalType == "Frog")
            {
                animal = new Frog(name, age, gender);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }

            return(animal);
        }
        public static void Main()
        {
            var animals = new List <Animal>();

            string command = string.Empty;

            while ((command = Console.ReadLine()) != "Beast!")
            {
                var atributes = Console.ReadLine().Split();

                string name   = atributes[0];
                int    age    = int.Parse(atributes[1]);
                string gender = atributes[2];

                switch (command)
                {
                case "Animal": var animal = new Animal(name, age, gender); AddAnimal(animals, animal); break;

                case "Cat": var cat = new Cat(name, age, gender); AddAnimal(animals, cat); break;

                case "Dog": var dog = new Dog(name, age, gender); AddAnimal(animals, dog); break;

                case "Frog": var frog = new Frog(name, age, gender); AddAnimal(animals, frog); break;

                case "Kitten": var kitten = new Kitten(name, age, gender); AddAnimal(animals, kitten); break;

                case "Tomcat": var tomcat = new Tomcat(name, age, gender); AddAnimal(animals, tomcat); break;

                default:
                    break;
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal.GetType().Name);
                Console.WriteLine($"{animal.Name} {animal.Age} {animal.Gender}");
                Console.WriteLine(animal.ProduceSound());
            }
        }
Exemple #28
0
        private static void Main()
        {
            Dog barry = new Dog("Barry", 13, Gender.Male);
            Dog jessica = new Dog("Jessica", 6, Gender.Female);
            Dog milo = new Dog("Milo", 17, Gender.Male);

            Kitten dona = new Kitten("Dona", 6);
            Kitten sweety = new Kitten("Sweety", 0);
            Kitten sisi = new Kitten("Sisi", 4);

            Tomcat zorro = new Tomcat("Zorro", 7);
            Tomcat lucky = new Tomcat("Lucky", 7);
            Tomcat tom = new Tomcat("Tom", 7);

            Frog randy = new Frog("Randy", 3, Gender.Male);
            Frog bob = new Frog("Bob", 6, Gender.Male);
            Frog sw = new Frog("Sw", 2, Gender.Male);

            IList<Animal> allAnimal = new List<Animal>();
            allAnimal.Add(barry);
            allAnimal.Add(jessica);
            allAnimal.Add(milo);
            allAnimal.Add(dona);
            allAnimal.Add(sweety);
            allAnimal.Add(sisi);
            allAnimal.Add(zorro);
            allAnimal.Add(lucky);
            allAnimal.Add(tom);
            allAnimal.Add(randy);
            allAnimal.Add(bob);
            allAnimal.Add(sw);

            foreach (var kind in allAnimal.GroupBy(x => x.GetType().Name))
            {
                double averageAge = kind.Select(x => x.Age).Average();
                Console.WriteLine("Animal : {0}, average age are: {1:F2}", kind.Key, averageAge);
            }
        }
        static void Main()
        {
            Animal jaba     = new Frog("baba jaba", 1, Genders.Female);
            Animal kekerica = new Frog("kikirica", 3, Genders.Female);

            Animal sharo = new Dog("sharo", 3, Genders.Male);
            Animal sara  = new Dog("sara", 2, Genders.Female);
            Animal oldy  = new Dog("oldy", 12, Genders.Male);

            Animal puhi        = new Kitten("puhi", 2);
            Animal tommy       = new Tomcat("tommy", 4);
            Animal mouseKiller = new Cat("mousy", 5, Genders.Male);

            Animal[] animals = new Animal[]
            {
                jaba,
                kekerica,
                sharo,
                sara,
                puhi,
                tommy,
                mouseKiller,
                oldy
            };

            var groupedAnimals = from animal in animals
                                 group animal by(animal is Cat)  ? typeof(Cat) : animal.GetType() into animalLine
                                 select new { GroupName = animalLine.Key, AverageAge = animalLine.ToList().Average(an => an.Age) };

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

            puhi.ProduceSound();
            oldy.ProduceSound();
            jaba.ProduceSound();
        }
Exemple #30
0
        static void Main()
        {
            Animals frog  = new Frog("Ginka", 1, "Female");
            Animals frog2 = new Frog("Gincho", 3, "Male");

            Animals sharo = new Dogs("Sharo", 3, "Male");
            Animals luki  = new Dogs("Luki", 2, "Female");
            Animals bu    = new Dogs("Bu", 10, "Male");

            Animals puhi = new Kitten("puhi", 2);
            Animals tom  = new Tomcat("Tom", 4);
            Animals muke = new Cat("Muke", 5, "Male");

            frog.ProduceSound();
            Console.WriteLine();

            List <Animals> animals = new List <Animals>()
            {
                frog,
                frog2,
                sharo,
                luki,
                puhi,
                tom,
                muke,
                bu
            };

            var listedAnimals = from anim in animals
                                group anim by anim.GetType() into groupAnim
                                select new { GroupName = groupAnim.Key, AverageAge = groupAnim.ToList().Average(an => an.Age) };

            foreach (var animal in listedAnimals)
            {
                Console.WriteLine("{0} - Average age: {1:N2}", animal.GroupName.Name, animal.AverageAge);
            }
        }
Exemple #31
0
        static void Main()
        {
            Kitten kitty = new Kitten("Pufi", 1); 
            Tomcat tomas = new Tomcat("Marko", 2);
            Dog sharo = new Dog("Kir4o", 3, SexEnum.male); 
            Frog squeek = new Frog("Kimi", 1, SexEnum.female); 
            kitty.MakeSomeNoise(); 
            tomas.MakeSomeNoise(); 
            sharo.MakeSomeNoise(); 
            squeek.MakeSomeNoise();
            
            Console.WriteLine(tomas.Gender); 
            Console.WriteLine(kitty.Gender); 

            Animal[] animals = 
            { 
                new Kitten("Koti", 2), 
                new Tomcat("Rocky", 4), 
                new Dog("Alex", 5, SexEnum.female), 
                new Frog("Siti", 2, SexEnum.female), 
                new Kitten("Kara", 6), 
                new Tomcat("Suzana", 2), 
                new Dog("Porsche", 7, SexEnum.female),
                new Frog("Kara", 10, SexEnum.female),
                new Kitten("Tonchi", 8), 
                new Tomcat("Jony", 8), 
                new Dog("Elma", 9, SexEnum.female), 
                new Frog("Liki", 5, SexEnum.female), 
            };

            var ordered = animals.GroupBy(x => x.GetType()); 
            Console.WriteLine(); 
            foreach (var animal in ordered) 
            { 
                Console.WriteLine("Average age of {0} is {1:F2}.", animal.Key.Name, animal.Average(x => x.Age));
            }
        }
Exemple #32
0
        public static void Main()
        {
            Animal jaba = new Frog("baba jaba", 1, Genders.Female);
            Animal kekerica = new Frog("kekerica", 3, Genders.Female);

            Animal sharo = new Dog("sharo", 3, Genders.Male);
            Animal sara = new Dog("sara", 2, Genders.Female);
            Animal oldy = new Dog("oldy", 12, Genders.Male);

            Animal puhi = new Kitten("puhi", 2);
            Animal tommy = new Tomcat("tommy", 4);
            Animal mouseKiller = new Cat("mousy", 5, Genders.Male);

            List<Animal> animals = new List<Animal>()
            {
                jaba,
                kekerica,
                sharo,
                sara,
                puhi,
                tommy,
                mouseKiller,
                oldy
            };
            var groupedAnimals = from animal in animals
                                 group animal by (animal is Cat) ? typeof(Cat) : animal.GetType() into g
                                 select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) };

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

            puhi.ProduceSound();
            oldy.ProduceSound();
            jaba.ProduceSound();
        }
        private void CreateAnimal()
        {
            try
            {
                switch (this.animalType)
                {
                case "Cat":
                    Cat cat = new Cat(animalName, animalAge, animalGender);
                    animals.Add(cat);
                    break;

                case "Dog":
                    Dog dog = new Dog(animalName, animalAge, animalGender);
                    animals.Add(dog);
                    break;

                case "Frog":
                    Frog frog = new Frog(animalName, animalAge, animalGender);
                    animals.Add(frog);
                    break;

                case "Kitten":
                    Kitten kitten = new Kitten(animalName, animalAge);
                    animals.Add(kitten);
                    break;

                case "Tomcat":
                    Tomcat tomcat = new Tomcat(animalName, animalAge);
                    animals.Add(tomcat);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #34
0
        private static Animal GetAnimal(string type, string[] arguments)
        {
            Animal NewAnimal = null;
            string name      = arguments[0];
            int    age       = int.Parse(arguments[1]);
            string gender    = string.Empty;

            if (arguments.Length == 3)
            {
                gender = arguments[2];
            }
            if (type == "Cat")
            {
                NewAnimal = new Cat(name, age, gender);
            }
            else if (type == "Dog")
            {
                NewAnimal = new Dog(name, age, gender);
            }
            else if (type == "Frog")
            {
                NewAnimal = new Frog(name, age, gender);
            }
            else if (type == "Kitten")
            {
                NewAnimal = new Kitten(name, age);
            }
            else if (type == "Tomcat")
            {
                NewAnimal = new Tomcat(name, age);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }
            return(NewAnimal);
        }
        private Animal GetAnimal(string type, string[] animalArgs) //подаваме типа и аргументите, връща ме ги през един и същи метод
                                                                   //защото всички инпрементират Animal
        {
            var    name   = animalArgs[0];
            var    age    = int.Parse(animalArgs[1]);
            Animal animal = null;

            string gender = GetGender(animalArgs);

            //gender
            if (type == "Dog")
            {
                animal = new Dog(name, age, gender);
            }
            else if (type == "Cat")
            {
                animal = new Cat(name, age, gender);
            }
            else if (type == "Frog")
            {
                animal = new Frog(name, age, gender);
            }
            else if (type == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (type == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }
            return(animal);
        }
Exemple #36
0
        private Animal GetAnimal(string input, string[] animalArgs)
        {
            Animal animal;

            string animalType   = input;
            string animalName   = animalArgs[0];
            int    animalAge    = int.Parse(animalArgs[1]);
            string animalGender = GetGender(animalArgs);

            if (animalType == "Dog")
            {
                animal = new Dog(animalName, animalAge, animalGender);
            }
            else if (animalType == "Cat")
            {
                animal = new Cat(animalName, animalAge, animalGender);
            }
            else if (animalType == "Frog")
            {
                animal = new Frog(animalName, animalAge, animalGender);
            }
            else if (animalType == "Kitten")
            {
                animal = new Kitten(animalName, animalAge);
            }
            else if (animalType == "Tomcat")
            {
                animal = new Tomcat(animalName, animalAge);
            }
            else
            {
                throw new ArgumentException("Invalid input");
            }

            return(animal);
        }
Exemple #37
0
        public static void Main(string[] args)
        {
            string        cmd  = Console.ReadLine();
            List <Animal> cage = new List <Animal>();


            while (cmd != "Beast!")
            {
                var    data   = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string name   = data[0];
                int    age    = int.Parse(data[1]);
                string gender = data[2];

                if (age >= 0 && name != string.Empty & name != " " && gender != string.Empty && gender != " ")
                {
                    switch (cmd)
                    {
                    case "Dog":
                    {
                        Dog dog = new Dog(name, age, gender);
                        cage.Add(dog);
                        break;
                    }

                    case "Cat":
                    {
                        Cat cat = new Cat(name, age, gender);
                        cage.Add(cat);
                        break;
                    }

                    case "Frog":
                    {
                        Frog frog = new Frog(name, age, gender);
                        cage.Add(frog);
                        break;
                    }

                    case "Kitten":
                    {
                        Kitten kitten = new Kitten(name, age);
                        cage.Add(kitten);
                        break;
                    }

                    case "Tomcat":
                    {
                        Tomcat tomcat = new Tomcat(name, age);
                        cage.Add(tomcat);
                        break;
                    }

                    case "Animal":
                    {
                        Animal animal = new Animal(name, age, gender);
                        cage.Add(animal);
                        break;
                    }

                    default:
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Invalid Input!");
                }
                cmd = Console.ReadLine();
            }

            foreach (var animal in cage)
            {
                Console.WriteLine(animal.ToString());
                animal.ProduceSound();
            }
        }
        public void Run()
        {
            string animal = Console.ReadLine();

            string[] properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

            while (true)
            {
                string name   = properties[0];
                int    age    = int.Parse(properties[1]);
                string gender = string.Empty;

                if (age <= 0)
                {
                    Console.WriteLine("Invalid input!");


                    animal = Console.ReadLine();

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

                    properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                    continue;
                }

                if (properties.Length > 2)
                {
                    gender = properties[2];
                }

                switch (animal)
                {
                case "Cat":
                {
                    Console.WriteLine(animal);
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat);
                    Console.WriteLine(cat.ProduceSound());
                }
                break;

                case "Dog":
                {
                    Dog dog = new Dog(name, age, gender);
                    Console.WriteLine(animal);
                    Console.WriteLine(dog);
                    Console.WriteLine(dog.ProduceSound());
                }
                break;

                case "Frog":
                {
                    Console.WriteLine(animal);
                    Frog frog = new Frog(name, age, gender);
                    Console.WriteLine(frog);
                    Console.WriteLine(frog.ProduceSound());
                }
                break;

                case "Tomcat":
                {
                    Console.WriteLine(animal);
                    Tomcat tomcat = new Tomcat(name, age);
                    Console.WriteLine(tomcat);
                    Console.WriteLine(tomcat.ProduceSound());
                }
                break;

                case "Kitten":
                {
                    Console.WriteLine(animal);
                    Kitten kitten = new Kitten(name, age);
                    Console.WriteLine(kitten);
                    Console.WriteLine(kitten.ProduceSound());
                }
                break;
                }
                animal = Console.ReadLine();

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

                properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
            }
        }
Exemple #39
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();


            while (true)
            {
                string firstLine = Console.ReadLine();

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

                string[] secondLine = Console.ReadLine()
                                      .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


                string name = secondLine[0];
                int    age;
                if (!int.TryParse(secondLine[1], out age))
                {
                    throw new ArgumentException("Invalid input!");
                }

                string gender = secondLine[2];

                try
                {
                    switch (firstLine)
                    {
                    case "Dog":
                        Dog dog = new Dog(name, age, gender);
                        Console.WriteLine("Dog");
                        Console.WriteLine(dog.GetResult());
                        break;

                    case "Cat":
                        Cat cat = new Cat(name, age, gender);
                        Console.WriteLine("Cat");
                        Console.WriteLine(cat.GetResult());
                        break;

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

                    case "Kitten":
                        Kitten kitten = new Kitten(name, age);
                        Console.WriteLine("Kitten");
                        Console.WriteLine(kitten.GetResult());
                        break;

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

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemple #40
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                try
                {
                    var input = Console.ReadLine();
                    if (input.Equals("Beast!"))
                    {
                        break;
                    }
                    var animalInput = Console.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    int age;
                    if (!int.TryParse(animalInput[1], out age))
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    switch (input)
                    {
                    case "Cat":
                        Cat cat = new Cat(animalInput[0], int.Parse(animalInput[1]), animalInput[2]);
                        Console.WriteLine(cat);
                        break;

                    case "Dog":
                        Dog dog = new Dog(animalInput[0], int.Parse(animalInput[1]), animalInput[2]);
                        Console.WriteLine(dog);
                        break;

                    case "Frog":
                        Frog frog = new Frog(animalInput[0], int.Parse(animalInput[1]), animalInput[2]);
                        Console.WriteLine(frog);
                        break;

                    case "Kitten":
                        Kitten kitten = new Kitten(animalInput[0], int.Parse(animalInput[1]));
                        Console.WriteLine(kitten);
                        break;

                    case "Tomcat":
                        Tomcat tomcat = new Tomcat(animalInput[0], int.Parse(animalInput[1]));
                        Console.WriteLine(tomcat);
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }

                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid input!");
                }
            }
        }
Exemple #41
0
        static void Main(string[] args)
        {
            try
            {
                Frog goshko = new Frog("Goshko", 10, "male");
                Frog peshko = new Frog("Peshko", 3, "male");
                Frog mariika = new Frog("Mariika", 7, "female");

                Dog ivancho = new Dog("Ivancho", 11, "male");
                Dog sashko = new Dog("Sashko", 1, "male");
                Dog magdalena = new Dog("Magdalena", 2, "female");

                Kitten tania = new Kitten("Tania", 3);
                Kitten penka = new Kitten("Penka", 4);
                Kitten goshka = new Kitten("Goshka", 10);

                Tomcat kristian = new Tomcat("Kristian", 1);
                Tomcat pesho = new Tomcat("Pesho", 10);
                Tomcat stanislav = new Tomcat("Stanislav", 5);

                IList<Animal> animals = new List<Animal>
                {
                    goshko, peshko, mariika, ivancho, sashko, magdalena,
                    tania, penka, goshka, kristian, pesho, stanislav
                };

                goshko.ProduceSound();
                sashko.ProduceSound();
                tania.ProduceSound();
                kristian.ProduceSound();

                double catsAverageAge = animals
                    .Where(animal => animal is Cat)
                    .Average(cat => cat.Age);

                double dogsAverageAge = animals
                    .Where(animal => animal is Dog)
                    .Average(dog => dog.Age);

                double frogsAverageAge = animals
                    .Where(animal => animal is Frog)
                    .Average(frog => frog.Age);

                Console.WriteLine("Frogs average age is: {0:F2}", frogsAverageAge);
                Console.WriteLine("Dogs average age is: {0:F2}", dogsAverageAge);
                Console.WriteLine("Cats average age is: {0:F2}", catsAverageAge);

            }
            catch (ArgumentOutOfRangeException ae)
            {
                Console.WriteLine(ae.Message);
            }
            catch (ArgumentNullException ae)
            {
                Console.WriteLine(ae.Message);
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            for (; ;)
            {
                try
                {
                    string input = Console.ReadLine();

                    if (input == "Beast!")
                    {
                        goto Foo;
                    }

                    string[] tokens = Console.ReadLine().Split();
                    string   name   = tokens[0];
                    int      age    = int.Parse(tokens[1]);
                    string   gender = tokens[2];

                    switch (input)
                    {
                    case "Cat":
                    {
                        Cat cat = new Cat(name, age, gender);
                        animals.Add(cat);
                        break;
                    }

                    case "Dog":
                    {
                        Dog dog = new Dog(name, age, gender);
                        animals.Add(dog);
                        break;
                    }

                    case "Frog":
                    {
                        Frog frog = new Frog(name, age, gender);
                        animals.Add(frog);
                        break;
                    }

                    case "Kitten":
                    {
                        Kitten kitten = new Kitten(name, age);
                        animals.Add(kitten);
                        break;
                    }

                    case "Tomcat":
                    {
                        Tomcat tomcat = new Tomcat(name, age);
                        animals.Add(tomcat);
                        break;
                    }

                    default:
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
            }

Foo:
            PrintAnimals(animals);
        }
        static void Main(string[] args)
        {
            Dog dog = new Dog("Pesho", 3, Gender.Male);
            Console.WriteLine(dog);
            dog.FetchStick();

            Console.WriteLine();

            Frog frog = new Frog("Peter", 33, Gender.Male);
            Console.WriteLine(frog);
            frog.Jump();

            Console.WriteLine();

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

            Console.WriteLine();

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

            Console.WriteLine();

            // create an array of Animals
            Animal[] animals = new Animal[]
            {
                new Dog("Ares",3, Gender.Male),
                new Frog("Mitko", 1, Gender.Female),
                new Kitten("Mac", 1),
                new Tomcat("Cotka",2),
                new Dog("Saires", 4, Gender.Male),
                new Frog("Stilian", 1, Gender.Female),
                new Kitten("Ioio", 2),
                new Tomcat("Antony",6),
                new Dog("Roy",2, Gender.Male),
                new Frog("Angelina", 6, Gender.Female),
                new Kitten("Alex", 1),
                new Tomcat("Mecjo",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));
            }
        }
        public static void Main(string[] args)
        {
            string type = string.Empty;

            Animal animal;

            while ((type = Console.ReadLine()) != "Beast!")
            {
                string[] animalInfo = Console
                                      .ReadLine()
                                      .Split(" ")
                                      .ToArray();

                string name   = animalInfo[0];
                int    age    = int.Parse(animalInfo[1]);
                string gender = animalInfo[2];

                switch (type)
                {
                case "Dog":
                    try
                    {
                        animal = new Dog(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case "Cat":
                    try
                    {
                        animal = new Cat(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case "Frog":
                    try
                    {
                        animal = new Frog(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case "Kittens":
                    try
                    {
                        animal = new Kitten(name, age);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case "Tomcat":
                    try
                    {
                        animal = new Tomcat(name, age);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;
                }
            }
        }
Exemple #45
0
        static void Main(string[] args)
        {
            string type;

            Animal animal;

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

                if (type == "Cat")
                {
                    try
                    {
                        animal = new Cat(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (type == "Dog")
                {
                    try
                    {
                        animal = new Dog(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (type == "Frog")
                {
                    try
                    {
                        animal = new Frog(name, age, gender);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (type == "Tomcat")
                {
                    try
                    {
                        animal = new Tomcat(name, age);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (type == "Kitten")
                {
                    try
                    {
                        animal = new Kitten(name, age);
                        Console.WriteLine(animal.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Exemple #46
0
        public static void Main()
        {
            string input = Console.ReadLine();

            while (true)
            {
                try
                {
                    var data = Console.ReadLine()
                               .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                               .ToArray();

                    string name = data[0];
                    int    age;
                    if (!int.TryParse(data[1], out age))
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    string gender = data[2];

                    switch (input)
                    {
                    case "Dog": Dog dog = new Dog(name, age, gender);
                        Console.WriteLine("Dog");
                        Console.WriteLine(dog.GetResult());
                        break;

                    case "Cat":
                        Cat cat = new Cat(name, age, gender);
                        Console.WriteLine("Cat");
                        Console.WriteLine(cat.GetResult());
                        break;

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

                    case "Kitten":
                        Kitten voti = new Kitten(name, age);
                        Console.WriteLine("Kitten");
                        Console.WriteLine(voti.GetResult());
                        break;

                    case "Tomcat":
                        Tomcat tom = new Tomcat(name, age);
                        Console.WriteLine("Tomcat");
                        Console.WriteLine(tom.GetResult());
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                input = Console.ReadLine();
            }
        }
Exemple #47
0
        public static void Main(string[] args)
        {
            List <Animal> list = new List <Animal>();

            string input = Console.ReadLine();

            while (input != "Beast!")
            {
                var command = Console.ReadLine().Split().ToArray();

                if (input == "Cat")
                {
                    if (command.Length == 3)
                    {
                        string name   = command[0];
                        int    age    = int.Parse(command[1]);
                        string gender = command[2];

                        if (age < 0)
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else if (gender != "Male" && gender != "Female")
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else
                        {
                            Cat cat = new Cat(name, age, gender);
                            list.Add(cat);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input!");
                    }
                }
                else if (input == "Dog")
                {
                    if (command.Length == 3)
                    {
                        string name   = command[0];
                        int    age    = int.Parse(command[1]);
                        string gender = command[2];

                        if (age < 0)
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else if (gender != "Male" && gender != "Female")
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else
                        {
                            Dog dog = new Dog(name, age, gender);
                            list.Add(dog);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input!");
                    }
                }
                else if (input == "Frog")
                {
                    if (command.Length == 3)
                    {
                        string name   = command[0];
                        int    age    = int.Parse(command[1]);
                        string gender = command[2];

                        if (age < 0)
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else if (gender != "Male" && gender != "Female")
                        {
                            Console.WriteLine("Invalid input!");
                        }
                        else
                        {
                            Frog frog = new Frog(name, age, gender);
                            list.Add(frog);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input!");
                    }
                }
                else if (input == "Tomcat")
                {
                    if (command.Length >= 2)
                    {
                        string name = command[0];
                        int    age  = int.Parse(command[1]);


                        if (age < 0)
                        {
                            Console.WriteLine("Invalid input!");
                        }

                        else
                        {
                            Tomcat cat = new Tomcat(name, age);
                            list.Add(cat);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input!");
                    }
                }
                else if (input == "Kitten")
                {
                    if (command.Length >= 2)
                    {
                        string name = command[0];
                        int    age  = int.Parse(command[1]);


                        if (age < 0)
                        {
                            Console.WriteLine("Invalid input!");
                        }

                        else
                        {
                            Kitten cat = new Kitten(name, age);
                            list.Add(cat);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input!");
                    }
                }


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

                input = Console.ReadLine();
            }

            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
Exemple #48
0
        public static void Main(string[] args)
        {
            string input = Console.ReadLine();

            List <Animal> animals = new List <Animal>();

            while (input != "Beast!")
            {
                string[] data = Console.ReadLine().Split().ToArray();

                string name   = null;
                int    age    = 0;
                string gender = null;

                try
                {
                    name = data[0];
                    age  = int.Parse(data[1]);
                    if (input != "Tomcat" && input != "Kitten")
                    {
                        gender = data[2];
                    }

                    switch (input)
                    {
                    case "Dog":
                        Dog currDog = new Dog(name, age, gender);
                        animals.Add(currDog);

                        break;

                    case "Frog":
                        Frog currFrog = new Frog(name, age, gender);
                        animals.Add(currFrog);

                        break;

                    case "Cat":
                        Cat currCat = new Cat(name, age, gender);
                        animals.Add(currCat);
                        break;

                    case "Kitten":
                        if (data.Length > 2)
                        {
                            Kitten currKitty = new Kitten(name, age, data[2]);
                            animals.Add(currKitty);
                        }
                        else
                        {
                            Kitten currKitty = new Kitten(name, age);
                            animals.Add(currKitty);
                        }
                        break;

                    case "Tomcat":

                        if (data.Length > 2)
                        {
                            Tomcat currTom = new Tomcat(name, age, data[2]);
                            animals.Add(currTom);
                        }
                        else
                        {
                            Tomcat currTom = new Tomcat(name, age);
                            animals.Add(currTom);
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Invalid input!");
                }

                input = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.GetType().Name);
                Console.WriteLine($"{animal.Name} {animal.Age} {animal.Gender}");
                Console.WriteLine(animal.ProduceSound());
            }
        }
Exemple #49
0
        public static void Main(string[] args)
        {
            string        input   = Console.ReadLine();
            List <Animal> animals = new List <Animal>();


            while (input != "Beast!")
            {
                string   animalType = input;
                string[] animalInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                if (animalInfo.Length == 3)
                {
                    string name   = animalInfo[0];
                    int    age    = int.Parse(animalInfo[1]);
                    string gender = animalInfo[2];

                    if (age < 0 || gender != "Female" && gender != "Male")
                    {
                        Console.WriteLine("Invalid input!");
                        input = Console.ReadLine();
                        continue;
                    }

                    Animal animal = null;

                    switch (animalType)
                    {
                    case "Dog":
                        animal = new Dog(name, age, gender);
                        break;

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

                    case "Tomcat":
                        animal = new Tomcat(name, age);
                        break;

                    case "Kitten":
                        animal = new Kitten(name, age);
                        break;

                    case "Frog":
                        animal = new Frog(name, age, gender);
                        break;
                    }

                    animals.Add(animal);
                }

                input = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
                Console.WriteLine(animal.ProduceSound());
            }
        }
        static void Main(string[] args)
        {
            // Constructors test

            Kitten kity = new Kitten(10);

            Console.WriteLine("Kity's sex is : " + kity.Sex);

            Tomcat tommy = new Tomcat(10);

            Console.WriteLine("Tommy's sex is : " + tommy.Sex);

            Dog rex = new Dog(10,Sex.Male);

            Console.WriteLine("Rex's sex is : " + rex.Sex);
            Console.WriteLine("And Rex is {0} years old", rex.Age);

            Frog gogo = new Frog(100, Sex.Male);
            Console.WriteLine("Gogo the frog is {0} years old: ", gogo.Age);

            // Sounds test
            Console.WriteLine("__________________");
            Console.WriteLine("Frogs sound: ");
            gogo.ProduceSound();
            Console.WriteLine("__________________");
            Console.WriteLine("Tomcats sound: ");
            tommy.ProduceSound();
            Console.WriteLine("__________________");
            Console.WriteLine("Kittens sound: ");
            kity.ProduceSound();
            Console.WriteLine("__________________");
            Console.WriteLine("Dogs sound: ");
            rex.ProduceSound();

            Console.WriteLine();

            Dog[] dogs = new Dog[4]
            {
              new Dog(10,Sex.Male),
              new Dog(12,Sex.Male),
              new Dog(14,Sex.Male),
              new Dog(8,Sex.Male),
            };

            Frog[] frogs = new Frog[4]
            {
                new Frog(4,Sex.Male),
                new Frog(5,Sex.Male),
                new Frog(3,Sex.Male),
                new Frog(10,Sex.Male)

            };

            Kitten[] kities = new Kitten[4]
            {
                new Kitten(10),
                new Kitten(11),
                new Kitten(15),
                new Kitten(18)
            };

            Tomcat[] tomcats = new Tomcat[4]
            {
                new Tomcat(12),
                new Tomcat(15),
                new Tomcat(13),
                new Tomcat(40),
            };

            // Avarage age
            Console.WriteLine(" The avarage age of Tomcat is " + AvarageAge(tomcats));
            Console.WriteLine(" The avarage age of Kitten is " + AvarageAge(kities));
            Console.WriteLine(" The avarage age of Dogs is " + AvarageAge(dogs));
            Console.WriteLine(" The avarage age of Frogs is " + AvarageAge(frogs));

            Console.WriteLine();
        }