Exemple #1
0
        private static string CreateNewSpecificAnimal(string type)
        {
            switch (type)
            {
            case "Cat":
                Cat cat = new Cat(animalName, animalAge, animalGender);
                return(cat.ToString());

            case "Dog":
                Dog dog = new Dog(animalName, animalAge, animalGender);
                return(dog.ToString());

            case "Frog":
                Frog frog = new Frog(animalName, animalAge, animalGender);
                return(frog.ToString());

            case "Kitten":
                Kitten kitten = new Kitten(animalName, animalAge);
                return(kitten.ToString());

            case "Tomcat":
                Tomcat tomcat = new Tomcat(animalName, animalAge);
                return(tomcat.ToString());

            default:
                return(null);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    string animalType = Console.ReadLine();
                    if (animalType == "Beast!")
                    {
                        break;
                    }
                    string[] animalArgs = Console.ReadLine().Split();
                    switch (animalType)
                    {
                    case "Dog":
                        Dog dog = new Dog(animalArgs);
                        dog.GetTypeOfAnimal();
                        Console.WriteLine(dog.ToString());
                        dog.ProduceSound();
                        break;

                    case "Cat":
                        Cat cat = new Cat(animalArgs);
                        cat.GetTypeOfAnimal();
                        Console.WriteLine(cat.ToString());
                        cat.ProduceSound();
                        break;

                    case "Frog":
                        Frog frog = new Frog(animalArgs);
                        frog.GetTypeOfAnimal();
                        Console.WriteLine(frog.ToString());
                        frog.ProduceSound();
                        break;

                    case "Kitten":
                        Kitten kitten = new Kitten(animalArgs);
                        kitten.GetTypeOfAnimal();
                        Console.WriteLine(kitten.ToString());
                        kitten.ProduceSound();
                        break;

                    case "Tomcat":
                        Tomcat tomcat = new Tomcat(animalArgs);
                        tomcat.GetTypeOfAnimal();
                        Console.WriteLine(tomcat.ToString());
                        tomcat.ProduceSound();
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            try
            {
                Cat rijko = new Cat("rijko", 5, "male");
                Console.WriteLine(rijko.ToString());
                rijko.ProduceSound();

                Tomcat darko = new Tomcat("darko", 3);
                Console.WriteLine(darko.ToString());
                darko.ProduceSound();

                Frog kikerana = new Frog("kikerana", 1, "female");
                Console.WriteLine(kikerana.ToString());
                kikerana.ProduceSound();

                Dog sharo = new Dog("sharo", 9);
                Console.WriteLine(sharo.ToString());
                sharo.ProduceSound();

                Animal[] animals = new Animal[] {
                    rijko,
                    darko,
                    kikerana,
                    sharo,
                    new Cat("tom", 2),
                    new Dog("zoro", 9),
                    new Cat("kotka", 4, "male")
                };

                Console.WriteLine();

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

                Console.WriteLine();


                var groupAnimals =
                    from animal in animals
                    group animal by animal.GetType().Name into typeGroup
                    select new
                {
                    typeGroup.Key,
                    AverageValue = Math.Round(typeGroup.Average(i => i.Age), 2)
                };

                foreach (var typeGroup in groupAnimals)
                {
                    Console.WriteLine("{0}s: average age is:{1}", typeGroup.Key, typeGroup.AverageValue);
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemple #4
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Random gen = new Random();


                if (radDog.Checked)
                {
                    Gender randomGender = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);

                    Dog shep = new Dog();

                    shep.name   = Interaction.InputBox("Enter your dog's name.", "Name");
                    shep.color  = Interaction.InputBox("Enter your dog's color.", "Color");
                    shep.weight = Convert.ToDouble(Interaction.InputBox("Enter your dog's weight.", "Weight"));
                    shep.gender = randomGender;

                    lblOut.Text += shep.ToString();
                    shep.MakeNoise();
                }
                else if (radCat.Checked)
                {
                    Cat mittens = new Cat();

                    Gender randomGender = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);

                    mittens.name   = Interaction.InputBox("Enter your cat's name.", "Name");
                    mittens.color  = Interaction.InputBox("Enter your cat's color.", "Color");
                    mittens.weight = Convert.ToDouble(Interaction.InputBox("Enter your cat's weight.", "Weight"));
                    mittens.gender = randomGender;

                    lblOut.Text += mittens.ToString();
                    mittens.MakeNoise();
                }
                else
                {
                    Bird tweetie = new Bird();

                    Gender randomGender = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);

                    tweetie.name   = Interaction.InputBox("Enter your bird's name.", "Name");
                    tweetie.color  = Interaction.InputBox("Enter your bird's color.", "Color");
                    tweetie.weight = Convert.ToDouble(Interaction.InputBox("Enter your bird's weight.", "Weight"));
                    tweetie.gender = randomGender;

                    lblOut.Text += tweetie.ToString();
                    tweetie.MakeNoise();
                }
            }

            catch
            {
                lblOut.Text = "Please enter valid values.";
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            // anim1 = new Animal("Woefie");
            anim2 = new Dog("Pluto");
            anim3 = new SmallDogo("Fifike");

            //MessageBox.Show(anim1.ToString());
            MessageBox.Show(anim2.ToString());
        }
        public static void Main(string[] args)
        {
            string command = Console.ReadLine();

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

                if (tokens.Length == 3 &&
                    char.IsLetter(tokens[0][0]) &&
                    int.TryParse(tokens[1], out int x) &&
                    x >= 0 &&
                    (tokens[2].ToLower() == "male" || tokens[2] == "female"))
                {
                    string typeOfAnimal = command;

                    string name = tokens[0];
                    int    age  = int.Parse(tokens[1]);
                    string sex  = tokens[2];

                    if (typeOfAnimal == "Cat")
                    {
                        Cat cat = new Cat(name, age, sex);
                        Console.WriteLine(cat.ToString());
                    }
                    else if (typeOfAnimal == "Dog")
                    {
                        Dog dog = new Dog(name, age, sex);
                        Console.WriteLine(dog.ToString());
                    }
                    else if (typeOfAnimal == "Frog")
                    {
                        Frog frog = new Frog(name, age, sex);
                        Console.WriteLine(frog.ToString());
                    }
                    else if (typeOfAnimal == "Kitten")
                    {
                        Kitten kitten = new Kitten(name, age);
                        Console.WriteLine(kitten.ToString());
                    }
                    else if (typeOfAnimal == "Tomcat")
                    {
                        Tomcat tomcat = new Tomcat(name, age);
                        Console.WriteLine(tomcat.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input!");
                }

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

            while ((type = Console.ReadLine()) != "Beast!")
            {
                string comand = Console.ReadLine();

                var comandArgs = comand.Split();
                var name       = comandArgs[0];
                var age        = int.Parse(comandArgs[1]);
                var gender     = comandArgs[2];

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

                if (type == "Dog")
                {
                    Dog dog = new Dog(name, age, gender);
                    Console.WriteLine(dog.ToString());
                    dog.ProduceSound();
                }
                else if (type == "Cat")
                {
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat.ToString());
                    cat.ProduceSound();
                }
                else if (type == "Frog")
                {
                    Frog frog = new Frog(name, age, gender);
                    Console.WriteLine(frog.ToString());
                    frog.ProduceSound();
                }
                else if (type == "Kittens")
                {
                    Kitten kit = new Kitten(name, age);
                    Console.WriteLine(kit.ToString());
                    kit.ProduceSound();
                }
                else if (type == "Tomcat")
                {
                    Tomcat tom = new Tomcat(name, age);
                    Console.WriteLine(tom.ToString());
                    tom.ProduceSound();
                }
            }
        }
        static void Main(string[] args)
        {
            var input = Console.ReadLine();

            while (input != "Beast!")
            {
                try
                {
                    var tokens = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    switch (input)
                    {
                    case "Dog":
                        Dog dog = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        Console.WriteLine(dog.ToString());
                        break;

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

                    case "Cat":
                        Cat cat = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        Console.WriteLine(cat.ToString());
                        break;

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

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

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

                input = Console.ReadLine();
            }
        }
Exemple #9
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            Random gen = new Random();

            if (radDog.Checked == true)
            {
                Gender random = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);
                Animal doggo1 = new Dog();

                doggo1.name   = Interaction.InputBox("Enter your dog's name");
                doggo1.color  = Interaction.InputBox("Enter your dog's color");
                doggo1.weight = Convert.ToDouble(Interaction.InputBox("Enter your dog's weight"));
                doggo1.gender = random;

                lblOutput.Text += doggo1.ToString();
                doggo1.Noise();
            }
            else if (radCat.Checked == true)
            {
                Gender random = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);
                Animal cato1  = new Cat();

                cato1.name   = Interaction.InputBox("Enter your cat's name");
                cato1.color  = Interaction.InputBox("Enter your cat's color");
                cato1.weight = Convert.ToDouble(Interaction.InputBox("Enter your cat's weight"));
                cato1.gender = random;

                lblOutput.Text += cato1.ToString();
                cato1.Noise();
            }
            else if (radBird.Checked == true)
            {
                Gender random = (Gender)gen.Next((int)Gender.Male, (int)Gender.Unknown + 1);
                Animal birb1  = new Bird();

                birb1.name   = Interaction.InputBox("Enter your bird's name");
                birb1.color  = Interaction.InputBox("Enter your bird's color");
                birb1.weight = Convert.ToDouble(Interaction.InputBox("Enter your bird's weight"));
                birb1.gender = random;

                lblOutput.Text += birb1.ToString();
                birb1.Noise();
            }
        }
Exemple #10
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (radDog.Checked == true)
            {
                Dog doggo1 = new Dog();

                doggo1.name   = Interaction.InputBox("Enter your dog's name");
                doggo1.color  = Interaction.InputBox("Enter your dog's color");
                doggo1.weight = Convert.ToDouble(Interaction.InputBox("Enter your dog's weight"));

                lblOutput.Text = doggo1.ToString();
                doggo1.Bark();
            }
            else if (radCat.Checked == true)
            {
                Cat cato1 = new Cat();

                cato1.name   = Interaction.InputBox("Enter your cat's name");
                cato1.color  = Interaction.InputBox("Enter your cat's color");
                cato1.weight = Convert.ToDouble(Interaction.InputBox("Enter your cat's weight"));

                lblOutput.Text = cato1.ToString();
                cato1.Meow();
            }
            else if (radBird.Checked == true)
            {
                Bird birb1 = new Bird();

                birb1.name   = Interaction.InputBox("Enter your bird's name");
                birb1.color  = Interaction.InputBox("Enter your bird's color");
                birb1.weight = Convert.ToDouble(Interaction.InputBox("Enter your bird's weight"));

                lblOutput.Text = birb1.ToString();
                birb1.Tweet();
            }
        }
Exemple #11
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);
                    }
                }
            }
        }
        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 #13
0
        public static void Main()
        {
            // polymorphism - we use given data type through other data type (interface or parent class)
            IAnimal cat = new Cat("Dzhoni", 7);
            IAnimal dog = new Dog("Sharo", 5);

            Console.WriteLine(cat.Speak());
            Console.WriteLine(dog.Speak());

            // no polymorphism
            var secondCat = new Cat("Dzhinks", 3);
            Cat thirdCat  = new Cat("Fluffy", 1);

            Console.WriteLine(secondCat.Purr());
            Console.WriteLine(thirdCat.Purr());
            // cat has no Purr() method, because it's IAnimal

            var secondDog = new Dog("Doggy", 3);
            Dog thirdDog  = new Dog("Spike", 4);

            Console.WriteLine(secondDog.CatchBone());
            Console.WriteLine(thirdDog.CatchBone());
            // dog has no CatchBone() method, because it's IAnimal

            Console.WriteLine();

            // without polymorphism
            List <Cat> cats = new List <Cat>();

            cats.Add(secondCat);
            cats.Add(thirdCat);

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

            dogs.Add(secondDog);
            dogs.Add(thirdDog);

            foreach (var curCat in cats)
            {
                Console.WriteLine(curCat.Speak());
            }

            foreach (var curDog in dogs)
            {
                Console.WriteLine(curDog.Speak());
            }

            Console.WriteLine();

            // using polymorphism - mix different related types in the same collection
            List <IAnimal> animals = new List <IAnimal>();

            animals.Add(cat);
            animals.Add(secondCat);
            animals.Add(thirdCat);
            animals.Add(dog);
            animals.Add(secondDog);
            animals.Add(thirdDog);

            foreach (var curAnimal in animals)
            {
                Console.WriteLine(curAnimal.Speak());
            }

            Console.WriteLine();

            // using polymorphism - invoke abstract operations
            SayAnimalName(cat);
            SayAnimalName(secondCat);
            SayAnimalName(thirdCat);
            SayAnimalName(dog);
            SayAnimalName(secondDog);
            SayAnimalName(thirdDog);

            Console.WriteLine();

            // using polymorphism - declare a more generic field which will be initialized and "specialized" later
            IAnimal animal;
            string  animalType = "Cat";

            switch (animalType)
            {
            case "Cat": { animal = new Cat("Puh", 1); Console.WriteLine(animal.Name); break; }

            case "Dog": { animal = new Dog("Snoop", 1); Console.WriteLine(animal.Name); break; }

            default:
                break;
            }

            Console.WriteLine();

            // polymorphism
            Animal fourthCat = new Cat("Sam", 5);
            Animal fourthDog = new Dog("Chestar", 4);

            Console.WriteLine(fourthCat.ToString()); //Animals.Animals.Cat
            Console.WriteLine(fourthDog.ToString()); //I'm a dog.

            Console.WriteLine(fourthCat.Age);
            Console.WriteLine(fourthDog.Age);

            Kitten kitten = new Kitten("Baby", 1);

            Console.WriteLine(kitten.Speak());

            Console.WriteLine();

            try
            {
                Animal oldCat = new Cat("Oldi", 150);
            }
            catch (InvalidAnimalAgeException ex)    // defined exception class in folder Exceptions
            {
                Console.WriteLine(ex.MinAge);
                Console.WriteLine(ex.MaxAge);
            }
        }