diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..8c4c3b5 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.CollectionGenericObjects; + +public class ListGenericObjects : ICollectionGenericObjects + where T : class +{ + private readonly List _collection; + + private int _maxCount; + public int Count => _collection.Count; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + public ListGenericObjects() + { + _collection = new(); + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count >= _maxCount) + { + return -1; + } + _collection.Add(obj); + return _collection.IndexOf(obj); + } + + public int Insert(T obj, int position) + { + if (Count >= _maxCount || position < 0 || position > _maxCount) + { + return -1; + } + + if (position > Count && position < _maxCount) + { + return Insert(obj); + } + + int copy_of_position = position - 1; + + while (position < Count) + { + if (_collection[position] == null) + { + _collection.Insert(position, obj); + return position; + } + position++; + } + + while (copy_of_position > 0) + { + if (_collection[copy_of_position] == null) + { + _collection.Insert(copy_of_position, obj); + return copy_of_position; + } + copy_of_position--; + } + + return -1; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + + T? removed_object = Get(position); + _collection.RemoveAt(position); + return removed_object; + } +}