// 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();
            }
        }
Example #3
0
        //*******************
        //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();
            }
        }