diff --git a/AircraftCarrier/AircraftCarrier/DrawingWarshipEqutables.cs b/AircraftCarrier/AircraftCarrier/DrawingWarshipEqutables.cs new file mode 100644 index 0000000..09747de --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/DrawingWarshipEqutables.cs @@ -0,0 +1,54 @@ +using ProjectAircraftCarrier.DrawingObjects; +using ProjectAircraftCarrier.Entities; +using System.Diagnostics.CodeAnalysis; +namespace ProjectAircraftCarrier.Generics +{ + internal class DrawingWarshipEqutables : IEqualityComparer + { + public bool Equals(DrawingWarship? x, DrawingWarship? y) + { + if (x == null || x.EntityWarship == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityWarship == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityWarship.Speed != y.EntityWarship.Speed) + { + return false; + } + if (x.EntityWarship.Weight != y.EntityWarship.Weight) + { + return false; + } + if (x.EntityWarship.BodyColor != y.EntityWarship.BodyColor) + { + return false; + } + if (x is DrawingAircraftCarrier && y is DrawingAircraftCarrier) + { + EntityAircraftCarrier EntityX = + (EntityAircraftCarrier)x.EntityWarship; + EntityAircraftCarrier EntityY = + (EntityAircraftCarrier)y.EntityWarship; + if (EntityX.Runway != EntityY.Runway) + return false; + if (EntityX.Cabin != EntityY.Cabin) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingWarship obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs index 0235c56..a686237 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBoxCollectionTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxCollectionSets = new GroupBox(); textBoxStorageName = new TextBox(); buttonDelObject = new Button(); @@ -43,8 +45,8 @@ fileToolStripMenuItem = new ToolStripMenuItem(); saveToolStripMenuItem = new ToolStripMenuItem(); loadToolStripMenuItem = new ToolStripMenuItem(); - openFileDialog = new System.Windows.Forms.OpenFileDialog(); - saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); groupBoxCollectionTools.SuspendLayout(); groupBoxCollectionSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); @@ -54,6 +56,8 @@ // groupBoxCollectionTools // groupBoxCollectionTools.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxCollectionTools.Controls.Add(buttonSortByColor); + groupBoxCollectionTools.Controls.Add(buttonSortByType); groupBoxCollectionTools.Controls.Add(groupBoxCollectionSets); groupBoxCollectionTools.Controls.Add(maskedTextBoxNumber); groupBoxCollectionTools.Controls.Add(buttonRefreshCollection); @@ -68,6 +72,26 @@ groupBoxCollectionTools.TabStop = false; groupBoxCollectionTools.Text = "Tools"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(15, 408); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(218, 41); + buttonSortByColor.TabIndex = 7; + buttonSortByColor.Text = "Sort by color"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(15, 361); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(218, 41); + buttonSortByType.TabIndex = 6; + buttonSortByType.Text = "Sort by type"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // groupBoxCollectionSets // groupBoxCollectionSets.Controls.Add(textBoxStorageName); @@ -120,7 +144,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(56, 468); + maskedTextBoxNumber.Location = new Point(56, 523); maskedTextBoxNumber.Margin = new Padding(4, 5, 4, 5); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(141, 31); @@ -140,7 +164,7 @@ // // buttonRemoveWarship // - buttonRemoveWarship.Location = new Point(9, 509); + buttonRemoveWarship.Location = new Point(9, 564); buttonRemoveWarship.Margin = new Padding(4, 5, 4, 5); buttonRemoveWarship.Name = "buttonRemoveWarship"; buttonRemoveWarship.Size = new Size(230, 45); @@ -151,7 +175,7 @@ // // buttonAddWarship // - buttonAddWarship.Location = new Point(9, 413); + buttonAddWarship.Location = new Point(9, 468); buttonAddWarship.Margin = new Padding(4, 5, 4, 5); buttonAddWarship.Name = "buttonAddWarship"; buttonAddWarship.Size = new Size(230, 45); @@ -169,12 +193,12 @@ pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // - // menuStrip1 + // menuStrip // menuStrip.ImageScalingSize = new Size(24, 24); menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); menuStrip.Location = new Point(0, 0); - menuStrip.Name = "menuStrip1"; + menuStrip.Name = "menuStrip"; menuStrip.Size = new Size(1143, 33); menuStrip.TabIndex = 2; menuStrip.Text = "menuStrip1"; @@ -250,5 +274,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/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs index ab619a2..68dda9b 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs @@ -37,7 +37,7 @@ namespace ProjectAircraftCarrier 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)) @@ -133,10 +133,18 @@ namespace ProjectAircraftCarrier catch (StorageOverflowException ex) { _logger.LogWarning($"Collection " + - $"{listBoxStorages.SelectedItem.ToString() ?? string.Empty} " + + $"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}" + $"is full"); MessageBox.Show(ex.Message); } + catch (ArgumentException) + { + _logger.LogWarning($"Object being added already exists in" + + $" the collection " + + $"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Object being added already exists in " + + "the collection"); + } }); form.AddEvent(warshipDelegate); } @@ -263,5 +271,37 @@ namespace ProjectAircraftCarrier } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => + CompareWarships(new WarshipCompareByType()); + /// + /// Сортировка по цвету + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => + CompareWarships(new WarshipCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void CompareWarships(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.ShowWarships(); + } } } diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx index a0623c8..b3e3a23 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx @@ -117,7 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17 + + 165, 17 + + + 351, 17 + \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetGeneric.cs b/AircraftCarrier/AircraftCarrier/SetGeneric.cs index d6fe64a..eb8973e 100644 --- a/AircraftCarrier/AircraftCarrier/SetGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetGeneric.cs @@ -33,41 +33,44 @@ namespace ProjectAircraftCarrier.Generics /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемый корабль /// - public bool Insert(T warship) + public void Insert(T warship, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(warship, 0); - return true; + Insert(warship, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль + /// Добавляемый корабль /// Позиция /// - public bool Insert(T warship, int position) + public void Insert(T warship, int position, IEqualityComparer? + equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) - return false; + throw new Exception("Incorrect insertion position"); + if (equal != null) + { + if (_places.Contains(warship, equal)) + throw new ArgumentException(nameof(warship)); + } _places.Insert(position, warship); - return true; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public void Remove(int position) { if (!(position >= 0 && position < Count)) throw new WarshipNotFoundException(position); _places.RemoveAt(position); - return true; } /// /// Получение объекта из набора по позиции @@ -84,11 +87,9 @@ namespace ProjectAircraftCarrier.Generics } set { - if (!(position >= 0 && position < Count && _places.Count < - _maxCount)) + if (!(position >= 0 && position < Count)) return; _places.Insert(position, value); - return; } } /// @@ -104,5 +105,10 @@ namespace ProjectAircraftCarrier.Generics yield break; } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipCompareByColor.cs b/AircraftCarrier/AircraftCarrier/WarshipCompareByColor.cs new file mode 100644 index 0000000..7d447f9 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/WarshipCompareByColor.cs @@ -0,0 +1,29 @@ +using ProjectAircraftCarrier.DrawingObjects; +namespace ProjectAircraftCarrier.Generics +{ + internal class WarshipCompareByColor : IComparer + { + public int Compare(DrawingWarship? x, DrawingWarship? y) + { + if (x == null || x.EntityWarship == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityWarship == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityWarship.BodyColor.Name != + y.EntityWarship.BodyColor.Name) + { + return x.EntityWarship.BodyColor.Name.CompareTo + (y.EntityWarship.BodyColor.Name); + } + + var speedCompare = x.EntityWarship.Speed.CompareTo + (y.EntityWarship.Speed); + if (speedCompare != 0) + return speedCompare; + + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipCompareByType.cs b/AircraftCarrier/AircraftCarrier/WarshipCompareByType.cs new file mode 100644 index 0000000..1df056a --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/WarshipCompareByType.cs @@ -0,0 +1,29 @@ +using ProjectAircraftCarrier.DrawingObjects; +namespace ProjectAircraftCarrier.Generics +{ + internal class WarshipCompareByType : IComparer + { + public int Compare(DrawingWarship? x, DrawingWarship? y) + { + if (x == null || x.EntityWarship == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityWarship == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityWarship.Speed.CompareTo(y.EntityWarship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/WarshipsCollectionInfo.cs b/AircraftCarrier/AircraftCarrier/WarshipsCollectionInfo.cs new file mode 100644 index 0000000..8cec672 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/WarshipsCollectionInfo.cs @@ -0,0 +1,24 @@ +namespace ProjectAircraftCarrier.Generics +{ + internal class WarshipsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public WarshipsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(WarshipsCollectionInfo? other) + { + if (other == null || other.Name == null) + throw new ArgumentNullException(nameof(other)); + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} + diff --git a/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs index 4e769f3..5ad19e0 100644 --- a/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs @@ -56,7 +56,8 @@ namespace ProjectAircraftCarrier.Generics { if (obj == null || collect == null) return false; - return collect._collection.Insert(obj); + collect._collection.Insert(obj, new DrawingWarshipEqutables()); + return true; } /// /// Перегрузка оператора вычитания @@ -138,5 +139,6 @@ namespace ProjectAircraftCarrier.Generics i++; } } + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs index 48814a2..f968adb 100644 --- a/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs +++ b/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs @@ -11,12 +11,13 @@ namespace ProjectAircraftCarrier.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _warshipStorages; + readonly Dictionary> _warshipStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _warshipStorages.Keys.ToList(); + public List Keys => + _warshipStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -44,7 +45,7 @@ namespace ProjectAircraftCarrier.Generics /// public WarshipsGenericStorage(int pictureWidth, int pictureHeight) { - _warshipStorages = new Dictionary>(); _pictureWidth = pictureWidth; @@ -56,11 +57,13 @@ namespace ProjectAircraftCarrier.Generics /// Название набора public void AddSet(string name) { - if (!_warshipStorages.ContainsKey(name)) + if (!_warshipStorages.ContainsKey(new WarshipsCollectionInfo(name, + string.Empty))) { - _warshipStorages.Add(name, new WarshipsGenericCollection - (_pictureWidth, - _pictureHeight)); + _warshipStorages.Add(new WarshipsCollectionInfo(name, + string.Empty), new WarshipsGenericCollection + + (_pictureWidth, _pictureHeight)); } else { @@ -73,9 +76,10 @@ namespace ProjectAircraftCarrier.Generics /// Название набора public void DelSet(string name) { - if (!_warshipStorages.ContainsKey(name)) + if (!_warshipStorages.ContainsKey(new WarshipsCollectionInfo(name, + string.Empty))) return; - _warshipStorages.Remove(name); + _warshipStorages.Remove(new WarshipsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -87,8 +91,10 @@ namespace ProjectAircraftCarrier.Generics { get { - if (_warshipStorages.ContainsKey(ind)) - return _warshipStorages[ind]; + WarshipsCollectionInfo indObj = new WarshipsCollectionInfo(ind, + string.Empty); + if (_warshipStorages.ContainsKey(indObj)) + return _warshipStorages[indObj]; return null; } } @@ -104,7 +110,8 @@ namespace ProjectAircraftCarrier.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _warshipStorages) { @@ -115,7 +122,7 @@ namespace ProjectAircraftCarrier.Generics $"{elem?.GetDataForSave(_separatorForObject)}" + $"{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}" + + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}" + $"{records}"); } if (data.Length == 0) @@ -124,7 +131,7 @@ namespace ProjectAircraftCarrier.Generics } using FileStream fs = new(filename, FileMode.Create); byte[] info = new UTF8Encoding(true).GetBytes - ($"CarStorage{Environment.NewLine}{data}"); + ($"WarshipStorage{Environment.NewLine}{data}"); fs.Write(info, 0, info.Length); return; } @@ -185,7 +192,8 @@ namespace ProjectAircraftCarrier.Generics } } } - _warshipStorages.Add(record[0], collection); + _warshipStorages.Add(new WarshipsCollectionInfo(record[0], + string.Empty), collection); } } }