diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/AbstractCompany.cs b/ProjectElectricLocomotive/CollectionGenericObjects/AbstractCompany.cs index 0022d60..f8b7406 100644 --- a/ProjectElectricLocomotive/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectElectricLocomotive/CollectionGenericObjects/AbstractCompany.cs @@ -62,7 +62,7 @@ public abstract class AbstractCompany public static int operator +(AbstractCompany company, DrawningLocomotive locomotive) { - return company._collection.Insert(locomotive); + return company._collection.Insert(locomotive, new DrawiningLocomotiveEqutables()) ?? false; } /// /// Перегрузка оператора удаления для класса diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectElectricLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs index f82973b..9ef401a 100644 --- a/ProjectElectricLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectElectricLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -27,22 +27,23 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); - + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// Cравнение двух объектов + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Cравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась -int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); - /// - /// Удаление объекта из коллекции с конкретной позиции - /// - /// Позиция - /// true - удаление прошло удачно, false - удаление не удалось -T Remove(int position); + T Remove(int position); /// /// Получение объекта по позиции /// @@ -60,4 +61,11 @@ T Remove(int position); /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); + } diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs b/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs index 0e4148e..0ea64ee 100644 --- a/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs @@ -55,21 +55,25 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // выброс ошибки если переполнение + //выброс ошибки если такой объект уже есть в коллекции + if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // проверка, что не превышено максимальное количество элементов // проверка позиции // вставка по позиции // выброс ошибки, если переполнение // выброс ошибки если выход за границу + //выброс ошибки если такой объект уже есть в коллекции + if (Count == _maxCount) throw new CollectionOverflowException(Count); @@ -98,5 +102,11 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + throw new NotImplementedException(); + } } } diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs index 44925ab..0cb1f62 100644 --- a/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs @@ -56,11 +56,13 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects if (_collection[position] == null) throw new ObjectNotFoundException(position); return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // вставка в свободное место набора // выброс ошибки, если переполнение //выброс ошибки, если выход за границы массива + //выброс ошибки если такой объект уже есть в коллекции + for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -72,7 +74,7 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // проверка позиции // проверка, что элемент массива по этой позиции пустой, если нет, то @@ -80,6 +82,7 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects // вставка //выброс ошибки, если переполнение //выброс ошибки, если выход за границы массива + //выброс ошибки если такой объект уже есть в коллекции if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) @@ -128,5 +131,11 @@ namespace ProjectElectricLocomotive.CollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + throw new NotImplementedException(); + } } } diff --git a/ProjectElectricLocomotive/Drawnings/DrawingLocomotiveEqutables.cs b/ProjectElectricLocomotive/Drawnings/DrawingLocomotiveEqutables.cs new file mode 100644 index 0000000..8693a10 --- /dev/null +++ b/ProjectElectricLocomotive/Drawnings/DrawingLocomotiveEqutables.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawiningLocomotiveEqutables : IEqualityComparer +{ + public bool Equals(DrawningLocomotive? x, DrawningLocomotive? y) + { + if (x == null || x.EntityLocomotive == null) + { + return false; + } + if (y == null || y.EntityLocomotive == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityLocomotive.Speed != y.EntityLocomotive.Speed) + { + return false; + } + if (x.EntityLocomotive.Weight != y.EntityLocomotive.Weight) + { + return false; + } + if (x.EntityLocomotive.BodyColor != y.EntityLocomotive.BodyColor) + { + return false; + } + if (x is DrawningElectricLocomotive && y is DrawningElectricLocomotive) + { + + + + // TODO доделать логику сравнения дополнительных параметров + } + return true; + } + public int GetHashCode([DisallowNull] DrawningLocomotive obj) + { + return obj.GetHashCode(); + } +} + diff --git a/ProjectElectricLocomotive/Drawnings/DrawningLocomotiveCompareByColor.cs b/ProjectElectricLocomotive/Drawnings/DrawningLocomotiveCompareByColor.cs new file mode 100644 index 0000000..686342d --- /dev/null +++ b/ProjectElectricLocomotive/Drawnings/DrawningLocomotiveCompareByColor.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Drawnings +{ + /// +/// Сравнение по цвету, скорости, весу +/// +public class DrawningCarCompareByColor : IComparer +{ +public int Compare(DrawningLocomotive? x, DrawningLocomotive? y) +{ +// TODO прописать логику сравения по цветам, скорости, весу +throw new NotImplementedException(); +} +} +} diff --git a/ProjectElectricLocomotive/Drawnings/DrawninglocomotiveCompareByType.cs b/ProjectElectricLocomotive/Drawnings/DrawninglocomotiveCompareByType.cs new file mode 100644 index 0000000..40f93a3 --- /dev/null +++ b/ProjectElectricLocomotive/Drawnings/DrawninglocomotiveCompareByType.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Drawnings; + +/// +/// Сравнение по типу, скорости, весу +/// +public class DrawningCarCompareByType : IComparer +{ + public int Compare(DrawningLocomotive? x, DrawningLocomotive? y) + { + if (x == null || x.EntityLocomotive == null) + { + return -1; + } + if (y == null || y.EntityLocomotive == null) + { + return 1; + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight); + } +} +