Exemple #1
0
        static void SingVerse(FarmAnimal animal)
        {
            //
            // OLD MACDONALD
            //
            Console.WriteLine("Old MacDonald had a farm. ee aye ee aye oh");

            Console.WriteLine($"And on his farm there was a {animal.Name}. ee ay ee ay oh");
            Console.WriteLine($"With a {animal.MakeSoundTwice()} here and a {animal.MakeSoundTwice()} there.");
            Console.WriteLine($"Here a {animal.MakeSoundOnce()}, there a {animal.MakeSoundOnce()}, everywhere a {animal.MakeSoundTwice()}.");
            Console.WriteLine($"Old Macdonald had a farm. ee aye ee aye oh");
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            List <ISound> animals = new List <ISound>();

            //
            // OLD MACDONALD
            //
            Console.WriteLine("Old MacDonald had a farm ee ay ee ay oh");

            // Let's try singing about a Farm Animal
            animals.Add(new Horse());
            animals.Add(new Chicken());
            animals.Add(new Ferrari());
            animals.Add(new Pig());

            foreach (var animal in animals)
            {
                Pig pig = animal as Pig;
                if (pig != null)
                {
                    Console.WriteLine($"Am I a FarmAnimal {((FarmAnimal)animal).MakeSoundTwice()}");
                    Console.WriteLine($"Or am i a Pig {pig.MakeSoundTwice()}");
                    Console.WriteLine();
                }

                if (animal is FarmAnimal)
                {
                    FarmAnimal farmAnimal = (FarmAnimal)animal;
                    Console.WriteLine("And on his farm there was a " + farmAnimal.Name + " ee ay ee ay oh");
                    Console.WriteLine("With a " + farmAnimal.MakeSoundTwice() + " here and a " + farmAnimal.MakeSoundTwice() + " there");
                    Console.WriteLine("Here a " + farmAnimal.MakeSoundOnce() + ", there a " + farmAnimal.MakeSoundOnce() + " everywhere a " + farmAnimal.MakeSoundTwice());
                    Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("I'm not a farm animal dude, have some respect.");
                    Console.WriteLine();
                }
            }

            Console.WriteLine();
            foreach (var animal in animals)
            {
                Console.WriteLine($"Sound I make going round and round is {animal.MakeSound()}");
            }


            // What if we wanted to sing about other things on the farm that were
            // singable but not farm animals?
            // Can it be done?

            Console.ReadLine();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            //
            // OLD MACDONALD
            //
            Chicken      chicken = new Chicken();
            Cow          cow     = new Cow();
            Duck         duck    = new Duck();
            Velociraptor cicero  = new Velociraptor();
            Platypus     occam   = new Platypus();

            // Show implicit casting (Chicken to Farm Animal)
            FarmAnimal console = duck;

            Console.WriteLine(console.NameOfAnimal);
            Console.WriteLine(console.MakeSoundOnce());
            Console.WriteLine(console.MakeSoundTwice());
            Console.WriteLine();

            // Show explicit casting (FarmAnimal to Chicken)
            Duck myDuck = (Duck)console;

            Console.WriteLine(myDuck.NameOfAnimal);
            Console.WriteLine(myDuck.MakeSoundOnce());
            Console.WriteLine(myDuck.MakeSoundTwice());
            Console.WriteLine();

            //Applying Polymorphism, we're allowed to work in terms of
            // generic types and not concrete classes. In this case
            // the list holds a collection of IFarmAnimal, meaning
            // any class that implements the IFarmAnimal interface is allowed
            // to be in the list.
            //Console.WriteLine("Old MacDonald had a farm ee ay ee ay oh");

            //Console.WriteLine("And on his farm there was a " + chicken.NameOfAnimal + " ee ay ee ay oh");
            //Console.WriteLine("With a " + chicken.MakeSoundTwice() + " here and a " + chicken.MakeSoundTwice() + " there");
            //Console.WriteLine("Here a " + chicken.MakeSoundOnce() + ", there a " + chicken.MakeSoundOnce() + " everywhere a " + chicken.MakeSoundTwice());
            //Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
            //Console.WriteLine();

            //Console.WriteLine("And on his farm there was a " + cow.NameOfAnimal + " ee ay ee ay oh");
            //Console.WriteLine("With a " + cow.MakeSoundTwice() + " here and a " + cow.MakeSoundTwice() + " there");
            //Console.WriteLine("Here a " + cow.MakeSoundOnce() + ", there a " + cow.MakeSoundOnce() + " everywhere a " + cow.MakeSoundTwice());
            //Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
            //Console.WriteLine();

            //Console.WriteLine("And on his farm there was a " + duck.NameOfAnimal + " ee ay ee ay oh");
            //Console.WriteLine("With a " + duck.MakeSoundTwice() + " here and a " + duck.MakeSoundTwice() + " there");
            //Console.WriteLine("Here a " + duck.MakeSoundOnce() + ", there a " + duck.MakeSoundOnce() + " everywhere a " + duck.MakeSoundTwice());
            //Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
            //Console.WriteLine();

            // ----- THIS IS GETTING REPETITIVE!
            // We can do better
            // How can we use what we've learned about inheritance
            // to help us remove code duplication
            //
            // What if we create some sort of base class that all our animals have in common?

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

            animals.Add(chicken);
            animals.Add(cow);
            animals.Add(duck);

            Console.WriteLine("Old MacDonald had a farm ee ay ee ay oh");
            foreach (FarmAnimal animal in animals)
            {
                Console.WriteLine("And on his farm there was a " + animal.NameOfAnimal + " ee ay ee ay oh");
                Console.WriteLine("With a " + animal.MakeSoundTwice() + " here and a " + animal.MakeSoundTwice() + " there");
                Console.WriteLine("Here a " + animal.MakeSoundOnce() + ", there a " + animal.MakeSoundOnce() + " everywhere a " + animal.MakeSoundTwice());
                Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
                Console.WriteLine();
            }

            // Interface
            List <IFarmAnimal> interfaceAnimals = new List <IFarmAnimal>();

            interfaceAnimals.Add(cicero);
            interfaceAnimals.Add(occam);

            Console.WriteLine();
            Console.WriteLine("Old MacDonald had a farm ee ay ee ay oh");
            foreach (IFarmAnimal animal in interfaceAnimals)
            {
                Console.WriteLine("And on his farm there was a " + animal.NameOfAnimal + " ee ay ee ay oh");
                Console.WriteLine("With a " + animal.MakeSoundTwice() + " here and a " + animal.MakeSoundTwice() + " there");
                Console.WriteLine("Here a " + animal.MakeSoundOnce() + ", there a " + animal.MakeSoundOnce() + " everywhere a " + animal.MakeSoundTwice());
                Console.WriteLine("Old Macdonald had a farm, ee ay ee ay oh");
                Console.WriteLine();
            }


            George friend = new George();

            friend.Fly();
            Console.WriteLine(friend.NameOfAnimal);
            Console.WriteLine(friend.MakeSoundOnce());
            Console.WriteLine(friend.MakeSoundTwice());
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            int width = 25;

            Console.WriteLine(CreateRow("Employee", "Hours Worked", "Total Weekly Pay", width));
            Console.WriteLine();
            Console.WriteLine(CreateRow("Bob Mackey", "20", "200", width));

            Console.WriteLine("{0, -20}{1, -20}{2, -20}", "Employee", "Hours Worked", "Total Weekly Pay");
            Console.WriteLine();
            Console.WriteLine("{0, -20}{1, -20}{2, -20}", "Bob Mackey", "20", "200");

            Console.ReadKey();
        }