diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs index ccf4527..3f42281 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs @@ -26,7 +26,7 @@ public abstract class AbstractCompany public static int? operator +(AbstractCompany company, DrawingPlane plane) { - return company._collection?.Insert(plane); + return company._collection?.Insert(plane, new DrawingPlaneEqutables()); } public static DrawingPlane operator -(AbstractCompany company, int position) @@ -63,4 +63,6 @@ public abstract class AbstractCompany protected abstract void DrawBackgound(Graphics g); protected abstract void SetObjectsPosition(); + + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/CollectionInfo.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..ee7aeda --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.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(); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs index 2a6e57f..b4f7415 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -7,9 +7,9 @@ public interface ICollectionGenericObjects int MaxCount { set; get; } - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); T Remove(int position); @@ -18,4 +18,6 @@ public interface ICollectionGenericObjects CollectionType GetCollectionType { get; } IEnumerable GetItems(); + + void CollectionSort(IComparer comparer); } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs index c85a5ef..8ed8c8a 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs @@ -39,14 +39,30 @@ 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); @@ -67,4 +83,8 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs index bcb6e76..d767a0f 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectSeaplane.Exceptions; +using ProjectSeaplane.Drawings; +using ProjectSeaplane.Exceptions; namespace ProjectSeaplane.CollectionGenericObjects; @@ -45,8 +46,17 @@ 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 DrawingPlane, item as DrawingPlane)) + throw new ObjectIsEqualException(); + } + } + int index = 0; while (index < _collection.Length) { @@ -59,8 +69,17 @@ 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 DrawingPlane, item as DrawingPlane)) + throw new ObjectIsEqualException(); + } + } + if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { @@ -105,4 +124,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs index 933e9dc..4abc574 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs @@ -10,17 +10,17 @@ 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>(); } /// /// Добавление коллекции в хранилище @@ -29,14 +29,13 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - // TODO проверка, что name не пустой и нет в словаре записи с таким ключом - // TODO Прописать логику для добавления - 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(); } /// /// Удаление коллекции @@ -44,10 +43,9 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - - // TODO Прописать логику для удаления коллекции - if (_storages.ContainsKey(name)) - _storages.Remove(name); + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + _storages.Remove(collectionInfo); } /// /// Доступ к коллекции @@ -58,9 +56,9 @@ public class StorageCollection { get { - // TODO Продумать логику получения объекта - 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; } } @@ -92,18 +90,17 @@ public class StorageCollection using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { StringBuilder sb = new(); sb.Append(Environment.NewLine); + // не сохраняем пустые коллекции if (value.Value.Count == 0) { continue; } sb.Append(value.Key); sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); sb.Append(value.Value.MaxCount); sb.Append(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) @@ -149,25 +146,27 @@ 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("Не удалось создать коллекцию"); + throw new Exception("Не удалось определить тип коллекции:" + record[1]); } - 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?.CreateDrawningPlane() is T plane) + if (elem?.CreateDrawningPlane() is T airplan) { try { - if (collection.Insert(plane) == -1) + if (collection.Insert(airplan) == -1) { throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); } @@ -178,7 +177,7 @@ public class StorageCollection } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByColor.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByColor.cs new file mode 100644 index 0000000..8ce40d8 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Drawings; + +public class DrawingPlaneCompareByColor : IComparer +{ + public int Compare(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + return 1; + } + + if (y == null || y.EntityPlane == null) + { + return -1; + } + var bodycolorCompare = x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByType.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByType.cs new file mode 100644 index 0000000..1a07cc1 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Drawings; + +public class DrawingPlaneCompareByType : IComparer +{ + public int Compare(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + return 1; + } + + if (y == null || y.EntityPlane == null) + { + return -1; + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneEqutables.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneEqutables.cs new file mode 100644 index 0000000..27c1af6 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlaneEqutables.cs @@ -0,0 +1,57 @@ +using ProjectSeaplane.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectSeaplane.Drawings; + +public class DrawingPlaneEqutables : IEqualityComparer +{ + public bool Equals(DrawingPlane? x, DrawingPlane? y) + { + if (x == null || x.EntityPlane == null) + { + return false; + } + if (y == null || y.EntityPlane == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityPlane.Speed != y.EntityPlane.Speed) + { + return false; + } + if (x.EntityPlane.Weight != y.EntityPlane.Weight) + { + return false; + } + if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor) + { + return false; + } + if (x is DrawingSeaplane && y is DrawingSeaplane) + { + EntitySeaplane _x = (EntitySeaplane)x.EntityPlane; + EntitySeaplane _y = (EntitySeaplane)x.EntityPlane; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.Floats != _y.Floats) + { + return false; + } + if (_x.InflatableBoat != _y.InflatableBoat) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingPlane obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Exceptions/ObjectIsEqualException.cs b/ProjectSeaplane/ProjectSeaplane/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..bbc2328 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Exceptions/ObjectIsEqualException.cs @@ -0,0 +1,13 @@ +using System.Runtime.Serialization; + +namespace ProjectSeaplane.Exceptions; + +[Serializable] +public class ObjectIsEqualException : ApplicationException +{ + public ObjectIsEqualException(int count) : base("В коллекции содержится равный элемент: " + count) { } + public ObjectIsEqualException() : base() { } + public ObjectIsEqualException(string message) : base(message) { } + public ObjectIsEqualException(string message, Exception exception) : base(message, exception) { } + protected ObjectIsEqualException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs index 7f4eaf5..3d945cd 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs @@ -52,6 +52,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); @@ -68,13 +70,15 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(677, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 583); + groupBoxTools.Size = new Size(200, 678); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddPlane); panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(buttonRefresh); @@ -83,7 +87,7 @@ panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(6, 346); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(188, 269); + panelCompanyTools.Size = new Size(188, 332); panelCompanyTools.TabIndex = 8; // // buttonAddPlane @@ -107,7 +111,7 @@ // // buttonRefresh // - buttonRefresh.Location = new Point(3, 197); + buttonRefresh.Location = new Point(3, 173); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(182, 41); buttonRefresh.TabIndex = 6; @@ -117,7 +121,7 @@ // // buttonDelPlane // - buttonDelPlane.Location = new Point(3, 107); + buttonDelPlane.Location = new Point(3, 79); buttonDelPlane.Name = "buttonDelPlane"; buttonDelPlane.Size = new Size(182, 41); buttonDelPlane.TabIndex = 4; @@ -127,7 +131,7 @@ // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(3, 154); + buttonGoToCheck.Location = new Point(3, 126); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(182, 41); buttonGoToCheck.TabIndex = 5; @@ -244,7 +248,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(677, 583); + pictureBox.Size = new Size(677, 678); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -288,11 +292,31 @@ // openFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(3, 267); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(182, 41); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Соритровать по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(3, 220); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(182, 41); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // FormSeaplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(877, 607); + ClientSize = new Size(877, 702); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -337,5 +361,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs index 9472a99..4852774 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs @@ -185,7 +185,7 @@ public partial class FormSeaplaneCollection : Form listBoxCollection.Items.Clear(); for (int i = 0; i < _storageCollection.Keys?.Count; ++i) { - string? colName = _storageCollection.Keys?[i]; + string? colName = _storageCollection.Keys?[i].Name; if (!string.IsNullOrEmpty(colName)) { listBoxCollection.Items.Add(colName); @@ -255,4 +255,24 @@ public partial class FormSeaplaneCollection : Form } } } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + ComparePlane(new DrawingPlaneCompareByType()); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) + { + ComparePlane(new DrawingPlaneCompareByColor()); + } + + private void ComparePlane(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } }