diff --git a/Sailboat/Sailboat/BoatCompareByColor.cs b/Sailboat/Sailboat/BoatCompareByColor.cs new file mode 100644 index 0000000..1cb511e --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByColor.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + internal class BoatCompareByColor + { + } +} diff --git a/Sailboat/Sailboat/BoatCompareByType.cs b/Sailboat/Sailboat/BoatCompareByType.cs new file mode 100644 index 0000000..6b51faa --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByType.cs @@ -0,0 +1,35 @@ +using Sailboat.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + internal class BoatCompareByType : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/DrawingBoatEqutables.cs b/Sailboat/Sailboat/DrawingBoatEqutables.cs new file mode 100644 index 0000000..e66d5c0 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoatEqutables.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class DrawingBoatEqutables : IEqualityComparer + { + public bool Equals(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBoat.Speed != y.EntityBoat.Speed) + { + return false; + } + if (x.EntityBoat.Weight != y.EntityBoat.Weight) + { + return false; + } + if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor) + { + return false; + } + if (x is DrawingSailboat && y is DrawingSailboat) + { + EntitySailboat EntityX = (EntitySailboat)x.EntityBoat; + EntitySailboat EntityY = (EntitySailboat)y.EntityBoat; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.Hull != EntityY.Hull) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingBoat obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 76c12fe..7525a4a 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -88,21 +88,6 @@ namespace Sailboat MessageBox.Show("Не удалось добавить объект"); _logger.LogInformation($"Не удалось добавить объект"); } - //try - //{ - // if (obj + drawingBoat) - // { - // MessageBox.Show("Объект добавлен"); - // pictureBoxCollection.Image = obj.ShowBoats(); - // _logger.LogInformation($"Объект {obj.GetType()} добавлен"); - // } - //} - //catch (StorageOverflowException ex) - //{ - // MessageBox.Show(ex.Message); - // MessageBox.Show("Не удалось добавить объект"); - // _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); - //} } private void buttonRemoveBoat_Click(object sender, EventArgs e) diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index 05134f9..77b93ab 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -36,13 +36,13 @@ namespace Sailboat.Generics /// /// Добавляемая лодка /// - public bool Insert(T boat) + public bool Insert(T boat, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) - { - return false; + { + throw new StorageOverflowException(_maxCount); } - Insert(boat, 0); + Insert(boat, 0, equal); return true; } /// @@ -51,13 +51,21 @@ namespace Sailboat.Generics /// Добавляемая лодка /// Позиция /// - public bool Insert(T boat, int position) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new BoatNotFoundException(position); if (_places.Count >= _maxCount) throw new StorageOverflowException(_maxCount); + + //вот это проверить перед отправкой хз что делает + if (equal != null) + { + if (_places.Contains(boat, equal)) + throw new ArgumentException(nameof(boat)); + } + _places.Insert(0, boat); return true; }