From c1eb01d4913dacea37da43b4bd99aa18bb73ea88 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Tue, 19 Dec 2023 12:51:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/DrawiningTankEqutables.cs | 68 ++++++++++++++ Tank/Tank/FormTankConfig.cs | 4 +- Tank/Tank/FormTanksCollections.Designer.cs | 101 ++++++++++++++------- Tank/Tank/FormTanksCollections.cs | 25 ++++- Tank/Tank/SetGeneric.cs | 13 ++- Tank/Tank/TankCompareByColor.cs | 34 +++++++ Tank/Tank/TankCompareByType.cs | 34 +++++++ Tank/Tank/TanksCollectionInfo.cs | 34 +++++++ Tank/Tank/TanksGenericCollection.cs | 5 +- Tank/Tank/TanksGenericStorage.cs | 26 +++--- 10 files changed, 293 insertions(+), 51 deletions(-) create mode 100644 Tank/Tank/DrawiningTankEqutables.cs create mode 100644 Tank/Tank/TankCompareByColor.cs create mode 100644 Tank/Tank/TankCompareByType.cs create mode 100644 Tank/Tank/TanksCollectionInfo.cs diff --git a/Tank/Tank/DrawiningTankEqutables.cs b/Tank/Tank/DrawiningTankEqutables.cs new file mode 100644 index 0000000..e8ec238 --- /dev/null +++ b/Tank/Tank/DrawiningTankEqutables.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawningObjects; +using Tank.Entities; + +namespace Tank +{ + internal class DrawiningTankEqutables : IEqualityComparer + { + public bool Equals(DrawArmoVehicle? x, DrawArmoVehicle? y) + { + if (x == null || x.Tank == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.Tank == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType() != y.GetType()) + { + return false; + } + if (x.Tank.Speed != y.Tank.Speed) + { + return false; + } + if (x.Tank.Weight != y.Tank.Weight) + { + return false; + } + if (x.Tank.BodyColor != y.Tank.BodyColor) + { + return false; + } + if (x is DrawTank && y is DrawTank) + { + if ((x.Tank as EntityTank).AdditionalColor != (y.Tank as EntityTank).AdditionalColor) + { + return false; + } + if ((x.Tank as EntityTank).BodyKit != (y.Tank as EntityTank).BodyKit) + { + return false; + } + if ((x.Tank as EntityTank).Caterpillar != (y.Tank as EntityTank).Caterpillar) + { + return false; + } + if ((x.Tank as EntityTank).Tower != (y.Tank as EntityTank).Tower) + { + return false; + } + + } + return true; + } + + public int GetHashCode([DisallowNull] DrawArmoVehicle obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Tank/Tank/FormTankConfig.cs b/Tank/Tank/FormTankConfig.cs index b3d82e3..075db9a 100644 --- a/Tank/Tank/FormTankConfig.cs +++ b/Tank/Tank/FormTankConfig.cs @@ -27,14 +27,14 @@ namespace Tank { InitializeComponent(); - panelSilver.MouseDown += panelColor_MouseDown; + panelCyan.MouseDown += panelColor_MouseDown; panelRed.MouseDown += panelColor_MouseDown; panelOrange.MouseDown += panelColor_MouseDown; panelYellow.MouseDown += panelColor_MouseDown; panelGreen.MouseDown += panelColor_MouseDown; - panelCyan.MouseDown += panelColor_MouseDown; panelBlue.MouseDown += panelColor_MouseDown; panelPink.MouseDown += panelColor_MouseDown; + panelSilver.MouseDown += panelColor_MouseDown; defaultColor = labelColor.BackColor; buttonCancel.Click += (s, e) => Close(); diff --git a/Tank/Tank/FormTanksCollections.Designer.cs b/Tank/Tank/FormTanksCollections.Designer.cs index d3f58a3..a981e94 100644 --- a/Tank/Tank/FormTanksCollections.Designer.cs +++ b/Tank/Tank/FormTanksCollections.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { panel1 = new Panel(); + SortColorButton = new Button(); + SortTypeButton = new Button(); menuStrip1 = new MenuStrip(); StripMenu = new ToolStripMenuItem(); SaveItem = new ToolStripMenuItem(); @@ -55,6 +57,8 @@ // // panel1 // + panel1.Controls.Add(SortColorButton); + panel1.Controls.Add(SortTypeButton); panel1.Controls.Add(menuStrip1); panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); @@ -63,18 +67,40 @@ panel1.Controls.Add(InputNum); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(649, 0); + panel1.Location = new Point(560, 0); + panel1.Margin = new Padding(3, 2, 3, 2); panel1.Name = "panel1"; - panel1.Size = new Size(237, 471); + panel1.Size = new Size(207, 402); panel1.TabIndex = 0; // + // SortColorButton + // + SortColorButton.Location = new Point(27, 56); + SortColorButton.Name = "SortColorButton"; + SortColorButton.Size = new Size(163, 23); + SortColorButton.TabIndex = 10; + SortColorButton.Text = "Сортировка по цвету"; + SortColorButton.UseVisualStyleBackColor = true; + SortColorButton.Click += ButtonSortByColor_Click; + // + // SortTypeButton + // + SortTypeButton.Location = new Point(27, 30); + SortTypeButton.Name = "SortTypeButton"; + SortTypeButton.Size = new Size(163, 23); + SortTypeButton.TabIndex = 9; + SortTypeButton.Text = "Сортировка по типу"; + SortTypeButton.UseVisualStyleBackColor = true; + SortTypeButton.Click += ButtonSortByType_Click; + // // menuStrip1 // menuStrip1.ImageScalingSize = new Size(20, 20); menuStrip1.Items.AddRange(new ToolStripItem[] { StripMenu }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(237, 28); + menuStrip1.Padding = new Padding(5, 2, 0, 2); + menuStrip1.Size = new Size(207, 24); menuStrip1.TabIndex = 7; menuStrip1.Text = "menuStrip1"; // @@ -82,20 +108,20 @@ // StripMenu.DropDownItems.AddRange(new ToolStripItem[] { SaveItem, LoadItem }); StripMenu.Name = "StripMenu"; - StripMenu.Size = new Size(59, 24); + StripMenu.Size = new Size(48, 20); StripMenu.Text = "Файл"; // // SaveItem // SaveItem.Name = "SaveItem"; - SaveItem.Size = new Size(166, 26); + SaveItem.Size = new Size(133, 22); SaveItem.Text = "Сохранить"; SaveItem.Click += SaveToolStripMenuItem_Click; // // LoadItem // LoadItem.Name = "LoadItem"; - LoadItem.Size = new Size(166, 26); + LoadItem.Size = new Size(133, 22); LoadItem.Text = "Загрузить"; LoadItem.Click += LoadToolStripMenuItem_Click; // @@ -106,16 +132,18 @@ panel2.Controls.Add(AddCollectButton); panel2.Controls.Add(SetTextBox); panel2.Controls.Add(label2); - panel2.Location = new Point(16, 62); + panel2.Location = new Point(14, 105); + panel2.Margin = new Padding(3, 2, 3, 2); panel2.Name = "panel2"; - panel2.Size = new Size(214, 220); + panel2.Size = new Size(187, 165); panel2.TabIndex = 6; // // DeleteCollectButton // - DeleteCollectButton.Location = new Point(3, 181); + DeleteCollectButton.Location = new Point(3, 136); + DeleteCollectButton.Margin = new Padding(3, 2, 3, 2); DeleteCollectButton.Name = "DeleteCollectButton"; - DeleteCollectButton.Size = new Size(208, 29); + DeleteCollectButton.Size = new Size(182, 22); DeleteCollectButton.TabIndex = 4; DeleteCollectButton.Text = "Удалить набор"; DeleteCollectButton.UseVisualStyleBackColor = true; @@ -124,18 +152,20 @@ // CollectionListBox // CollectionListBox.FormattingEnabled = true; - CollectionListBox.ItemHeight = 20; - CollectionListBox.Location = new Point(3, 91); + CollectionListBox.ItemHeight = 15; + CollectionListBox.Location = new Point(3, 68); + CollectionListBox.Margin = new Padding(3, 2, 3, 2); CollectionListBox.Name = "CollectionListBox"; - CollectionListBox.Size = new Size(207, 84); + CollectionListBox.Size = new Size(182, 64); CollectionListBox.TabIndex = 3; CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // // AddCollectButton // - AddCollectButton.Location = new Point(3, 56); + AddCollectButton.Location = new Point(3, 42); + AddCollectButton.Margin = new Padding(3, 2, 3, 2); AddCollectButton.Name = "AddCollectButton"; - AddCollectButton.Size = new Size(208, 29); + AddCollectButton.Size = new Size(182, 22); AddCollectButton.TabIndex = 2; AddCollectButton.Text = "Добавить набор"; AddCollectButton.UseVisualStyleBackColor = true; @@ -143,9 +173,10 @@ // // SetTextBox // - SetTextBox.Location = new Point(2, 23); + SetTextBox.Location = new Point(2, 17); + SetTextBox.Margin = new Padding(3, 2, 3, 2); SetTextBox.Name = "SetTextBox"; - SetTextBox.Size = new Size(209, 27); + SetTextBox.Size = new Size(183, 23); SetTextBox.TabIndex = 1; // // label2 @@ -153,15 +184,16 @@ label2.AutoSize = true; label2.Location = new Point(3, 0); label2.Name = "label2"; - label2.Size = new Size(66, 20); + label2.Size = new Size(52, 15); label2.TabIndex = 0; label2.Text = "Наборы"; // // UpdateButton // - UpdateButton.Location = new Point(10, 414); + UpdateButton.Location = new Point(9, 369); + UpdateButton.Margin = new Padding(3, 2, 3, 2); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(219, 37); + UpdateButton.Size = new Size(192, 28); UpdateButton.TabIndex = 4; UpdateButton.Text = "Обновить коллекцию"; UpdateButton.UseVisualStyleBackColor = true; @@ -169,9 +201,10 @@ // // DeleteButton // - DeleteButton.Location = new Point(10, 374); + DeleteButton.Location = new Point(9, 339); + DeleteButton.Margin = new Padding(3, 2, 3, 2); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(219, 37); + DeleteButton.Size = new Size(192, 28); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить технику"; DeleteButton.UseVisualStyleBackColor = true; @@ -179,9 +212,10 @@ // // AddButton // - AddButton.Location = new Point(10, 296); + AddButton.Location = new Point(9, 281); + AddButton.Margin = new Padding(3, 2, 3, 2); AddButton.Name = "AddButton"; - AddButton.Size = new Size(219, 37); + AddButton.Size = new Size(192, 28); AddButton.TabIndex = 2; AddButton.Text = "Добавить технику"; AddButton.UseVisualStyleBackColor = true; @@ -189,17 +223,18 @@ // // InputNum // - InputNum.Location = new Point(10, 337); + InputNum.Location = new Point(9, 312); + InputNum.Margin = new Padding(3, 2, 3, 2); InputNum.Name = "InputNum"; - InputNum.Size = new Size(219, 27); + InputNum.Size = new Size(192, 23); InputNum.TabIndex = 1; // // label1 // label1.AutoSize = true; - label1.Location = new Point(122, 39); + label1.Location = new Point(107, 88); label1.Name = "label1"; - label1.Size = new Size(103, 20); + label1.Size = new Size(83, 15); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -207,8 +242,9 @@ // DrawTank.Dock = DockStyle.Fill; DrawTank.Location = new Point(0, 0); + DrawTank.Margin = new Padding(3, 2, 3, 2); DrawTank.Name = "DrawTank"; - DrawTank.Size = new Size(649, 471); + DrawTank.Size = new Size(560, 402); DrawTank.TabIndex = 1; DrawTank.TabStop = false; // @@ -218,11 +254,12 @@ // // FormTanksCollections // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(886, 471); + ClientSize = new Size(767, 402); Controls.Add(DrawTank); Controls.Add(panel1); + Margin = new Padding(3, 2, 3, 2); Name = "FormTanksCollections"; Text = "CollectionsFrame"; panel1.ResumeLayout(false); @@ -256,5 +293,7 @@ private ToolStripMenuItem LoadItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button SortColorButton; + private Button SortTypeButton; } } \ No newline at end of file diff --git a/Tank/Tank/FormTanksCollections.cs b/Tank/Tank/FormTanksCollections.cs index 8b7ce86..c06ec5c 100644 --- a/Tank/Tank/FormTanksCollections.cs +++ b/Tank/Tank/FormTanksCollections.cs @@ -36,7 +36,7 @@ namespace Tank CollectionListBox.Items.Clear(); foreach (var key in _storage.Keys) { - CollectionListBox.Items.Add(key); + CollectionListBox.Items.Add(key.Name); } if (CollectionListBox.Items.Count > 0 && (index == -1 || index >= CollectionListBox.Items.Count)) @@ -66,8 +66,7 @@ namespace Tank private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { - DrawTank.Image = - _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks(); + DrawTank.Image = _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks(); } private void ButtonDelObject_Click(object sender, EventArgs e) @@ -116,6 +115,11 @@ namespace Tank MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"{ex.Message} в наборе {CollectionListBox.SelectedItem.ToString()}"); } + catch (ApplicationException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"добавление танка неуспешно {ex.Message}"); + } } private void ButtonAddTank_Click(object sender, EventArgs e) @@ -226,5 +230,20 @@ namespace Tank } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTank(new TankCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTank(new TankCompareByColor()); + + private void CompareTank(IComparer comparer) + { + if (CollectionListBox.SelectedIndex == -1) + return; + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + return; + obj.Sort(comparer); + DrawTank.Image = obj.ShowTanks(); + } } } \ No newline at end of file diff --git a/Tank/Tank/SetGeneric.cs b/Tank/Tank/SetGeneric.cs index f4dc946..1ea1413 100644 --- a/Tank/Tank/SetGeneric.cs +++ b/Tank/Tank/SetGeneric.cs @@ -17,6 +17,9 @@ namespace Tank // Максимальное количество объектов private readonly int _maxCount; + // Сортировка набора объектов + public void SortSet(IComparer comparer) => _places.Sort(comparer); + // Конструктор public SetGeneric(int count) { @@ -25,19 +28,23 @@ namespace Tank } // Добавление объекта в набор - public bool Insert(T tank) + public bool Insert(T tank, IEqualityComparer? equal = null) { - return Insert(tank, 0); + return Insert(tank, 0, equal); } // Добавление на конкретную позицию - public bool Insert(T tank, int position) + public bool Insert(T tank, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new TankNotFoundException(position); // По позиции if (Count >= _maxCount) throw new TankStorageOverflowException(_maxCount); // Макс количество в коллекции + + if (equal != null && _places.Contains(tank, equal)) + throw new ApplicationException("уже существует"); + _places.Insert(0, tank); return true; } diff --git a/Tank/Tank/TankCompareByColor.cs b/Tank/Tank/TankCompareByColor.cs new file mode 100644 index 0000000..9515d36 --- /dev/null +++ b/Tank/Tank/TankCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawningObjects; + +namespace Tank +{ + internal class TankCompareByColor : IComparer + { + public int Compare(DrawArmoVehicle? x, DrawArmoVehicle? y) + { + if (x == null || x.Tank == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.Tank == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.Tank.BodyColor != y.Tank.BodyColor) + { + return x.Tank.BodyColor.Name.CompareTo(y.Tank.BodyColor.Name); + } + var speedCompare = x.Tank.Speed.CompareTo(y.Tank.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.Tank.Weight.CompareTo(y.Tank.Weight); + } + } +} \ No newline at end of file diff --git a/Tank/Tank/TankCompareByType.cs b/Tank/Tank/TankCompareByType.cs new file mode 100644 index 0000000..8ce9181 --- /dev/null +++ b/Tank/Tank/TankCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawningObjects; + +namespace Tank +{ + internal class TankCompareByType : IComparer + { + public int Compare(DrawArmoVehicle? x, DrawArmoVehicle? y) + { + if (x == null || x.Tank == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.Tank == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.Tank.Speed.CompareTo(y.Tank.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.Tank.Weight.CompareTo(y.Tank.Weight); + } + } +} \ No newline at end of file diff --git a/Tank/Tank/TanksCollectionInfo.cs b/Tank/Tank/TanksCollectionInfo.cs new file mode 100644 index 0000000..4b6b3dc --- /dev/null +++ b/Tank/Tank/TanksCollectionInfo.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank +{ + internal class TankCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + + public TankCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + + public bool Equals(TankCollectionInfo? other) + { + if (other == null || Name == null || other.Name == null) return false; + if (Name == other.Name) return true; + + throw new NotImplementedException(); + //return Name.Equals(other.Name); + } + + public override int GetHashCode() + { + return Name?.GetHashCode() ?? 0; + } + } +} \ No newline at end of file diff --git a/Tank/Tank/TanksGenericCollection.cs b/Tank/Tank/TanksGenericCollection.cs index 46db3fb..aa22727 100644 --- a/Tank/Tank/TanksGenericCollection.cs +++ b/Tank/Tank/TanksGenericCollection.cs @@ -43,7 +43,7 @@ namespace Tank.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawiningTankEqutables()); } // Перегрузка оператора вычитания @@ -54,6 +54,9 @@ namespace Tank.Generics return obj; } + // Сортировка + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + // Получение объекта IMoveableObject public U? GetU(int pos) { diff --git a/Tank/Tank/TanksGenericStorage.cs b/Tank/Tank/TanksGenericStorage.cs index dc8bb28..64e8be3 100644 --- a/Tank/Tank/TanksGenericStorage.cs +++ b/Tank/Tank/TanksGenericStorage.cs @@ -7,16 +7,17 @@ using Tank.Generics; using Tank.DrawningObjects; using Tank.MovementStrategy; using Tank.Exceptions; +using Tank.Entities; namespace Tank { internal class TanksGenericStorage { // Словарь - readonly Dictionary> _tankStorages; + readonly Dictionary> _tankStorages; // Возвращение списка названий наборов - public List Keys => _tankStorages.Keys.ToList(); + public List Keys => _tankStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; @@ -27,7 +28,7 @@ namespace Tank // Конструктор public TanksGenericStorage(int pictureWidth, int pictureHeight) { - _tankStorages = new Dictionary>(); + _tankStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -35,15 +36,17 @@ namespace Tank // Добавление набора public void AddSet(string name) { - if (_tankStorages.ContainsKey(name)) return; - _tankStorages[name] = new TanksGenericCollection(_pictureWidth, _pictureHeight); + TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty); + if (_tankStorages.ContainsKey(Info)) return; + _tankStorages[Info] = new TanksGenericCollection(_pictureWidth, _pictureHeight); } // Удаление набора public void DelSet(string name) { - if (!_tankStorages.ContainsKey(name)) return; - _tankStorages.Remove(name); + TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty); + if (!_tankStorages.ContainsKey(Info)) return; + _tankStorages.Remove(Info); } // Доступ к набору @@ -52,7 +55,8 @@ namespace Tank { get { - if (_tankStorages.ContainsKey(ind)) return _tankStorages[ind]; + TankCollectionInfo Info = new TankCollectionInfo(ind, string.Empty); + if (_tankStorages.ContainsKey(Info)) return _tankStorages[Info]; return null; } } @@ -65,14 +69,14 @@ namespace Tank } StringBuilder data = new(); - foreach (KeyValuePair> record in _tankStorages) + foreach (KeyValuePair> record in _tankStorages) { StringBuilder records = new(); foreach (DrawArmoVehicle? elem in record.Value.GetTanks) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -135,7 +139,7 @@ namespace Tank } } } - _tankStorages.Add(name, collection); + _tankStorages.Add(new TankCollectionInfo(name, string.Empty), collection); } } }