/// <summary> /// Takes the first pet of the prefered type out of the shelter /// </summary> /// <param name="prefered">A Pet of the type that will be returned</param> /// <returns>A Pet depending on the type of prefered Pet passed in</returns> public Pet Dequeue(Pet preferred) { Pet result = null; Pet temp = null; if (preferred.GetType() == Front.GetType()) { result = Dequeue(); } else { temp = Dequeue(); Enqueue(temp); while (Front != temp) { if ((Front.GetType() == preferred.GetType()) && (result == null)) { result = Dequeue(); } else { Enqueue(Dequeue()); } } } if (result == null) { result = Dequeue(); } return(result); }
/// <summary> /// dequeues the first of the animal entered as "pref", else dequeues any dog or cat at the front /// </summary> /// <param name="pref">the type of animal to dequeue</param> /// <returns>the dequeued animal</returns> public Animal Dequeue(string pref) { string fPref = pref.ToLower(); string frontType = Front.GetType().Name.ToLower(); if ((fPref != "cat" && fPref != "dog") || frontType == fPref) { Console.WriteLine($"Dequeueing a {Front.GetType().Name.ToLower()}"); return(Dequeue()); } Animal oldFront = Front; // handles a single animal and no match if (fPref != frontType && Front == Rear) { Console.WriteLine($"There's only one animal in the shelter," + $" and it's not a {fPref}."); return(null); } while (frontType != fPref) { Enqueue(Dequeue()); if (Front == oldFront) { Console.WriteLine("That animal cannot be found here."); return(null); } frontType = Front.GetType().Name.ToLower(); } Console.WriteLine($"Dequeueing a {frontType}"); Animal tmp1 = Dequeue(); // loop to ensure the shelter is in proper order before returning // the desired cat or dog while (Front != oldFront) { Enqueue(Dequeue()); } return(tmp1); }