diff --git a/ProjectSeaplane/ProjectSeaplane/DrawiningPlaneEquatables.cs b/ProjectSeaplane/ProjectSeaplane/DrawiningPlaneEquatables.cs new file mode 100644 index 0000000..d3a2686 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/DrawiningPlaneEquatables.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.Entities; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Generics +{ + internal class DrawiningPlaneEqutables : IEqualityComparer + { + public bool Equals(DrawningPlane? x, DrawningPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + 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 DrawningSeaplane && y is DrawningSeaplane) + { + EntitySeaplane _seplaneX = (EntitySeaplane)x.EntityPlane; + EntitySeaplane _seplaneY = (EntitySeaplane)y.EntityPlane; + if (_seplaneX.Boat != _seplaneY.Boat) + { + return false; + } + if (_seplaneX.Floater != _seplaneY.Floater) + { + return false; + } + if (_seplaneX.AdditionalColor != _seplaneY.AdditionalColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningPlane obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs index 60fe1f7..6e3f69f 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs @@ -30,6 +30,8 @@ { pictureBoxCollection = new PictureBox(); groupBox1 = new GroupBox(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); groupBox2 = new GroupBox(); buttonDeleteObject = new Button(); listBoxStorages = new ListBox(); @@ -62,6 +64,8 @@ // // groupBox1 // + groupBox1.Controls.Add(ButtonSortByColor); + groupBox1.Controls.Add(ButtonSortByType); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemovePlane); @@ -74,6 +78,26 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(14, 291); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(149, 23); + ButtonSortByColor.TabIndex = 6; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(14, 264); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(149, 23); + ButtonSortByType.TabIndex = 5; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // groupBox2 // groupBox2.Controls.Add(buttonDeleteObject); @@ -180,14 +204,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Size = new Size(133, 22); SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(133, 22); LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -241,5 +265,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs index e334d3b..d6f8720 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs @@ -34,6 +34,26 @@ namespace ProjectSeaplane _logger = logger; } + private void ButtonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor()); + + private void ComparePlanes(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowPlanes(); + } + /// /// Заполнение listBoxObjects /// @@ -41,20 +61,22 @@ namespace ProjectSeaplane { int index = listBoxStorages.SelectedIndex; listBoxStorages.Items.Clear(); - foreach (var key in _storage.Keys) + for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(key); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } - if (listBoxStorages.Items.Count > 0 && (index == -1 || index - >= listBoxStorages.Items.Count)) + + if (listBoxStorages.Items.Count > 0 && (index == -1 || + index >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } else if (listBoxStorages.Items.Count > 0 && index > -1 && - index < listBoxStorages.Items.Count) + index < listBoxStorages.Items.Count) { listBoxStorages.SelectedIndex = index; } + } /// /// Добавление набора в коллекцию @@ -149,6 +171,12 @@ namespace ProjectSeaplane MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + + } }); form.AddEvent(planeDelegate); @@ -177,7 +205,7 @@ namespace ProjectSeaplane { return; } - + try { int pos = Convert.ToInt32(maskedTextBoxNumber.Text); diff --git a/ProjectSeaplane/ProjectSeaplane/PlaneCompareByColor.cs b/ProjectSeaplane/ProjectSeaplane/PlaneCompareByColor.cs new file mode 100644 index 0000000..18b11ab --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/PlaneCompareByColor.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.Entities; + +namespace ProjectSeaplane.Generics +{ + internal class PlaneCompareByColor : IComparer + { + public int Compare(DrawningPlane? x, DrawningPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityPlane.BodyColor.Name != y.EntityPlane.BodyColor.Name) + { + return x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawningPlane) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningSeaplane) + { + EntitySeaplane _seplaneX = (EntitySeaplane)x.EntityPlane; + EntitySeaplane _seplaneY = (EntitySeaplane)y.EntityPlane; + + if (_seplaneX.AdditionalColor.Name != _seplaneY.AdditionalColor.Name) + { + return _seplaneX.AdditionalColor.Name.CompareTo(_seplaneY.AdditionalColor.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/PlaneCompareByType.cs b/ProjectSeaplane/ProjectSeaplane/PlaneCompareByType.cs new file mode 100644 index 0000000..66ed0c8 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/PlaneCompareByType.cs @@ -0,0 +1,28 @@ +using ProjectSeaplane.DrawningObjects; +namespace ProjectSeaplane.Generics +{ + internal class PlaneCompareByType : IComparer + { + public int Compare(DrawningPlane? x, DrawningPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + 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/PlanesCollectionInfo.cs b/ProjectSeaplane/ProjectSeaplane/PlanesCollectionInfo.cs new file mode 100644 index 0000000..17f2eb9 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/PlanesCollectionInfo.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Generics +{ + internal class PlanesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public PlanesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(PlanesCollectionInfo? other) + { + // TODO прописать логику сравнения по свойству Name + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } + +} diff --git a/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs b/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs index 9da302c..91d6bd7 100644 --- a/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs @@ -12,6 +12,8 @@ namespace ProjectSeaplane.Generics where T : DrawningPlane where U : IMoveableObject { + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Получение объектов коллекции /// @@ -62,7 +64,7 @@ namespace ProjectSeaplane.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawiningPlaneEqutables()); } /// diff --git a/ProjectSeaplane/ProjectSeaplane/PlanesGenericStorage.cs b/ProjectSeaplane/ProjectSeaplane/PlanesGenericStorage.cs index 0a1c523..94df4f1 100644 --- a/ProjectSeaplane/ProjectSeaplane/PlanesGenericStorage.cs +++ b/ProjectSeaplane/ProjectSeaplane/PlanesGenericStorage.cs @@ -8,11 +8,24 @@ using ProjectSeaplane.MovementStrategy; using ProjectSeaplane.Generics; using System.IO; using ProjectSeaplane.Exceptions; +using System.Diagnostics.Eventing.Reader; namespace ProjectSeaplane.Generics { internal class PlanesGenericStorage { + readonly Dictionary> _planeStorages; + public List Keys => _planeStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + public PlanesGenericStorage(int pictureWidth, int pictureHeight) + { + _planeStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } /// /// Разделитель для записи ключа и значения элемента словаря /// @@ -38,7 +51,7 @@ namespace ProjectSeaplane.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _planeStorages) { StringBuilder records = new(); @@ -118,31 +131,32 @@ namespace ProjectSeaplane.Generics } } - _planeStorages.Add(name, collection); + _planeStorages.Add(new PlanesCollectionInfo(name, string.Empty), collection); } } } - readonly Dictionary> _planeStorages; - public List Keys => _planeStorages.Keys.ToList(); - private readonly int _pictureWidth; - private readonly int _pictureHeight; - public PlanesGenericStorage(int pictureWidth, int pictureHeight) - { - _planeStorages = new Dictionary>(); - _pictureWidth = pictureWidth; - _pictureHeight = pictureHeight; - } + public void AddSet(string name) { - if (_planeStorages.ContainsKey(name)) return; - _planeStorages[name] = new PlanesGenericCollection(_pictureWidth, _pictureHeight); + if (_planeStorages.ContainsKey(new PlanesCollectionInfo(name, string.Empty))) + { + MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + else { + _planeStorages.Add(new PlanesCollectionInfo(name, string.Empty), new PlanesGenericCollection(_pictureWidth, _pictureHeight)); + } } public void DelSet(string name) { - if (!_planeStorages.ContainsKey(name)) return; - _planeStorages.Remove(name); + PlanesGenericCollection plane; + if (_planeStorages.TryGetValue(new PlanesCollectionInfo(name, string.Empty), out plane)) + { + _planeStorages.Remove(new PlanesCollectionInfo(name, string.Empty)); + } } public PlanesGenericCollection? @@ -150,7 +164,8 @@ namespace ProjectSeaplane.Generics { get { - if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind]; + PlanesCollectionInfo infPlane = new PlanesCollectionInfo(ind, string.Empty); + if (_planeStorages.ContainsKey(infPlane)) return _planeStorages[infPlane]; return null; } } diff --git a/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs b/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs index 81b45b7..53ecd14 100644 --- a/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs +++ b/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs @@ -35,14 +35,16 @@ namespace ProjectSeaplane.Generics _maxCount = count; _places = new List(count); } + + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый автомобиль /// - public bool Insert(T plane) + public bool Insert(T plane, IEqualityComparer? equal = null) { - return Insert(plane, 0); + return Insert(plane, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,15 +52,21 @@ namespace ProjectSeaplane.Generics /// Добавляемый автомобиль /// Позиция /// - public bool Insert(T plane, int position) + public bool Insert(T plane, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new PlaneNotFoundException(position); + + if (Count > _maxCount) { throw new StorageOverflowException(Count); } + if (equal != null && _places.Contains(plane, equal)) + { + throw new ArgumentException("Добавляемый объект уже сущесвует в коллекции"); + } _places.Insert(0, plane); return true; }