// Constructor that takes in the size of the collection. // It sets the size of the internal array that will be used. // It also sets the length of the collection to zero since nothing is added yet. public DroidCollection(int sizeOfCollection) { // Make new array for the collection droidCollection = new IDroid[sizeOfCollection]; // Set length of collection to 0 lengthOfCollection = 8; // Add droids to the array droidCollection[0] = new JanitorDroid("Carbonite", "Red", true, false, true, true, false); droidCollection[1] = new ProtocolDroid("Vanadium", "White", 2); droidCollection[2] = new AstromechDroid("Quadranium", "Blue", true, true, true, true, 6); droidCollection[3] = new UtilityDroid("Carbonite", "White", false, false, false); droidCollection[4] = new UtilityDroid("Vanadium", "Green", true, false, true); droidCollection[5] = new ProtocolDroid("Quadranium", "Blue", 7); droidCollection[6] = new JanitorDroid("Quadranium", "Green", false, true, true, false, true); droidCollection[7] = new AstromechDroid("Tears Of A Jedi", "White", true, false, true, false, 3); }
/****************************************************************************** * * Overloaded Add methods. These add the four droid types to the collection. * Adding a droid advances the collectionposition variable so we can add a * droid to the next array slot. * * All four of these are functionally identical and only have different signatures, * so only the first is commented. * ******************************************************************************/ /// <summary> /// ProtocolDroid Add method /// </summary> /// <param name="material"></param> /// <param name="color"></param> /// <param name="numberOfLanguages"></param> public void Add(string material, string color, int numberOfLanguages) { // create a new Droid of the type indicated by the method signature droids[collectionPosition] = new ProtocolDroid( material, color, numberOfLanguages); // invoke the total cost calculation method for this specific droid type droids[collectionPosition].CalculateTotalCost(); // advance to the next position collectionPosition++; }
// 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; }
// Add a new Protocol Droid to the collection public void AddNewProtocolDroid(string name, string type, string material, string color, int numOfLanguages) { droids[droidLength] = new ProtocolDroid(name, type, material, color, 1); droidLength++; }