// The Add method for a Protocol Droid. The parameters passed in match those needed for a protocol droid public bool Add(string Material, string Color, int NumberOfLanguages) { // If there is room to add the new droid if (lengthOfCollection < (droidCollection.Length - 1)) { // Add the new droid. Note that the droidCollection is of type IDroid, but the droid being stored is // of type Protocol Droid. This is okay because of Polymorphism. droidCollection[lengthOfCollection] = new ProtocolDroid(Material, Color, NumberOfLanguages); // Increase the length of the collection lengthOfCollection++; // Return that it was successful return(true); } // Else, there is no room for the droid else { // Return false return(false); } }
/// <summary> /// Public method to Sort the droids into categories using a modified bucket sort /// </summary> public void SortIntoCategories() { // Create a generic stack for each type of droid, and pass in the droid type as the generic that will // come through on the stack class as T. GenericStack <ProtocolDroid> protocolStack = new GenericStack <ProtocolDroid>(); GenericStack <UtilityDroid> utilityStack = new GenericStack <UtilityDroid>(); GenericStack <JanitorDroid> janitorStack = new GenericStack <JanitorDroid>(); GenericStack <AstromechDroid> astromechStack = new GenericStack <AstromechDroid>(); // Create a queue to hold the droids as we pop them off the stack. GenericQueue <IDroid> categorizedDroidQueue = new GenericQueue <IDroid>(); // For each IDroid in the droidCollection foreach (IDroid droid in this.droidCollection) { // If the droid is not null we want to process it. If it is null we will go to the else if (droid != null) { // The testing of the droids must occur in this order. It must be done in the order of // most specific droid to least specific. // If we were to test a droid that IS of type Astromech against Utility BEFORE we test against // Astromech, it would pass and be put into the Utility stack and not the Astromech. That is why it // is important to test from most specific to least. // If the droid is an Astromech, push it on the astromech stack if (droid is AstromechDroid) { astromechStack.Push((AstromechDroid)droid); } // Else if it is a JanitorDroid, push it on the janitor stack else if (droid is JanitorDroid) { janitorStack.Push((JanitorDroid)droid); } // Do for Utility else if (droid is UtilityDroid) { utilityStack.Push((UtilityDroid)droid); } // Do for Protocol else if (droid is ProtocolDroid) { protocolStack.Push((ProtocolDroid)droid); } } // The droid we are trying to consider is null, break out of the loop. else { break; } } // Now that the droids are all in thier respective stacks we can do the work // of poping them off of the stacks and adding them to the queue. // It is required that they be popped off from each stack in this order so that they have // the correct order going into the queue. // This is a primer pop. It gets the first droid off the stack, which could be null if the stack is empty AstromechDroid currentAstromechDroid = astromechStack.Pop(); // While the droid that is popped off is not null while (currentAstromechDroid != null) { // Add the popped droid to the queue. categorizedDroidQueue.Enqueue(currentAstromechDroid); // Pop off the next droid for the loop test currentAstromechDroid = astromechStack.Pop(); } // See above method for Astromech. It is the same except for Janitor JanitorDroid currentJanitorDroid = janitorStack.Pop(); while (currentJanitorDroid != null) { categorizedDroidQueue.Enqueue(currentJanitorDroid); currentJanitorDroid = janitorStack.Pop(); } // See above method for Astromech. It is the same except for Utility UtilityDroid currentUtilityDroid = utilityStack.Pop(); while (currentUtilityDroid != null) { categorizedDroidQueue.Enqueue(currentUtilityDroid); currentUtilityDroid = utilityStack.Pop(); } // See above method for Astromech. It is the same except for Protocol ProtocolDroid currentProtocolDroid = protocolStack.Pop(); while (currentProtocolDroid != null) { categorizedDroidQueue.Enqueue(currentProtocolDroid); currentProtocolDroid = protocolStack.Pop(); } // Now that the droids have all been removed from the stacks and put into the queue // we need to dequeue them all and put them back into the original array. // Set a int counter to 0. int counter = 0; // This is a primer dequeue that will get the first droid out of the queue. IDroid iDroid = categorizedDroidQueue.Dequeue(); // While the dequeued droid is not null. while (iDroid != null) { // Add the droid to the droid collection using the int counter as the index this.droidCollection[counter] = iDroid; // Increment the counter counter++; // Dequeue the next droid off the queue so it can be used in the while condition iDroid = categorizedDroidQueue.Dequeue(); } // Set the length of the collection to the value of the counter. It should be the same, but in case it changed. this.lengthOfCollection = counter; }