diff --git a/AirFighter/AirplaneCompareByColor.cs b/AirFighter/AirplaneCompareByColor.cs new file mode 100644 index 0000000..260c358 --- /dev/null +++ b/AirFighter/AirplaneCompareByColor.cs @@ -0,0 +1,49 @@ +using ProjectAirFighter.DrawningObjects; +using ProjectAirFighter.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.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.GetBrightness() != y.EntityAirplane.BodyColor.GetBrightness()) + { + return x.EntityAirplane.BodyColor.GetBrightness().CompareTo(y.EntityAirplane.BodyColor.GetBrightness()); + } + 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 DrawningAirFighter) + { + EntityAirFighter EntityX = (EntityAirFighter)x.EntityAirplane; + EntityAirFighter EntityY = (EntityAirFighter)y.EntityAirplane; + if (EntityX.AdditionalColor.GetBrightness() != EntityY.AdditionalColor.GetBrightness()) + { + return EntityX.AdditionalColor.GetBrightness().CompareTo(EntityY.AdditionalColor.GetBrightness()); + } + } + 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/AirFighter/AirplaneCompareByType.cs b/AirFighter/AirplaneCompareByType.cs new file mode 100644 index 0000000..f94ac39 --- /dev/null +++ b/AirFighter/AirplaneCompareByType.cs @@ -0,0 +1,32 @@ +using ProjectAirFighter.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.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/AirFighter/AirplanesCollectionInfo.cs b/AirFighter/AirplanesCollectionInfo.cs new file mode 100644 index 0000000..b93b2f7 --- /dev/null +++ b/AirFighter/AirplanesCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.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) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/AirFighter/AirplaneGenericCollection.cs b/AirFighter/AirplanesGenericCollection.cs similarity index 93% rename from AirFighter/AirplaneGenericCollection.cs rename to AirFighter/AirplanesGenericCollection.cs index 4a08e12..c6a8be2 100644 --- a/AirFighter/AirplaneGenericCollection.cs +++ b/AirFighter/AirplanesGenericCollection.cs @@ -1,6 +1,7 @@ using ProjectAirFighter.MovementStrategy; using ProjectAirFighter.DrawningObjects; using System.Drawing; +using Microsoft.VisualBasic.Logging; namespace ProjectAirFighter.Generics { @@ -19,6 +20,8 @@ namespace ProjectAirFighter.Generics private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + public AirplaneslGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -33,7 +36,7 @@ namespace ProjectAirFighter.Generics { if (obj == null) return false; - return collect?._collection.Insert(obj) ?? false; + return collect?._collection.Insert(obj, new DrawningAirplaneEqutables()) ?? false; } public static T? operator -(AirplaneslGenericCollection collect, int diff --git a/AirFighter/AirplanesGenericStorage.cs b/AirFighter/AirplanesGenericStorage.cs index 7bfd6a9..0596773 100644 --- a/AirFighter/AirplanesGenericStorage.cs +++ b/AirFighter/AirplanesGenericStorage.cs @@ -12,8 +12,9 @@ namespace ProjectAirFighter.Generics { internal class AirplanesGenericStorage { - readonly Dictionary> _airplaneStorages; - public List Keys => _airplaneStorages.Keys.ToList(); + readonly Dictionary> _airplaneStorages; + public List Keys => _airplaneStorages.Keys.ToList(); + private readonly int _pictureWidth; @@ -24,7 +25,7 @@ namespace ProjectAirFighter.Generics private static readonly char _separatorForObject = ':'; public AirplanesGenericStorage(int pictureWidth, int pictureHeight) { - _airplaneStorages = new Dictionary>(); + _airplaneStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -35,7 +36,7 @@ namespace ProjectAirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _airplaneStorages) { StringBuilder records = new(); @@ -43,7 +44,7 @@ namespace ProjectAirFighter.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -111,7 +112,8 @@ StringSplitOptions.RemoveEmptyEntries); } } } - _airplaneStorages.Add(record[0], collection); + _airplaneStorages.Add(new AirplaneCollectionInfo(record[0], string.Empty), collection); + str = sr.ReadLine(); } while (str != null); @@ -120,22 +122,23 @@ StringSplitOptions.RemoveEmptyEntries); } public void AddSet(string name) { - _airplaneStorages.Add(name, new AirplaneslGenericCollection(_pictureWidth, _pictureHeight)); + _airplaneStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplaneslGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_airplaneStorages.ContainsKey(name)) + if (!_airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty))) return; - _airplaneStorages.Remove(name); + _airplaneStorages.Remove(new AirplaneCollectionInfo(name, string.Empty)); } public AirplaneslGenericCollection? this[string ind] { get { - if (_airplaneStorages.ContainsKey(ind)) - return _airplaneStorages[ind]; + AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty); + if (_airplaneStorages.ContainsKey(indObj)) + return _airplaneStorages[indObj]; return null; } } diff --git a/AirFighter/DrawiningAirplaneEqutables.cs b/AirFighter/DrawiningAirplaneEqutables.cs new file mode 100644 index 0000000..6480900 --- /dev/null +++ b/AirFighter/DrawiningAirplaneEqutables.cs @@ -0,0 +1,59 @@ +using ProjectAirFighter.DrawningObjects; +using ProjectAirFighter.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirFighter.Generics +{ + internal class DrawningAirplaneEqutables : 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 DrawningAirFighter && y is DrawningAirFighter) + { + EntityAirFighter EntityX = (EntityAirFighter)x.EntityAirplane; + EntityAirFighter EntityY = (EntityAirFighter)y.EntityAirplane; + if (EntityX.Racket != EntityY.Racket) + return false; + if (EntityX.Wing != EntityY.Wing) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningAirplane obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/AirFighter/FormAirplaneCollection.Designer.cs b/AirFighter/FormAirplaneCollection.Designer.cs index da9ef7a..64c991f 100644 --- a/AirFighter/FormAirplaneCollection.Designer.cs +++ b/AirFighter/FormAirplaneCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.toolGroupBox = new System.Windows.Forms.GroupBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.kitGroupbox = new System.Windows.Forms.GroupBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); @@ -53,6 +55,8 @@ // // toolGroupBox // + this.toolGroupBox.Controls.Add(this.ButtonSortByColor); + this.toolGroupBox.Controls.Add(this.ButtonSortByType); this.toolGroupBox.Controls.Add(this.maskedTextBoxNumber); this.toolGroupBox.Controls.Add(this.kitGroupbox); this.toolGroupBox.Controls.Add(this.updateCollectionButton); @@ -66,9 +70,29 @@ this.toolGroupBox.TabStop = false; this.toolGroupBox.Text = "Инструменты"; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(8, 409); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(213, 35); + this.ButtonSortByColor.TabIndex = 10; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(8, 370); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(216, 33); + this.ButtonSortByType.TabIndex = 9; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(7, 406); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(6, 496); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(215, 27); this.maskedTextBoxNumber.TabIndex = 7; @@ -125,7 +149,7 @@ // // updateCollectionButton // - this.updateCollectionButton.Location = new System.Drawing.Point(6, 505); + this.updateCollectionButton.Location = new System.Drawing.Point(7, 575); this.updateCollectionButton.Name = "updateCollectionButton"; this.updateCollectionButton.Size = new System.Drawing.Size(215, 40); this.updateCollectionButton.TabIndex = 3; @@ -135,7 +159,7 @@ // // deleteAirplaneButton // - this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 449); + this.deleteAirplaneButton.Location = new System.Drawing.Point(6, 529); this.deleteAirplaneButton.Name = "deleteAirplaneButton"; this.deleteAirplaneButton.Size = new System.Drawing.Size(215, 40); this.deleteAirplaneButton.TabIndex = 2; @@ -145,7 +169,7 @@ // // addAirplaneButton // - this.addAirplaneButton.Location = new System.Drawing.Point(8, 360); + this.addAirplaneButton.Location = new System.Drawing.Point(6, 450); this.addAirplaneButton.Name = "addAirplaneButton"; this.addAirplaneButton.Size = new System.Drawing.Size(215, 40); this.addAirplaneButton.TabIndex = 0; @@ -244,5 +268,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/AirFighter/FormAirplaneCollection.cs b/AirFighter/FormAirplaneCollection.cs index 5c39787..6c41b25 100644 --- a/AirFighter/FormAirplaneCollection.cs +++ b/AirFighter/FormAirplaneCollection.cs @@ -78,7 +78,7 @@ namespace ProjectAirFighter 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)) @@ -197,6 +197,11 @@ namespace ProjectAirFighter 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); } @@ -224,5 +229,24 @@ MessageBoxIcon.Question) == DialogResult.Yes) } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor()); + 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); + pictureBoxCollection.Image = obj.ShowAirplanes(); + } } } diff --git a/AirFighter/SetGeneric.cs b/AirFighter/SetGeneric.cs index da972ac..152c5a7 100644 --- a/AirFighter/SetGeneric.cs +++ b/AirFighter/SetGeneric.cs @@ -22,21 +22,28 @@ namespace ProjectAirFighter.Generics _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; }