diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCollectionInfo.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCollectionInfo.cs new file mode 100644 index 0000000..e041ae6 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCollectionInfo.cs @@ -0,0 +1,21 @@ +namespace AirplaneWithRadar.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 (this.Name == other?.Name) { return true; } + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs new file mode 100644 index 0000000..95bdc9c --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs @@ -0,0 +1,30 @@ +using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.Entities; + +namespace AirplaneWithRadar +{ + internal class AirplaneCompareByColor : IComparer + { + public int Compare(PaintAirplane? x, PaintAirplane? y) + { + if (x == null || x.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var colorCompare = x.AirplaneEntity.BodyColor.Name.CompareTo(y.AirplaneEntity.BodyColor.Name); + if (!(x is PaintAirplaneWithRadar && y is PaintAirplaneWithRadar)) + { + return colorCompare; + } + return ((AirplaneWithRadarEntity)x.AirplaneEntity).AdditColor.Name.CompareTo(((AirplaneWithRadarEntity)y.AirplaneEntity).AdditColor.Name); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs new file mode 100644 index 0000000..cc741c7 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs @@ -0,0 +1,30 @@ +using AirplaneWithRadar.PaintObjects; + +namespace AirplaneWithRadar.Generics +{ + internal class AirplaneCompareByType : IComparer + { + public int Compare(PaintAirplane? x, PaintAirplane? y) + { + if (x == null || x.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.AirplaneEntity.Speed.CompareTo(y.AirplaneEntity.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.AirplaneEntity.Weight.CompareTo(y.AirplaneEntity.Weight); + } + + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs index 9b63331..a645398 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneNotFoundException.cs @@ -8,8 +8,7 @@ namespace AirplaneWithRadar.Exceptions public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции { i}") { } public AirplaneNotFoundException() : base() { } public AirplaneNotFoundException(string message) : base(message) { } - public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) - { } + public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { } protected AirplaneNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs index 1e39bad..0448649 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs @@ -13,6 +13,8 @@ namespace AirplaneWithRadar.Generics private readonly int placeSizeHeight = 70; private readonly SetGeneric collection; + public void SortSet(IComparer comparer) => collection.SortSet(comparer); + public AirplanesGenericCollection(int picWidth, int picHeight) { int width = picWidth / placeSizeWidth; @@ -28,7 +30,7 @@ namespace AirplaneWithRadar.Generics { return -1; } - return collect?.collection.Insert(obj); + return collect?.collection.Insert(obj, new PaintAirplaneEqutables()); } public static bool operator -(AirplanesGenericCollection collect, int pos) { @@ -84,7 +86,6 @@ namespace AirplaneWithRadar.Generics j--; } } - public IEnumerable GetAirplanes => collection.GetAirplanes(); } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs index 848eb28..06ce2a1 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs @@ -8,8 +8,8 @@ namespace AirplaneWithRadar.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 pictWidth; private readonly int pictHeight; @@ -18,7 +18,7 @@ namespace AirplaneWithRadar.Generics private static readonly char separatorForObject = ':'; public AirplanesGenericStorage(int pictureWidth, int pictureHeight) { - airplaneStorages = new Dictionary>(); + airplaneStorages = new Dictionary>(); pictWidth = pictureWidth; pictHeight = pictureHeight; } @@ -29,7 +29,7 @@ namespace AirplaneWithRadar.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in airplaneStorages) + foreach (KeyValuePair> record in airplaneStorages) { StringBuilder records = new(); foreach (PaintAirplane? elem in record.Value.GetAirplanes) @@ -99,7 +99,7 @@ namespace AirplaneWithRadar.Generics } } } - airplaneStorages.Add(name, collection); + airplaneStorages.Add(new AirplaneCollectionInfo(name, String.Empty), collection); } return; } @@ -107,20 +107,27 @@ namespace AirplaneWithRadar.Generics public void AddSet(string name) { - if (airplaneStorages.ContainsKey(name)) { return; } - airplaneStorages.Add(name, new AirplanesGenericCollection(pictWidth, pictHeight)); + var newColl = new AirplanesGenericCollection(pictWidth, pictHeight); + if (Keys.Contains(new AirplaneCollectionInfo(name, string.Empty))) + { + MessageBox.Show("Набор с таким именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, String.Empty))) { return; } + airplaneStorages.Add(new AirplaneCollectionInfo(name, String.Empty), newColl); } public void DelSet(string name) { - if (airplaneStorages.ContainsKey(name)) - airplaneStorages.Remove(name); + if (airplaneStorages.ContainsKey(new AirplaneCollectionInfo(name, String.Empty))) + airplaneStorages.Remove(new AirplaneCollectionInfo(name, String.Empty)); } public AirplanesGenericCollection? this[string ind] { get { - if (airplaneStorages.ContainsKey(ind)) return airplaneStorages[ind]; - return null; + if (!airplaneStorages.ContainsKey(new AirplaneCollectionInfo(ind, String.Empty))) + return null; + return airplaneStorages[new AirplaneCollectionInfo(ind, String.Empty)]; } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs index 4b003e6..7ac0f75 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -48,6 +48,8 @@ сохранитьToolStripMenuItem = new ToolStripMenuItem(); загрузитьToolStripMenuItem = new ToolStripMenuItem(); файлToolStripMenuItem = new ToolStripMenuItem(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); @@ -56,6 +58,8 @@ // // groupBox1 // + groupBox1.Controls.Add(buttonSortByType); + groupBox1.Controls.Add(buttonSortByColor); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(ButtonRefreshCollection); @@ -76,14 +80,14 @@ groupBox2.Controls.Add(listBoxStorages); groupBox2.Location = new Point(13, 22); groupBox2.Name = "groupBox2"; - groupBox2.Size = new Size(204, 209); + groupBox2.Size = new Size(204, 185); groupBox2.TabIndex = 5; groupBox2.TabStop = false; groupBox2.Text = "Наборы"; // // buttonDelObject // - buttonDelObject.Location = new Point(17, 178); + buttonDelObject.Location = new Point(17, 150); buttonDelObject.Name = "buttonDelObject"; buttonDelObject.Size = new Size(170, 25); buttonDelObject.TabIndex = 7; @@ -112,22 +116,22 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(10, 93); + listBoxStorages.Location = new Point(10, 80); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(188, 79); + listBoxStorages.Size = new Size(188, 64); listBoxStorages.TabIndex = 4; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(39, 289); + maskedTextBoxNumber.Location = new Point(39, 319); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(161, 23); maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(20, 370); + ButtonRefreshCollection.Location = new Point(23, 400); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(194, 46); ButtonRefreshCollection.TabIndex = 2; @@ -137,7 +141,7 @@ // // ButtonDeleteAirplane // - ButtonDeleteAirplane.Location = new Point(20, 318); + ButtonDeleteAirplane.Location = new Point(23, 348); ButtonDeleteAirplane.Name = "ButtonDeleteAirplane"; ButtonDeleteAirplane.Size = new Size(194, 46); ButtonDeleteAirplane.TabIndex = 1; @@ -147,9 +151,9 @@ // // ButtonAddAirplane // - ButtonAddAirplane.Location = new Point(23, 237); + ButtonAddAirplane.Location = new Point(30, 267); ButtonAddAirplane.Name = "ButtonAddAirplane"; - ButtonAddAirplane.Size = new Size(194, 46); + ButtonAddAirplane.Size = new Size(181, 46); ButtonAddAirplane.TabIndex = 0; ButtonAddAirplane.Text = "Добавить самолёт"; ButtonAddAirplane.UseVisualStyleBackColor = true; @@ -192,14 +196,14 @@ // сохранитьToolStripMenuItem1 // сохранитьToolStripMenuItem1.Name = "сохранитьToolStripMenuItem1"; - сохранитьToolStripMenuItem1.Size = new Size(180, 22); + сохранитьToolStripMenuItem1.Size = new Size(133, 22); сохранитьToolStripMenuItem1.Text = "Сохранить"; сохранитьToolStripMenuItem1.Click += SaveToolStripMenuItem_Click; // // загрузитьToolStripMenuItem1 // загрузитьToolStripMenuItem1.Name = "загрузитьToolStripMenuItem1"; - загрузитьToolStripMenuItem1.Size = new Size(180, 22); + загрузитьToolStripMenuItem1.Size = new Size(133, 22); загрузитьToolStripMenuItem1.Text = "Загрузить"; загрузитьToolStripMenuItem1.Click += LoadToolStripMenuItem_Click; // @@ -224,6 +228,26 @@ файлToolStripMenuItem.Size = new Size(48, 20); файлToolStripMenuItem.Text = "Файл"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(125, 223); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(86, 38); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(31, 223); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(88, 38); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // FormAirplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -267,5 +291,7 @@ private ToolStripMenuItem файлToolStripMenuItem; private ToolStripMenuItem сохранитьToolStripMenuItem1; private ToolStripMenuItem загрузитьToolStripMenuItem1; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs index 8ae04ce..2256519 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs @@ -1,7 +1,7 @@ using AirplaneWithRadar.Generics; using AirplaneWithRadar.Exceptions; using Microsoft.Extensions.Logging; - +using AirplaneWithRadar.PaintObjects; namespace AirplaneWithRadar { @@ -23,7 +23,7 @@ namespace AirplaneWithRadar 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)) { @@ -109,6 +109,11 @@ namespace AirplaneWithRadar MessageBox.Show($"Ошибка при добавлении: {ex.Message}"); logger.LogWarning($"Ошибка: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show($"Ошибка при добавлении: {ex.Message}"); + logger.LogWarning($"Ошибка: {ex.Message}"); + } }); formAirplaneConfig.Show(); } @@ -203,9 +208,29 @@ namespace AirplaneWithRadar MessageBoxButtons.OK, MessageBoxIcon.Error); logger.LogWarning($"Ошибка при загрузке: {ex.Message}"); } - + } } + + 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.SortSet(comparer); + pictureBoxCollection.Image = obj.ShowAirplanes(); + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx index 7bf303f..3871620 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneEqutables.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneEqutables.cs new file mode 100644 index 0000000..9fc7236 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneEqutables.cs @@ -0,0 +1,58 @@ +using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace AirplaneWithRadar.Generics; + +internal class PaintAirplaneEqutables : IEqualityComparer +{ + public bool Equals(PaintAirplane? x, PaintAirplane? y) + { + if (x == null || x.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.AirplaneEntity == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.AirplaneEntity.Speed != y.AirplaneEntity.Speed) + { + return false; + } + if (x.AirplaneEntity.Weight != y.AirplaneEntity.Weight) + { + return false; + } + if (x.AirplaneEntity.BodyColor != y.AirplaneEntity.BodyColor) + { + return false; + } + if (x is PaintAirplaneWithRadar && y is PaintAirplaneWithRadar) + { + AirplaneWithRadarEntity xEntity = (AirplaneWithRadarEntity)x.AirplaneEntity; + AirplaneWithRadarEntity yEntity = (AirplaneWithRadarEntity)x.AirplaneEntity; + if (xEntity.AdditColor != yEntity.AdditColor) + { + return false; + } + if (xEntity.AdditFuelPod != yEntity.AdditFuelPod) + { + return false; + } + if (xEntity.RadarOnBoard != yEntity.AdditFuelPod) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] PaintAirplane obj) + { + return obj.GetHashCode(); + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs index 23a317a..9f5f0d0 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs @@ -1,4 +1,5 @@ using AirplaneWithRadar.Exceptions; +using System.Collections.Generic; namespace AirplaneWithRadar.Generics; @@ -14,7 +15,9 @@ internal class SetGeneric places = new List(count); } - public int Insert(T airplane) + public void SortSet(IComparer comparer) => places.Sort(comparer); + + public int Insert(T airplane, IEqualityComparer? equal = null) { if (Count == 0) { @@ -24,8 +27,11 @@ internal class SetGeneric int emptyPosition = -1; for (int i = 0; i < Count; i++) { - if (places[i] == null) + if (places[i] is null) { + if (equal is not null && places.Contains(airplane, equal)) + throw new ArgumentException("Объект уже существует"); + emptyPosition = i; break; } @@ -37,13 +43,16 @@ internal class SetGeneric if (emptyPosition < 0) { + if (equal is not null && places.Contains(airplane, equal)) + throw new ArgumentException("Объект уже существует"); + places.Add(airplane); return 0; } Insert(airplane, emptyPosition); return 0; } - public int Insert(T airplane, int position) + public int Insert(T airplane, int position, IEqualityComparer? equal = null) { if (position > maxCount) { @@ -53,8 +62,11 @@ internal class SetGeneric { return -1; } - if (places[position] == null) + if (places[position] is null) { + if (equal is not null && places.Contains(airplane, equal)) + throw new ArgumentException("Объект уже существует"); + places[position] = airplane; return position; } @@ -62,12 +74,15 @@ internal class SetGeneric int emptyPosition = -1; for (int i = position + 1; i < Count; i++) { - if (places[i]==null) + if (places[i] is null) { + if (equal is not null && places.Contains(airplane, equal)) + throw new ArgumentException("Объект уже существует"); + emptyPosition = i; break; } - } + } if (emptyPosition < 0) { return -1;