// Public method to sort the droids by type: public void SortDroidsByType() { // Instantiate the GenericStack for each type of droid: GenericStack <Droid> protocolStack = new GenericStack <Droid>(); GenericStack <Droid> utilityStack = new GenericStack <Droid>(); GenericStack <Droid> janitorialStack = new GenericStack <Droid>(); GenericStack <Droid> astromechStack = new GenericStack <Droid>(); // Instantiate the GenericQueue to be used: GenericQueue <Droid> droidQueue = new GenericQueue <Droid>(); // Loop through the droidCollection and sort the droids by type, putting into their stacks: for (int i = 0; i < lengthOfCollection; i++) { // If the droid is of type ProtocolDroid, add to the protocolStack: if (droidCollection[i].GetType() == typeof(ProtocolDroid)) { protocolStack.AddToFront(droidCollection[i]); } // If the droid is of type UtilityDroid, add to the utilityStack: else if (droidCollection[i].GetType() == typeof(UtilityDroid)) { utilityStack.AddToFront(droidCollection[i]); } // If the droid is of type JanitorDroid, add to the janitorialStack: else if (droidCollection[i].GetType() == typeof(JanitorDroid)) { janitorialStack.AddToFront(droidCollection[i]); } // Else, the droid is of type AstromechDroid, so add to the astromechStack: else { astromechStack.AddToFront(droidCollection[i]); } } // Place the droids in the queue in the desired order (astromech, janitor, utility, protocol) // by removing the current droid from the appropriate stack and adding it to the back of the queue // until the size of the current stack is 0: while (astromechStack.Size > 0) { droidQueue.AddToBack(astromechStack.RemoveFromFront()); } while (janitorialStack.Size > 0) { droidQueue.AddToBack(janitorialStack.RemoveFromFront()); } while (utilityStack.Size > 0) { droidQueue.AddToBack(utilityStack.RemoveFromFront()); } while (protocolStack.Size > 0) { droidQueue.AddToBack(protocolStack.RemoveFromFront()); } // Replace the droids in droidCollection with the now sorted droid collection from the droidQueue: for (int i = 0; i < lengthOfCollection; i++) { droidCollection[i] = droidQueue.RemoveFromFront(); } }
public void DroidsByType() { //Individual stacks for the droids by type GenericStack <Droid> Protocols = new GenericStack <Droid>(); GenericStack <Droid> Astromechs = new GenericStack <Droid>(); GenericStack <Droid> Janitors = new GenericStack <Droid>(); GenericStack <Droid> Utilities = new GenericStack <Droid>(); //One single queue that will hold the sorted droids. They will be inserted by type //after that have been soted into there respective stack GenericQueue <Droid> DroidQueue = new GenericQueue <Droid>(); //Bubble sort for the droids by type for (int i = 0; i < lengthOfCollection; i++) { if (droidCollection[i].GetType() == typeof(ProtocolDroid)) { Protocols.AddToFront(droidCollection[i]); } if (droidCollection[i].GetType() == typeof(AstromechDroid)) { Astromechs.AddToFront(droidCollection[i]); } if (droidCollection[i].GetType() == typeof(JanitorDroid)) { Janitors.AddToFront(droidCollection[i]); } if (droidCollection[i].GetType() == typeof(UtilityDroid)) { Utilities.AddToFront(droidCollection[i]); } } //Add the droids to the queue for further sorting and allowing printing ong //only one array - this will empty the current collections and place them in //a new one while (Astromechs.Size > 0) { DroidQueue.AddToBack(Astromechs.RemoveFromFront()); } while (Janitors.Size > 0) { DroidQueue.AddToBack(Janitors.RemoveFromFront()); } while (Utilities.Size > 0) { DroidQueue.AddToBack(Utilities.RemoveFromFront()); } while (Protocols.Size > 0) { DroidQueue.AddToBack(Protocols.RemoveFromFront()); } //Reload the droid collection for (int r = 0; r < lengthOfCollection; r++) { droidCollection[r] = DroidQueue.RemoveFromFront(); } }
public void AddStack(GenericStack <T> stack) { Node <T> tempNode = stack.HeadNode; this.depth += stack.Depth; while (tempNode != null) { Add(tempNode.Droid); tempNode = tempNode.Next; } }
public void CategorizeByModel() { //Method to categorize droids in the order of Astromech, Janitor, Utility, Protocol GenericStack <IDroid> protocolStack = new GenericStack <IDroid>(); GenericStack <IDroid> utilityStack = new GenericStack <IDroid>(); GenericStack <IDroid> janitorStack = new GenericStack <IDroid>(); GenericStack <IDroid> astromechStack = new GenericStack <IDroid>(); foreach (IDroid droid in droidCollection) { if (droid != null) { switch (droid.GetModel()) { //Go through the droidCollection and separate the droids into stacks by Model case "PROTOCOL": protocolStack.Add(droid); break; case "UTILITY": utilityStack.Add(droid); break; case "JANITOR": janitorStack.Add(droid); break; case "ASTROMECH": astromechStack.Add(droid); break; default: break; } } } //Create the queue and add the stacks to it in the desired order GenericQueue <IDroid> droidQueue = new GenericQueue <IDroid>(); droidQueue.AddStack(astromechStack); droidQueue.AddStack(janitorStack); droidQueue.AddStack(utilityStack); droidQueue.AddStack(protocolStack); droidCollection = new IDroid[droidQueue.Depth]; for (int i = 0; i < droidQueue.Depth; i++) { droidCollection[i] = (IDroid)droidQueue.Retrieve(i + 1).Droid; } }
public void SortModel() { GenericStack <UtilityDroid> UtilityDroidStack = new GenericStack <UtilityDroid>(); GenericStack <AstromechDroid> AstromechDroidStack = new GenericStack <AstromechDroid>(); GenericStack <ProtocolDroid> ProtocolDroidStack = new GenericStack <ProtocolDroid>(); GenericStack <JanitorDroid> JanitorDroidStack = new GenericStack <JanitorDroid>(); foreach (IDroid droids in droidCollection) { if (droids is UtilityDroid) { UtilityDroidStack.Add((UtilityDroid)droids); } else if (droids is AstromechDroid) { AstromechDroidStack.Add((AstromechDroid)droids); } else if (droids is ProtocolDroid) { ProtocolDroidStack.Add((ProtocolDroid)droids); } else if (droids is JanitorDroid) { JanitorDroidStack.Add((JanitorDroid)droids); } //*****************************************************************************************************************PROBLEM***********************************************************************************************************************************8************************************************* //Error 4 Argument 1: cannot convert from 'cis237assignment4.GenericStack<cis237assignment4.UtilityDroid>' to 'cis237assignment4.IDroid' c:\users\jason\cis237\cis237assignment4\cis237assignment4\droidcollection.cs 174 23 cis237assignment4 //Error 3 The best overloaded method match for 'cis237assignment4.GenericQueue<cis237assignment4.IDroid>.Add(cis237assignment4.IDroid)' has some invalid arguments c:\users\jason\cis237\cis237assignment4\cis237assignment4\droidcollection.cs 175 13 cis237assignment4 /************************************************************************************************************************************************************************************************************************************************/ } GenericQueue <IDroid> Queue = new GenericQueue <IDroid>(); //Queue.Add(AstromechDroidStack); //Queue.Add(UtilityDroidStack); }
//method to organize droids by type public bool Organize() { GenericStack <AstromechDroid> astromechStack = new GenericStack <AstromechDroid>(); GenericStack <JanitorDroid> janitorStack = new GenericStack <JanitorDroid>(); GenericStack <UtilityDroid> utilityStack = new GenericStack <UtilityDroid>(); GenericStack <ProtocolDroid> protocolStack = new GenericStack <ProtocolDroid>(); GenericQueue <IDroid> droidQueue = new GenericQueue <IDroid>(); //Add droids to appropriate stack types for (int i = 0; i < lengthOfCollection; i++) { try { astromechStack.Add((AstromechDroid)droidCollection[i]); } catch { try { janitorStack.Add((JanitorDroid)droidCollection[i]); } catch { try { utilityStack.Add((UtilityDroid)droidCollection[i]); } catch { try { protocolStack.Add((ProtocolDroid)droidCollection[i]); } catch { return(false); } } } } } //Add droids in order to the queue while (astromechStack.Head != null) { droidQueue.Add((IDroid)astromechStack.Pop()); } while (janitorStack.Head != null) { droidQueue.Add((IDroid)janitorStack.Pop()); } while (utilityStack.Head != null) { droidQueue.Add((IDroid)utilityStack.Pop()); } while (protocolStack.Head != null) { droidQueue.Add((IDroid)protocolStack.Pop()); } //Dequeue droids back into the array for (int i = 0; droidQueue.Tail != null; i++) { droidCollection[i] = (IDroid)droidQueue.Dequeue(); } return(true); }
//******************* //Categorize by model //******************* public void CategorizeModels() { //declare queue and stacks GenericQueue <IDroid> queue = new GenericQueue <IDroid>(); GenericStack <IDroid> pro = new GenericStack <IDroid>(); GenericStack <IDroid> uti = new GenericStack <IDroid>(); GenericStack <IDroid> jan = new GenericStack <IDroid>(); GenericStack <IDroid> ast = new GenericStack <IDroid>(); //sort the collection array into stacks foreach (IDroid droid in droidCollection) { //we cannot have the null droids in the array counted for if (droid != null) { switch (droid.Model) { case "Protocol": //push onto stack pro.AddToTop(droid); break; case "Utility": uti.AddToTop(droid); break; case "Janitor": jan.AddToTop(droid); break; case "Astromech": ast.AddToTop(droid); break; default: queue.AddToBack(droid); break; } } } //Remove from stacks and add to queue //order to be displayed is astromech, janitor, utility, protocol while (!ast.IsEmpty) { //pop off of stack, and then enqueue that item queue.AddToBack(ast.RemoveFromTop()); } while (!jan.IsEmpty) { queue.AddToBack(jan.RemoveFromTop()); } while (!uti.IsEmpty) { queue.AddToBack(uti.RemoveFromTop()); } while (!pro.IsEmpty) { queue.AddToBack(pro.RemoveFromTop()); } //Replace collection array with queue int c = 0; while (!queue.IsEmpty) { //dequeue item into collection array droidCollection[c++] = queue.RemoveFromFront(); } }