예제 #1
0
        static void Main(string[] args)
        {
            // Create reference variables for pets and interfaces
            Pet  thisPet = null;
            Dog  dog     = null;
            Cat  cat     = null;
            IDog iDog    = null;
            ICat iCat    = null;

            Pets   pets = new Pets();
            Random rand = new Random();


            for (int i = 0; i < 50; i++)
            {
start:
                // 1 in 10 chance of adding an animal
                if (rand.Next(1, 11) == 1)
                {
                    // 50% chance of adding a dog
                    if (rand.Next(0, 2) == 0)
                    {
                        // Upon winning the dog, initialize variables and ask for each of their values
                        Console.WriteLine("Congratulations, you won a dog!");

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

                        Console.Write("What is its age? ");


                        do
                        {
                            try
                            {
                                age = Int16.Parse(Console.ReadLine());
                            }
                            catch
                            {
                                Console.Write("Please enter an integer: ");
                            }
                        } while (age <= 0);


                        Console.WriteLine("What will you call it? ");
                        try
                        {
                            name = Console.ReadLine();
                        }
                        catch
                        {
                        }

                        Console.WriteLine("Give it a license #: ");
                        try
                        {
                            license = Console.ReadLine();
                        }
                        catch
                        {
                        }

                        // Create new dog passing values to its constructor, and add it to petList

                        pets.Add(dog = new Dog(license, name, age));

                        Console.WriteLine("License: #" + license + ", Name: " + name + ", Age: " + age);
                    }
                    else
                    {
                        // else add a cat
                        // Upon winning the dog, initialize variables and ask for each of their values
                        Console.WriteLine("Congratulations, you won a cat!");

                        string name = null;
                        int    age  = 0;

                        Console.Write("What is its age? ");


                        do
                        {
                            try
                            {
                                age = Int16.Parse(Console.ReadLine());
                            }
                            catch
                            {
                                Console.Write("\r");
                                Console.Write("Please enter an integer: ");
                            }
                        } while (age <= 0);


                        Console.WriteLine("What will you call it? ");
                        try
                        {
                            name = Console.ReadLine();
                        }
                        catch
                        {
                        }

                        // Create new cat passing values to its constructor, and add it to petList
                        pets.Add(cat = new Cat(name, age));

                        Console.WriteLine("Name: " + name + ", Age: " + age);
                    }
                }
                else
                {
                    try
                    {
                        // Get a random pet from petList and make it do something
                        if (thisPet.GetType() == typeof(Cat))
                        {
                            iCat = (ICat)thisPet;

                            // make the cat do something random
                            int nAction = rand.Next(0, 4);
                            switch (nAction)
                            {
                            case 0:
                                iCat.Eat();
                                break;

                            case 1:
                                iCat.Scratch();
                                break;

                            case 2:
                                iCat.Play();
                                break;

                            case 3:
                                iCat.Purr();
                                break;

                            case 4:
                                iDog.GotoVet();
                                break;
                            }
                        }
                        // Get a random pet from petList and make it do something
                        else if (thisPet.GetType() == typeof(Dog))
                        {
                            iDog = (IDog)thisPet;

                            // make the cat do something random
                            int nAction = rand.Next(0, 4);
                            switch (nAction)
                            {
                            case 0:
                                iDog.Eat();
                                break;

                            case 1:
                                iDog.NeedWalk();
                                break;

                            case 2:
                                iCat.Play();
                                break;

                            case 3:
                                iDog.Bark();
                                break;

                            case 4:
                                iDog.GotoVet();
                                break;
                            }
                        }
                    }
                    catch
                    {
                        goto start;
                    }
                }

                Console.WriteLine(i);
            }
        }
예제 #2
0
        //Method: Main
        //Purpose: Create instances of pets
        static void Main(string[] args)
        {
            string name;
            int    age;
            string license;

            Pet  thisPet = null;
            Dog  dog     = null;
            Cat  cat     = null;
            IDog iDog    = null;
            ICat iCat    = null;

            Pets pets = new Pets();

            Random rand = new Random();

            for (int i = 0; i < 50; i++)
            {
                if (rand.Next(1, 11) == 1)
                {
                    if (rand.Next(0, 2) == 0)
                    {
                        Console.WriteLine("You bought a dog!");
                        Console.Write("Dog's Name => ");
                        name = Console.ReadLine();
                        Console.Write("Age => ");
                        while (true)
                        {
                            try
                            {
                                age = int.Parse(Console.ReadLine());
                                break;
                            }
                            catch
                            {
                                Console.Write("Age => ");
                            }
                        }
                        Console.Write("License => ");
                        license = Console.ReadLine();

                        thisPet = new Dog(license, name, age);
                    }
                    else
                    {
                        Console.WriteLine("You bought a cat!");
                        Console.Write("Cat's Name => ");
                        name = Console.ReadLine();
                        Console.Write("Age => ");
                        while (true)
                        {
                            try
                            {
                                age = int.Parse(Console.ReadLine());
                                break;
                            }
                            catch
                            {
                                Console.Write("Age => ");
                            }
                        }
                        cat          = new Cat();
                        thisPet      = cat;
                        thisPet.Name = name;
                        thisPet.age  = age;
                    }
                }
                else
                {
                    thisPet = pets[rand.Next(0, pets.Count)];
                }

                if (thisPet != null)
                {
                    if (thisPet.GetType() == typeof(Cat))
                    {
                        iCat = (ICat)thisPet;
                        int choice = rand.Next(0, 4);

                        if (choice == 0)
                        {
                            iCat.Eat();
                        }
                        else if (choice == 1)
                        {
                            iCat.Play();
                        }
                        else if (choice == 2)
                        {
                            iCat.Scratch();
                        }
                        else
                        {
                            iCat.Purr();
                        }
                    }
                    else
                    {
                        iDog = (IDog)thisPet;
                        int choice = rand.Next(0, 5);

                        if (choice == 0)
                        {
                            iDog.Eat();
                        }
                        else if (choice == 1)
                        {
                            iDog.Play();
                        }
                        else if (choice == 2)
                        {
                            iDog.Bark();
                        }
                        else if (choice == 3)
                        {
                            iDog.NeedWalk();
                        }
                        else
                        {
                            iDog.GotoVet();
                        }
                    }
                }
            }
        }