diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCollectionInfo.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCollectionInfo.cs new file mode 100644 index 0000000..7ea8442 --- /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 : IEquatable + { + 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..616084a --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplaneCompareByColor.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 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); + } + 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..f0834db 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs @@ -30,6 +30,8 @@ { pictureBoxAirplanesCollection = new PictureBox(); groupBoxAirplaneWithRadar = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxCollection = new GroupBox(); buttonRemoveObject = new Button(); listBoxStorages = new ListBox(); @@ -59,10 +61,11 @@ pictureBoxAirplanesCollection.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxAirplanesCollection.TabIndex = 0; pictureBoxAirplanesCollection.TabStop = false; - pictureBoxAirplanesCollection.Click += pictureBoxAirplanesCollection_Click; // // groupBoxAirplaneWithRadar // + groupBoxAirplaneWithRadar.Controls.Add(buttonSortByColor); + groupBoxAirplaneWithRadar.Controls.Add(buttonSortByType); groupBoxAirplaneWithRadar.Controls.Add(groupBoxCollection); groupBoxAirplaneWithRadar.Controls.Add(buttonUpdateCollection); groupBoxAirplaneWithRadar.Controls.Add(buttonDeleteAirplane); @@ -71,11 +74,31 @@ 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 = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(7, 487); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(238, 29); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(6, 453); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(238, 29); + buttonSortByType.TabIndex = 5; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // groupBoxCollection // groupBoxCollection.Controls.Add(buttonRemoveObject); @@ -183,14 +206,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 +225,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); @@ -238,5 +261,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/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs index 1d4e9db..048edf7 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs @@ -28,7 +28,7 @@ namespace ProjectAirplaneWithRadar listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -64,11 +64,16 @@ 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); } + catch (ArgumentException ex) + { + Log.Warning($" {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show(" "); + } }); form.AddEvent(airplaneDelegate); } @@ -89,8 +94,6 @@ namespace ProjectAirplaneWithRadar { return; } - - try { int pos = Convert.ToInt32(maskedTextBoxNumber.Text); @@ -125,7 +128,6 @@ namespace ProjectAirplaneWithRadar } pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); } - private void buttonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) @@ -150,17 +152,11 @@ namespace ProjectAirplaneWithRadar ReloadObjects(); Log.Information($" : {name}"); } - } private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxAirplanesCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); } - - private void pictureBoxAirplanesCollection_Click(object sender, EventArgs e) - { - - } private void SaveToolStripMenuItem_Click_1(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) @@ -175,7 +171,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,9 +194,30 @@ namespace ProjectAirplaneWithRadar catch (Exception ex) { Log.Warning(" "); - MessageBox.Show($" : {ex.Message}","", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($" : {ex.Message}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } + + private void buttonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType()); + + + private void CompareAirplanes(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor()); + } } \ No newline at end of file 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; }