Answer to Question #180509 in C# for Miley

Question #180509

Maintain a list of ducks to do following operations

1.       Add a duck

2.       Remove a duck

3.       Remove all ducks

4.       Capability to iterate the duck collection in increasing order of their weights. This should be the collections default iteration behaviour

5.       Capability to iterate the duck collection in increasing order of number of wings.



1
Expert's answer
2021-04-14T07:37:29-0400
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace DuckType
{
    public class Duck
    {
        public string Name { get; private set; }
        public int Weight { get; private set; }
        public int WingsCount { get; private set; }




        public Duck(string name, int weight, int wings)
        {
            Name = name;
            Weight = weight;
            WingsCount = wings;
        }


    }


    public class DuckCollection
    {


        private class DuckNestedIteratorByWeight : IEnumerable<Duck>, IEnumerator<Duck>
        {
            private Duck[] ducks;
            private int currentPos = -1;


            public DuckNestedIteratorByWeight(Duck[] duckArray)
            {


                ducks = duckArray;
                Duck minDuck = ducks[0];


                for (int i = 0; i < ducks.Length -1; i++)
                {
                    for (int j = i + 1; j < ducks.Length; j++)
                    {
                        if (ducks[i].Weight > ducks[j].Weight)
                        {
                            minDuck = ducks[i];
                            ducks[i] = ducks[j];
                            ducks[j] = minDuck;
                        }
                    }
                }
                
            }


            public Duck Current => ducks[currentPos];


            object IEnumerator.Current => this.Current;


            public void Dispose()
            {
                ducks = null;
            }


            public IEnumerator<Duck> GetEnumerator()
            {
                return this;
            }


            public bool MoveNext()
            {
                currentPos += 1;
                return currentPos < ducks.Length;
            }


            public void Reset()
            {
                currentPos = -1;
            }


            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
        }




        private class DuckNestedIteratorByWings : IEnumerable<Duck>, IEnumerator<Duck>
        {
            private Duck[] ducks;
            private int currentPos = -1;


            public DuckNestedIteratorByWings(Duck[] duckArray)
            {


                ducks = duckArray;
                Duck minDuck = ducks[0];


                for (int i = 0; i < ducks.Length - 1; i++)
                {
                    for (int j = i + 1; j < ducks.Length; j++)
                    {
                        if (ducks[i].WingsCount > ducks[j].WingsCount)
                        {
                            minDuck = ducks[i];
                            ducks[i] = ducks[j];
                            ducks[j] = minDuck;
                        }
                    }
                }


            }


            public Duck Current => ducks[currentPos];


            object IEnumerator.Current => this.Current;


            public void Dispose()
            {
                ducks = null;
            }


            public IEnumerator<Duck> GetEnumerator()
            {
                return this;
            }


            public bool MoveNext()
            {
                currentPos += 1;
                return currentPos < ducks.Length;
            }


            public void Reset()
            {
                currentPos = -1;
            }


            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
        }


        private Duck[] _internalDuckStorage;
        private int actualDuckCount = 0;


        public DuckCollection()
        {
            _internalDuckStorage = new Duck[1];
        }


        public DuckCollection(int count)
        {
            _internalDuckStorage = new Duck[count];
        }


        public void AddDuck(Duck d)
        {
            actualDuckCount += 1;
            if (_internalDuckStorage.Length >= actualDuckCount)
            {
                _internalDuckStorage[actualDuckCount - 1] = d;
            }
            else
            {


                Duck[] extendedCollection = new Duck[_internalDuckStorage.Length + 1];


                for (int i = 0; i < _internalDuckStorage.Length; i++)
                {
                    extendedCollection[i] = _internalDuckStorage[i];
                }


                extendedCollection[extendedCollection.Length - 1] = d;


                _internalDuckStorage = extendedCollection;


            }
        }


        public void RemoveDuck(Duck d)
        {
            int duckIndex = -1;


            for (int i = 0; i < _internalDuckStorage.Length; i++)
            {
                if (_internalDuckStorage[i] == d)
                {
                    duckIndex = i;
                }
            }


            if (duckIndex > 0)
            {
                Duck[] newDuckCollection = new Duck[_internalDuckStorage.Length - 1];


                for (int i = 0; i < newDuckCollection.Length; i++)
                {
                    if (i>=duckIndex)
                    {
                        newDuckCollection[i] = _internalDuckStorage[i+1];
                    }
                    else
                    {
                        newDuckCollection[i] = _internalDuckStorage[i];
                    }
                    
                }


                _internalDuckStorage = newDuckCollection;
            }
        }


        public void RemoveAllDucks()
        {
            _internalDuckStorage = new Duck[1];
        }


        public IEnumerable<Duck> GetIteratorByWeight()
        {
            return new DuckNestedIteratorByWeight(_internalDuckStorage);
        }


        public IEnumerable<Duck> GetIteratorByWings()
        {
            return new DuckNestedIteratorByWings(_internalDuckStorage);
        }




    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
14.04.21, 14:36

Dear Rounak Ghosh, Questions in this section are answered for free. We can't fulfill them all and there is no guarantee of answering certain question but we are doing our best. And if answer is published it means it was attentively checked by experts. You can try it yourself by publishing your question. Although if you have serious assignment that requires large amount of work and hence cannot be done for free you can submit it as assignment and our experts will surely assist you.

Rounak Ghosh
14.04.21, 13:19

How to implement step 4 and 5 ?

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS