Ejemplo n.º 1
0
        public static void EvictCat(Object sender, ElapsedEventArgs e, Pets petList)
        {
            //gets first cat of the petList
            Cat cat   = null;
            int index = 0;

            while (cat == null)
            {
                if (petList[index] is Cat)
                {
                    cat = (Cat)petList[index];
                }
            }
            //evicts first cat in list
            cat.Evicted(petList);

            //dialogue in console
            Console.WriteLine($"{Name}: You will be a homeless furball, {cat.Name}");

            System.Timers.Timer myTimer = (System.Timers.Timer)sender;
            myTimer.Stop();
        }
Ejemplo n.º 2
0
 public void Evicted(Pets petlist)
 {
     petlist.RemoveAt(0);
     Console.WriteLine($"{Name}: AAAAAAAAAAAAAAAAAAAAAAH! Help me, I don't like the cold!");
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            int i       = 0;
            int petEl   = 0;
            Pet thisPet = null;

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

            // seed the random number generator
            Random rand = new Random();

            Pets pets = new Pets();

            Timer myTimer = new Timer(20000);

            myTimer.Elapsed += (sender, e) => Dog.EvictCat(sender, e, pets);
            //myTimer.Elapsed += (sender, e) => Console.WriteLine("yes");
            myTimer.Start();

            for (i = 0; i < 50; ++i)
            {
                // 1 in 10 chance of adding an animal
                if (rand.Next(1, 11) == 1)
                {
                    Console.WriteLine();
                    if (rand.Next(1, 3) == 1)
                    {
                        // add a cat
                        cat = new Cat();
                        Console.WriteLine("You bought a cat!");
                        Console.Write("Cat's Name => ");
                        cat.Name = Console.ReadLine();
                        Console.Write("Age => ");
                        cat.age = Convert.ToInt32(Console.ReadLine());

                        thisPet = cat;
                    }
                    else
                    {
                        // add a dog
                        string szLicense;
                        string szName;
                        int    nAge;

                        Console.WriteLine("You bought a dog!");
                        Console.Write("Dog's Name => ");
                        szName = Console.ReadLine();
                        Console.Write("Age => ");
                        nAge = Convert.ToInt32(Console.ReadLine());
                        Console.Write("License => ");
                        szLicense = Console.ReadLine();

                        dog = new Dog(szLicense, szName, nAge);

                        thisPet = dog;
                    }

                    pets.Add(thisPet);
                }
                else
                {
                    petEl = rand.Next(0, pets.Count);

                    thisPet = pets[petEl];

                    if (thisPet == null)
                    {
                        continue;
                    }

                    if (thisPet.GetType() == typeof(Cat))
                    {
                        iCat = (ICat)thisPet;

                        int nAction = rand.Next(0, 4);
                        switch (nAction)
                        {
                        case 0:
                            iCat.Eat();
                            break;

                        case 1:
                            iCat.Play();
                            break;

                        case 2:
                            iCat.Purr();
                            break;

                        case 3:
                            iCat.Scratch();
                            break;
                        }
                    }
                    else
                    {
                        iDog = (IDog)thisPet;
                        int nAction = rand.Next(0, 5);

                        switch (nAction)
                        {
                        case 0:
                            iDog.Eat();
                            break;

                        case 1:
                            iDog.Play();
                            break;

                        case 2:
                            iDog.Bark();
                            break;

                        case 3:
                            iDog.NeedWalk();
                            break;

                        case 4:
                            iDog.GotoVet();
                            break;
                        }
                    }
                }
            }
        }