diff --git a/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/FormCruiserCollection.Designer.cs index 1fd64c8..d4a365c 100644 --- a/Cruiser/FormCruiserCollection.Designer.cs +++ b/Cruiser/FormCruiserCollection.Designer.cs @@ -45,6 +45,8 @@ UploadToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + buttonSortColor = new Button(); + buttonSortType = new Button(); groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); groupBoxStorage.SuspendLayout(); @@ -110,6 +112,8 @@ // // groupBoxStorage // + groupBoxStorage.Controls.Add(buttonSortType); + groupBoxStorage.Controls.Add(buttonSortColor); groupBoxStorage.Controls.Add(listBoxStorages); groupBoxStorage.Controls.Add(buttonDelObject); groupBoxStorage.Controls.Add(textBoxStorageName); @@ -127,7 +131,7 @@ listBoxStorages.ItemHeight = 15; listBoxStorages.Location = new Point(10, 117); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(120, 124); + listBoxStorages.Size = new Size(120, 64); listBoxStorages.TabIndex = 7; listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // @@ -198,6 +202,26 @@ saveFileDialog.FileName = "saveFileDialog"; saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortColor + // + buttonSortColor.Location = new Point(6, 216); + buttonSortColor.Name = "buttonSortColor"; + buttonSortColor.Size = new Size(122, 30); + buttonSortColor.TabIndex = 8; + buttonSortColor.Text = "Сорт. по цвету"; + buttonSortColor.UseVisualStyleBackColor = true; + buttonSortColor.Click += ButtonSortByColor_Click; + // + // buttonSortType + // + buttonSortType.Location = new Point(6, 184); + buttonSortType.Name = "buttonSortType"; + buttonSortType.Size = new Size(120, 26); + buttonSortType.TabIndex = 9; + buttonSortType.Text = "Сорт. по типу"; + buttonSortType.UseVisualStyleBackColor = true; + buttonSortType.Click += ButtonSortByType_Click; + // // FormCruiserCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -241,5 +265,7 @@ private ToolStripMenuItem UploadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortType; + private Button buttonSortColor; } } \ No newline at end of file diff --git a/Cruiser/FormCruiserCollection.cs b/Cruiser/FormCruiserCollection.cs index e4c63b6..7805a11 100644 --- a/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/FormCruiserCollection.cs @@ -48,7 +48,7 @@ namespace Cruiser 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)) { @@ -137,10 +137,15 @@ namespace Cruiser { MessageBox.Show("Не удалось добавить объект"); } - } - catch (ApplicationException ex) - { - MessageBox.Show(ex.Message); + } + catch (ApplicationException ex) + { + MessageBox.Show(ex.Message); + } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } } /// @@ -168,7 +173,7 @@ namespace Cruiser return; } int pos = Convert.ToInt32(textBoxNumber.Text); - try + try { if (obj - pos != null) { @@ -181,13 +186,13 @@ namespace Cruiser _logger.LogWarning($"Удаление круизера не удалось(обьект не найден)"); MessageBox.Show("Не удалось удалить объект"); } - } - catch (CruiserNotFoundException ex) + } + catch (CruiserNotFoundException ex) { _logger.LogWarning($"Удаление круизера не удалось {ex.Message}"); MessageBox.Show(ex.Message); } - + } /// /// Обновление рисунка по набору @@ -258,5 +263,35 @@ namespace Cruiser } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void CompareCruiser(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.ShowCruiser(); + } } } diff --git a/Cruiser/Generics/CruiserCollectionInfo.cs b/Cruiser/Generics/CruiserCollectionInfo.cs new file mode 100644 index 0000000..cc3d069 --- /dev/null +++ b/Cruiser/Generics/CruiserCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser.Generics +{ + internal class CruiserCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CruiserCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CruiserCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/Cruiser/Generics/CruiserCompareByColor.cs b/Cruiser/Generics/CruiserCompareByColor.cs new file mode 100644 index 0000000..98caf6a --- /dev/null +++ b/Cruiser/Generics/CruiserCompareByColor.cs @@ -0,0 +1,44 @@ +using Cruiser.Entities; +using Cruiser.Drawing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser.Generics +{ + internal class CruiserCompareByColor : IComparer + { + public int Compare(DrawingCruiser? x, DrawingCruiser? y) + { + if (x == null || x.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityCruiser.BodyColor.Name.CompareTo(y.EntityCruiser.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityCruiser is EntityProCruiser _cruiserProX && y.EntityCruiser is EntityProCruiser _cruiserProY) + { + var ElementsColorCompare = _cruiserProX.ElementsColor.Name.CompareTo(_cruiserProY.ElementsColor.Name); + if (ElementsColorCompare != 0) + { + return ElementsColorCompare; + } + } + var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight); + } + } +} diff --git a/Cruiser/Generics/CruiserCompareByType.cs b/Cruiser/Generics/CruiserCompareByType.cs new file mode 100644 index 0000000..6ee0a22 --- /dev/null +++ b/Cruiser/Generics/CruiserCompareByType.cs @@ -0,0 +1,35 @@ +using Cruiser.Drawing; +using Cruiser.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser.Generics +{ + internal class CruiserCompareByType : IComparer + { + public int Compare(DrawingCruiser? x, DrawingCruiser? y) + { + if (x == null || x.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight); + } + } +} diff --git a/Cruiser/Generics/CruisersGenericCollection.cs b/Cruiser/Generics/CruisersGenericCollection.cs index 4d059e4..9df145b 100644 --- a/Cruiser/Generics/CruisersGenericCollection.cs +++ b/Cruiser/Generics/CruisersGenericCollection.cs @@ -1,4 +1,5 @@ using Cruiser.MovementStrategy; +using Cruiser.Generics; using System; using System.Collections.Generic; using System.Linq; @@ -22,6 +23,11 @@ namespace Cruiser.Generics /// public IEnumerable GetCruisers => _collection.GetCruisers(); /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Ширина окна прорисовки /// private readonly int _pictureWidth; @@ -66,7 +72,7 @@ namespace Cruiser.Generics { return false; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningCruiserEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/Cruiser/Generics/CruisersGenericStorage.cs b/Cruiser/Generics/CruisersGenericStorage.cs index 1191391..b12f7b3 100644 --- a/Cruiser/Generics/CruisersGenericStorage.cs +++ b/Cruiser/Generics/CruisersGenericStorage.cs @@ -27,11 +27,11 @@ namespace Cruiser.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _cruiserStorages; + readonly Dictionary> _cruiserStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _cruiserStorages.Keys.ToList(); + public List Keys => _cruiserStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -47,7 +47,7 @@ namespace Cruiser.Generics /// public CruisersGenericStorage(int pictureWidth, int pictureHeight) { - _cruiserStorages = new Dictionary>(); + _cruiserStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,8 +57,8 @@ namespace Cruiser.Generics /// Название набора public void AddSet(string name) { - if (_cruiserStorages.ContainsKey(name)) return; - _cruiserStorages[name] = new CarsGenericCollection(_pictureWidth, _pictureHeight); + if (_cruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty))) return; + _cruiserStorages[new CruiserCollectionInfo(name, string.Empty)] = new CarsGenericCollection(_pictureWidth, _pictureHeight); } /// /// Удаление набора @@ -66,8 +66,8 @@ namespace Cruiser.Generics /// Название набора public void DelSet(string name) { - if (!_cruiserStorages.ContainsKey(name)) return; - _cruiserStorages.Remove(name); + if (!_cruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty))) return; + _cruiserStorages.Remove(new CruiserCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -79,7 +79,7 @@ namespace Cruiser.Generics { get { - if (_cruiserStorages.ContainsKey(ind)) return _cruiserStorages[ind]; + if (_cruiserStorages.ContainsKey(new CruiserCollectionInfo(ind, string.Empty))) return _cruiserStorages[new CruiserCollectionInfo(ind, string.Empty)]; return null; } } @@ -95,7 +95,7 @@ namespace Cruiser.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _cruiserStorages) + foreach (KeyValuePair> record in _cruiserStorages) { StringBuilder records = new(); foreach (DrawingCruiser? elem in record.Value.GetCruisers) @@ -168,7 +168,7 @@ namespace Cruiser.Generics } } - _cruiserStorages.Add(name, collection); + _cruiserStorages.Add(new CruiserCollectionInfo(name, string.Empty), collection); } return true; } diff --git a/Cruiser/Generics/DrawiningCruiserEqutables.cs b/Cruiser/Generics/DrawiningCruiserEqutables.cs new file mode 100644 index 0000000..e8cc902 --- /dev/null +++ b/Cruiser/Generics/DrawiningCruiserEqutables.cs @@ -0,0 +1,64 @@ +using Cruiser.Entities; +using Cruiser.Drawing; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser.Generics +{ + internal class DrawiningCruiserEqutables : IEqualityComparer + { + public bool Equals(DrawingCruiser? x, DrawingCruiser? y) + { + if (x == null || x.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCruiser == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityCruiser.Speed != y.EntityCruiser.Speed) + { + return false; + } + if (x.EntityCruiser.Weight != y.EntityCruiser.Weight) + { + return false; + } + if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor) + { + return false; + } + if (x is DrawingProCruiser && y is DrawingProCruiser) + { + EntityProCruiser _cruiserX = (EntityProCruiser)x.EntityCruiser; + EntityProCruiser _cruiserY = (EntityProCruiser)y.EntityCruiser; + if (_cruiserX.Helipad != _cruiserY.Helipad) + { + return false; + } + if (_cruiserX.RocketMines != _cruiserY.RocketMines) + { + return false; + } + if (_cruiserX.ElementsColor != _cruiserY.ElementsColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingCruiser obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Cruiser/Generics/SetGeneric.cs b/Cruiser/Generics/SetGeneric.cs index daa55fc..421b098 100644 --- a/Cruiser/Generics/SetGeneric.cs +++ b/Cruiser/Generics/SetGeneric.cs @@ -41,14 +41,13 @@ namespace Cruiser.Generics /// /// Добавляемый лайнер /// - public bool Insert(T cruiser) //починил код, работал неправильно + public bool Insert(T cruiser, IEqualityComparer? equal = null) //починил код, работал неправильно, переделал на инт { if (_places.Count >= _maxCount) { throw new StorageOverflowException(_places.Count); } - _places.Insert(0, cruiser); - return true; + return Insert(cruiser, 0, equal); } /// /// Удаление объекта из набора с конкретной позиции @@ -70,16 +69,14 @@ namespace Cruiser.Generics /// Добавляемый автомобиль /// Позиция /// - public bool Insert(T cruiser, int position) //починил код, работал неправильно + public bool Insert(T cruiser, int position, IEqualityComparer? equal = null) //починил код, работал неправильно, переделал на инт { - if (_places.Count >= _maxCount) - { - throw new StorageOverflowException(_places.Count); - } - if (position < 0 || position > _places.Count) - { + if (position < 0 || position > Count) throw new CruiserNotFoundException(position); - } + if (Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + if (equal != null && _places.Contains(cruiser, equal)) + throw new ArgumentException("Круизер уже имеется"); _places.Insert(position, cruiser); return true; } @@ -109,6 +106,6 @@ namespace Cruiser.Generics } } } - + public void SortSet(IComparer comparer) => _places.Sort(comparer); } }