Example #1
0
 // The Add method for a Utility droid. Code is the same as the above method except for the type of droid being created.
 // The method can be redeclared as Add since it takes different parameters. This is called method overloading.
 public bool Add(string Material, string Color, bool HasToolBox, bool HasComputerConnection, bool HasArm)
 {
     if (lengthOfCollection < (droidCollection.Length - 1))
     {
         droidCollection[lengthOfCollection] = new UtilityDroid(Material, Color, HasToolBox, HasComputerConnection, HasArm);
         lengthOfCollection++;
         return(true);
     }
     else
     {
         return(false);
     }
 }
 // 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);
 }
 /// <summary>
 /// UtilityDroid Add method
 /// </summary>
 /// <param name="material"></param>
 /// <param name="color"></param>
 /// <param name="toolBox"></param>
 /// <param name="computerConnection"></param>
 /// <param name="arm"></param>
 public void Add(
     string material,
     string color,
     bool toolBox,
     bool computerConnection,
     bool arm)
 {
     droids[collectionPosition] = new UtilityDroid(
         material,
         color,
         toolBox,
         computerConnection,
         arm);
     droids[collectionPosition].CalculateTotalCost();
     collectionPosition++;
 }
Example #4
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;
        }
 // Add a new Utility Droid to the collection
 public void AddNewUtilityDroid(string name, string type, string material, string color, bool toolBox, bool computerConnection, bool arm)
 {
     droids[droidLength] = new UtilityDroid(name, type, material, color, true, true, true);
     droidLength++;
 }