diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs index 92867a5..1f0f818 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs @@ -64,7 +64,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningWarship warship) { - return company._collection.Insert(warship); + return company._collection.Insert(warship, new DrawningWarshipEqutables()); } /// @@ -122,4 +122,10 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectPosition(); + + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..a823f54 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.CollectionGenericObjects; + +public class CollectionInfo : IEquatable +{ + public string Name { get; private set; } + public CollectionType CollectionType { get; private set; } + public string Description { get; private set; } + private static readonly string _separator = "-"; + public CollectionInfo(string name, CollectionType collectionType, string description) + { + Name = name; + CollectionType = collectionType; + Description = description; + } + public static CollectionInfo? GetCollectionInfo(string data) + { + string[] strs = data.Split(_separator, + StringSplitOptions.RemoveEmptyEntries); + if (strs.Length < 1 || strs.Length > 3) + { + return null; + } + return new CollectionInfo(strs[0], + (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? + strs[2] : string.Empty); + } + public override string ToString() + { + return Name + _separator + CollectionType + _separator + Description; + } + public bool Equals(CollectionInfo? other) + { + return Name == other?.Name; + } + public override bool Equals(object? obj) + { + return Equals(obj as CollectionInfo); + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } +} \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs index 2452d03..a54c4e9 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -27,16 +27,18 @@ public interface ICollectionGenericObjects /// Добавление объекта в коллекцию /// /// Добавляемый объект + /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -62,4 +64,10 @@ public interface ICollectionGenericObjects /// /// Поэлементный вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs index d71a199..e0f67b1 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs @@ -48,15 +48,29 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } 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 (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); @@ -77,4 +91,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs index 24dfa0c..eef8c4a 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,5 +1,7 @@ -using ProjectAircraftCarrier.Exceptions; +using ProjectAircraftCarrier.Drawnings; +using ProjectAircraftCarrier.Exceptions; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -60,8 +62,16 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningWarship, item as DrawningWarship)) + throw new ObjectIsEqualException(); + } + } int index = 0; while (index < _collection.Length) { @@ -75,8 +85,16 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningWarship, item as DrawningWarship)) + throw new ObjectIsEqualException(); + } + } if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { @@ -122,4 +140,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs index a7b5def..e5f53be 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs @@ -10,19 +10,19 @@ public class StorageCollection /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -32,12 +32,13 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (_storages.ContainsKey(name)) return; + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (_storages.ContainsKey(collectionInfo)) return; if (collectionType == CollectionType.None) return; else if (collectionType == CollectionType.Massive) - _storages[name] = new MassiveGenericObjects(); + _storages[collectionInfo] = new MassiveGenericObjects(); else if (collectionType == CollectionType.List) - _storages[name] = new ListGenericObjects(); + _storages[collectionInfo] = new ListGenericObjects(); } /// @@ -46,8 +47,9 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (_storages.ContainsKey(name)) - _storages.Remove(name); + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + _storages.Remove(collectionInfo); } /// @@ -59,8 +61,9 @@ public class StorageCollection { get { - if (_storages.ContainsKey(name)) - return _storages[name]; + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + return _storages[collectionInfo]; return null; } } @@ -100,7 +103,7 @@ public class StorageCollection using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { writer.Write(Environment.NewLine); if (value.Value.Count == 0) @@ -110,8 +113,6 @@ public class StorageCollection writer.Write(value.Key); writer.Write(_separatorForKeyValue); - writer.Write(value.Value.GetCollectionType); - writer.Write(_separatorForKeyValue); writer.Write(value.Value.MaxCount); writer.Write(_separatorForKeyValue); @@ -160,20 +161,22 @@ public class StorageCollection while ((strs = fs.ReadLine()) != null) { string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) + if (record.Length != 3) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + CollectionInfo? collectionInfo = collectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? + throw new Exception("Не удалось создать коллекцию"); if (collection == null) { throw new Exception("Не удалось создать коллекцию"); } - collection.MaxCount = Convert.ToInt32(record[2]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + collection.MaxCount = Convert.ToInt32(record[1]); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningWarship() is T warship) @@ -191,7 +194,7 @@ public class StorageCollection } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs new file mode 100644 index 0000000..ff57024 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Drawnings; + +public class DrawningShipCompareByColor : IComparer +{ + public int Compare(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return 1; + } + + if (y == null || y.EntityWarship == null) + { + return -1; + } + var bodycolorCompare = x.EntityWarship.BodyColor.Name.CompareTo(y.EntityWarship.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityWarship.Speed.CompareTo(y.EntityWarship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs new file mode 100644 index 0000000..0c77084 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Drawnings; + +public class DrawningShipCompareByType : IComparer +{ + public int Compare(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return 1; + } + + if (y == null || y.EntityWarship == null) + { + return -1; + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityWarship.Speed.CompareTo(y.EntityWarship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } +} \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs new file mode 100644 index 0000000..1e62665 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAircraftCarrier.Entities; + +namespace ProjectAircraftCarrier.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawningWarshipEqutables : IEqualityComparer +{ + public bool Equals(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return false; + } + if (y == null || y.EntityWarship == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityWarship.Speed != y.EntityWarship.Speed) + { + return false; + } + if (x.EntityWarship.Weight != y.EntityWarship.Weight) + { + return false; + } + if (x.EntityWarship.BodyColor != y.EntityWarship.BodyColor) + { + return false; + } + if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier) + { + DrawningAircraftCarrier _x = (DrawningAircraftCarrier)x.EntityWarship; + DrawningAircraftCarrier _y = (DrawningAircraftCarrier)x.EntityWarship; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.AircraftDeck != _y.AircraftDeck) + { + return false; + } + if (_x.ControlRoom != _y.ControlRoom) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningWarship obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..976d5e0 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Exceptions; + +internal class ObjectIsEqualException +{ +}