diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCollectionInfo.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCollectionInfo.cs new file mode 100644 index 0000000..84758d8 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCollectionInfo.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Generics +{ + internal class AirplaneCollectionInfo + { + public string Name { get; private set; } + public string Description { get; private set; } + public AirplaneCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(AirplaneCollectionInfo? other) + { + if (other != null) + { + return Name == other.Name; + } + else + { + return false; + } + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByColor.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByColor.cs new file mode 100644 index 0000000..114498b --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByColor.cs @@ -0,0 +1,49 @@ +using ProjectAirplaneWithRadar.DrawningObjects; +using ProjectAirplaneWithRadar.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Generics +{ + internal class AirplaneCompareByColor : IComparer + { + public int Compare(DrawningAirplane? x, DrawningAirplane? y) + { + if (x == null || x.EntityAirplane == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirplane == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityAirplane.BodyColor.Name != y.EntityAirplane.BodyColor.Name) + { + return x.EntityAirplane.BodyColor.Name.CompareTo(y.EntityAirplane.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawningAirplane) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningAirplaneWithRadar) + { + EntityAirplaneWithRadar EntityX = (EntityAirplaneWithRadar)x.EntityAirplane; + EntityAirplaneWithRadar EntityY = (EntityAirplaneWithRadar)y.EntityAirplane; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = x.EntityAirplane.Speed.CompareTo(y.EntityAirplane.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirplane.Weight.CompareTo(y.EntityAirplane.Weight); + } + } +} diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByType.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByType.cs new file mode 100644 index 0000000..5ef29d1 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByType.cs @@ -0,0 +1,33 @@ +using ProjectAirplaneWithRadar.DrawningObjects; +using ProjectAirplaneWithRadar.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Generics +{ + internal class AirplaneCompareByType : IComparer + { + public int Compare(DrawningAirplane? x, DrawningAirplane? y) + { + if (x == null || x.EntityAirplane == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirplane == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityAirplane.Speed.CompareTo(y.EntityAirplane.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirplane.Weight.CompareTo(y.EntityAirplane.Weight); + } + } +} diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs index 917d53c..8cff206 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs @@ -13,6 +13,7 @@ namespace ProjectAirplaneWithRadar.Generics private readonly int _placeSizeWidth = 215; private readonly int _placeSizeHeight = 90; private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public AirplanesGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -28,7 +29,7 @@ namespace ProjectAirplaneWithRadar.Generics { return false; } - return collect?._collection.Insert(obj)??false; + return collect?._collection.Insert(obj, new DrawningAirplanesEqutables()) ?? false; } public static bool operator -(AirplanesGenericCollection collect, int pos) { diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs index db8ed75..9595be7 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs @@ -11,8 +11,8 @@ namespace ProjectAirplaneWithRadar.Generics { internal class AirplanesGenericStorage { - readonly Dictionary> _airplanesStorages; - public List Keys => _airplanesStorages.Keys.ToList(); + readonly Dictionary> _airplanesStorages; + public List Keys => _airplanesStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; private static readonly char _separatorForKeyValue = '|'; @@ -20,7 +20,7 @@ namespace ProjectAirplaneWithRadar.Generics private static readonly char _separatorForObject = ':'; public AirplanesGenericStorage(int pictureWidth, int pictureHeight) { - _airplanesStorages = new Dictionary>(); + _airplanesStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -31,7 +31,7 @@ namespace ProjectAirplaneWithRadar.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _airplanesStorages) { StringBuilder records = new(); @@ -39,7 +39,7 @@ namespace ProjectAirplaneWithRadar.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -98,7 +98,7 @@ namespace ProjectAirplaneWithRadar.Generics } } } - _airplanesStorages.Add(record[0], collection); + _airplanesStorages.Add(new AirplaneCollectionInfo (record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); } @@ -106,21 +106,22 @@ namespace ProjectAirplaneWithRadar.Generics } public void AddSet(string name) { - _airplanesStorages.Add(name, new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); + _airplanesStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_airplanesStorages.ContainsKey(name)) + if (!_airplanesStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty))) return; - _airplanesStorages.Remove(name); + _airplanesStorages.Remove(new AirplaneCollectionInfo(name, string.Empty)); } public AirplanesGenericCollection?this[string ind] { get { - if (_airplanesStorages.ContainsKey(ind)) - return _airplanesStorages[ind]; - return null; + AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty); + if (_airplanesStorages.ContainsKey(indObj)) + return _airplanesStorages[indObj]; + return null; } } } diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplanesEqutable.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplanesEqutable.cs new file mode 100644 index 0000000..28f56a2 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/DrawningAirplanesEqutable.cs @@ -0,0 +1,59 @@ +using ProjectAirplaneWithRadar.DrawningObjects; +using ProjectAirplaneWithRadar.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Generics +{ + internal class DrawningAirplanesEqutables : IEqualityComparer + { + public bool Equals(DrawningAirplane? x, DrawningAirplane? y) + { + if (x == null || x.EntityAirplane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAirplane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAirplane.Speed != y.EntityAirplane.Speed) + { + return false; + } + if (x.EntityAirplane.Weight != y.EntityAirplane.Weight) + { + return false; + } + if (x.EntityAirplane.BodyColor != y.EntityAirplane.BodyColor) + { + return false; + } + if (x is DrawningAirplaneWithRadar && y is DrawningAirplaneWithRadar) + { + EntityAirplaneWithRadar EntityX = (EntityAirplaneWithRadar)x.EntityAirplane; + EntityAirplaneWithRadar EntityY = (EntityAirplaneWithRadar)y.EntityAirplane; + if (EntityX.Radar != EntityY.Radar) + return false; + if (EntityX.DopBak != EntityY.DopBak) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningAirplane obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs index 4496e0a..5e9ac67 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs @@ -71,7 +71,7 @@ groupBoxAirplaneWithRadar.Dock = DockStyle.Right; groupBoxAirplaneWithRadar.Location = new Point(650, 28); groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar"; - groupBoxAirplaneWithRadar.Size = new Size(251, 464); + groupBoxAirplaneWithRadar.Size = new Size(251, 522); groupBoxAirplaneWithRadar.TabIndex = 1; groupBoxAirplaneWithRadar.TabStop = false; groupBoxAirplaneWithRadar.Text = "Инструменты"; @@ -183,14 +183,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(224, 26); + SaveToolStripMenuItem.Size = new Size(166, 26); SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(224, 26); + LoadToolStripMenuItem.Size = new Size(166, 26); LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -202,7 +202,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(901, 492); + ClientSize = new Size(901, 550); Controls.Add(groupBoxAirplaneWithRadar); Controls.Add(pictureBoxAirplanesCollection); Controls.Add(menuStrip1); diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs index 1d4e9db..58a630c 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs @@ -64,7 +64,7 @@ namespace ProjectAirplaneWithRadar Log.Information($" {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); } - catch(StorageOverflowException ex) + catch (StorageOverflowException ex) { Log.Warning($" {listBoxStorages.SelectedItem.ToString() ?? string.Empty} "); MessageBox.Show(ex.Message); @@ -89,8 +89,8 @@ namespace ProjectAirplaneWithRadar { return; } - - + + try { int pos = Convert.ToInt32(maskedTextBoxNumber.Text); @@ -175,7 +175,7 @@ namespace ProjectAirplaneWithRadar catch (Exception ex) { Log.Warning(" "); - MessageBox.Show($" : {ex.Message}","", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($" : {ex.Message}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -198,7 +198,7 @@ namespace ProjectAirplaneWithRadar catch (Exception ex) { Log.Warning(" "); - MessageBox.Show($" : {ex.Message}","", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($" : {ex.Message}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs index 16f3f1a..ad035b7 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs @@ -19,19 +19,25 @@ namespace ProjectAirplaneWithRadar.Generics _maxCount = count; _places = new List(count); } - public bool Insert(T airplane) + public void SortSet(IComparer comparer) => _places.Sort(comparer); + public bool Insert(T airplane, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(airplane, 0); + Insert(airplane, 0, equal); return true; } - public bool Insert(T airplane, int position) + public bool Insert(T airplane, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) return false; + if (equal != null) + { + if (_places.Contains(airplane, equal)) + throw new ArgumentException(nameof(airplane)); + } _places.Insert(position, airplane); return true; }