// Method to sort the droids based on what type of droid they are
        public void CategorySort()
        {
            // Create instances of the generic stack class for each type of droid
            GenericStack <IDroid> protocolStack  = new GenericStack <IDroid>();
            GenericStack <IDroid> utilityStack   = new GenericStack <IDroid>();
            GenericStack <IDroid> janitorStack   = new GenericStack <IDroid>();
            GenericStack <IDroid> astromechStack = new GenericStack <IDroid>();

            // Create an instance of the generic queue class
            GenericQueue <IDroid> droidQueue = new GenericQueue <IDroid>();

            // Put each element from the array into a stack
            foreach (IDroid droid in droidCollection)
            {
                // Null elements are not used
                if (droid != null)
                {
                    switch (droid.GetType().Name.ToString())
                    {
                    // Protocol stack
                    case "ProtocolDroid":
                        protocolStack.Push(droid);
                        break;

                    // Astromech stack
                    case "AstromechDroid":
                        astromechStack.Push(droid);
                        break;

                    // Janitor stack
                    case "JanitorDroid":
                        janitorStack.Push(droid);
                        break;

                    // Utility stack
                    case "UtilityDroid":
                        utilityStack.Push(droid);
                        break;
                    }
                }
            }

            // Empty the astromech stack into the queue
            while (astromechStack.IsEmpty != true)
            {
                droidQueue.Enqueue(astromechStack.Pop());
            }

            // Empty the janitor stack into the queue
            while (janitorStack.IsEmpty != true)
            {
                droidQueue.Enqueue(janitorStack.Pop());
            }

            // Empty the utility stack into the queue
            while (utilityStack.IsEmpty != true)
            {
                droidQueue.Enqueue(utilityStack.Pop());
            }

            // Empty the protocol stack into the queue
            while (protocolStack.IsEmpty != true)
            {
                droidQueue.Enqueue(protocolStack.Pop());
            }

            // Replace the droid collection array elements with the droid queue
            for (int i = 0; i < droidCollection.Length; i++)
            {
                if (droidQueue.IsEmpty != true)
                {
                    droidCollection[i] = droidQueue.Dequeue();
                }
            }
        }
Exemple #2
0
        /// <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;
        }
        public void CategorizeByModel()
        {
            // Make instance of Protocol Droids
            GenericStack <ProtocolDroid> protocolStackList = new GenericStack <ProtocolDroid>();
            // Make instance of Utility Droids
            GenericStack <UtilityDroid> utilityStackList = new GenericStack <UtilityDroid>();
            // Make instance of Janitor Droids
            GenericStack <JanitorDroid> janitorStackList = new GenericStack <JanitorDroid>();
            // Make instance of Astromech Droids
            GenericStack <AstromechDroid> astromechStackList = new GenericStack <AstromechDroid>();

            // Pushing Droids
            foreach (Droid droid in droids)
            {
                if (droid != null)
                {
                    if (droid.GetType() == typeof(ProtocolDroid))
                    {
                        protocolStackList.Push((ProtocolDroid)droid);
                    }
                    if (droid.GetType() == typeof(UtilityDroid))
                    {
                        utilityStackList.Push((UtilityDroid)droid);
                    }
                    if (droid.GetType() == typeof(JanitorDroid))
                    {
                        janitorStackList.Push((JanitorDroid)droid);
                    }
                    if (droid.GetType() == typeof(AstromechDroid))
                    {
                        astromechStackList.Push((AstromechDroid)droid);
                    }
                }
            }

            // Make instance of GenericQueue class
            GenericQueue queue = new GenericQueue();

            // QUEUE DROIDS IN ORDER OF ASTROMECH, JANITOR, UTILITY, PROTOCOL
            Queue <Droid> queueDroids = new Queue <Droid>();

            while (!astromechStackList.IsEmpty)
            {
                queueDroids.Enqueue(astromechStackList.Pop());
            }
            while (!janitorStackList.IsEmpty)
            {
                queueDroids.Enqueue(janitorStackList.Pop());
            }
            while (!utilityStackList.IsEmpty)
            {
                queueDroids.Enqueue(utilityStackList.Pop());
            }
            while (!protocolStackList.IsEmpty)
            {
                queueDroids.Enqueue(protocolStackList.Pop());
            }

            // Reset the droid length
            droidLength = 0;
            // Dequeue each droid in appropriate list
            while (queueDroids.Count != 0)
            {
                droids[droidLength] = queueDroids.Dequeue();
                droidLength++;
            }

            // Loop through all of the droids
            foreach (Droid droid in droids)
            {
                // If the current beverage is not null, concat it to the return string
                if (droid != null)
                {
                    // Print
                    Console.WriteLine(droid);
                }
            }
        }