diff --git a/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs b/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs new file mode 100644 index 0000000..f39d176 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs @@ -0,0 +1,18 @@ +namespace SelfPropelledArtilleryUnit.Generics +{ + internal class SPAUCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CarsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CarsCollectionInfo? other) + { + // TODO прописать логику сравнения по свойству Name + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.sln b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.sln index 3eda489..a567d7a 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.sln +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SelfPropelledArtilleryUnit", "SelfPropelledArtilleryUnit\SelfPropelledArtilleryUnit.csproj", "{82A37504-B0B3-4518-AF90-0433F4E332E4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelfPropelledArtilleryUnit", "SelfPropelledArtilleryUnit\SelfPropelledArtilleryUnit.csproj", "{82A37504-B0B3-4518-AF90-0433F4E332E4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawiningSPAUEqutables.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawiningSPAUEqutables.cs new file mode 100644 index 0000000..60163c1 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawiningSPAUEqutables.cs @@ -0,0 +1,58 @@ +using SelfPropelledArtilleryUnit.DrawningObjects; +using SelfPropelledArtilleryUnit.Entities; +using System.Diagnostics.CodeAnalysis; +namespace SelfPropelledArtilleryUnit.Generics +{ + internal class DrawiningSPAUEqutables : IEqualityComparer + { + public bool Equals(DrawningSPAU? x, DrawningSPAU? y) + { + if (x == null || x.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntitySPAU.Speed != y.EntitySPAU.Speed) + { + return false; + } + if (x.EntitySPAU.Weight != y.EntitySPAU.Weight) + { + return false; + } + if (x.EntitySPAU.BodyColor != y.EntitySPAU.BodyColor) + { + return false; + } + if (x is DrawningSPAUchild && y is DrawningSPAUchild) + { + EntitySPAUchild xChild = (EntitySPAUchild)x.EntitySPAU; + EntitySPAUchild yChild = (EntitySPAUchild)y.EntitySPAU; + if (xChild.Ballon != yChild.Ballon) + { + return false; + } + if (xChild.AdditionalColor != yChild.AdditionalColor) + { + return false; + } + if (xChild.BodyKit != yChild.BodyKit) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningSPAU obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs index 79a993d..c22886f 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.Designer.cs @@ -35,6 +35,8 @@ ButtonRefreshCollection = new Button(); panel1 = new Panel(); panel2 = new Panel(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); listBoxStorages = new ListBox(); ButtonDelObject = new Button(); ButtonAddObject = new Button(); @@ -61,14 +63,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(18, 71); + maskedTextBoxNumber.Location = new Point(18, 42); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(143, 27); maskedTextBoxNumber.TabIndex = 1; // // ButtonAddSPAU // - ButtonAddSPAU.Location = new Point(44, 19); + ButtonAddSPAU.Location = new Point(44, 3); ButtonAddSPAU.Name = "ButtonAddSPAU"; ButtonAddSPAU.Size = new Size(102, 33); ButtonAddSPAU.TabIndex = 2; @@ -78,7 +80,7 @@ // // ButtonRemoveSPAU // - ButtonRemoveSPAU.Location = new Point(44, 118); + ButtonRemoveSPAU.Location = new Point(44, 75); ButtonRemoveSPAU.Name = "ButtonRemoveSPAU"; ButtonRemoveSPAU.Size = new Size(100, 37); ButtonRemoveSPAU.TabIndex = 3; @@ -88,7 +90,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(44, 172); + ButtonRefreshCollection.Location = new Point(44, 118); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(100, 35); ButtonRefreshCollection.TabIndex = 4; @@ -102,22 +104,44 @@ panel1.Controls.Add(ButtonRemoveSPAU); panel1.Controls.Add(ButtonAddSPAU); panel1.Controls.Add(maskedTextBoxNumber); - panel1.Location = new Point(711, 245); + panel1.Location = new Point(711, 305); panel1.Name = "panel1"; - panel1.Size = new Size(176, 227); + panel1.Size = new Size(176, 167); panel1.TabIndex = 5; // // panel2 // + panel2.Controls.Add(ButtonSortByColor); + panel2.Controls.Add(ButtonSortByType); panel2.Controls.Add(listBoxStorages); panel2.Controls.Add(ButtonDelObject); panel2.Controls.Add(ButtonAddObject); panel2.Controls.Add(textBoxStorageName); panel2.Location = new Point(711, 40); panel2.Name = "panel2"; - panel2.Size = new Size(174, 199); + panel2.Size = new Size(174, 259); panel2.TabIndex = 6; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(3, 227); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(168, 29); + ButtonSortByColor.TabIndex = 9; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(3, 182); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(168, 39); + ButtonSortByType.TabIndex = 8; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // listBoxStorages // listBoxStorages.FormattingEnabled = true; @@ -130,7 +154,7 @@ // // ButtonDelObject // - ButtonDelObject.Location = new Point(18, 168); + ButtonDelObject.Location = new Point(18, 153); ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Size = new Size(143, 28); ButtonDelObject.TabIndex = 3; @@ -175,14 +199,14 @@ // openFileDialog // openFileDialog.Name = "openFileDialog"; - openFileDialog.Size = new Size(224, 26); + openFileDialog.Size = new Size(205, 26); openFileDialog.Text = "Открыть файл"; openFileDialog.Click += LoadToolStripMenuItem_Click; // // saveFileDialog // saveFileDialog.Name = "saveFileDialog"; - saveFileDialog.Size = new Size(224, 26); + saveFileDialog.Size = new Size(205, 26); saveFileDialog.Text = "Сохранить файл"; saveFileDialog.Click += SaveToolStripMenuItem_Click; // @@ -238,5 +262,7 @@ private ToolStripMenuItem saveFileDialog; private OpenFileDialog openFileDialog_; private OpenFileDialog saveFileDialog_; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs index 3253b6f..4eb41ad 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSPAUCollection.cs @@ -50,7 +50,7 @@ namespace SelfPropelledArtilleryUnit 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)) @@ -169,7 +169,7 @@ namespace SelfPropelledArtilleryUnit MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"Неудачная попытка добавления: {ex}"); } - + } @@ -200,7 +200,7 @@ namespace SelfPropelledArtilleryUnit pos = Convert.ToInt32(maskedTextBoxNumber.Text); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Не удалось удалить объект"); _logger.LogWarning($"Неудачная попытка удаления: {ex}"); @@ -221,7 +221,7 @@ namespace SelfPropelledArtilleryUnit _logger.LogWarning($"Неудачная попытка удаления c позиции {pos}"); } } - catch(SPAUNotFoundException ex) + catch (SPAUNotFoundException ex) { MessageBox.Show(ex.Message); _logger.LogWarning($"Неудачная попытка удаления: {ex}"); @@ -258,11 +258,11 @@ namespace SelfPropelledArtilleryUnit try { _storage.SaveData(saveFileDialog_.FileName); - + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation("Успешное сохранение"); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Не сохранилось: {ex.Message}"); @@ -291,5 +291,38 @@ namespace SelfPropelledArtilleryUnit } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareSPAU(new SPAUCompareByType()); + + /// + /// Сортировка по цвету + /// + /// + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareSPAU(new SPAUCompareByColor()); + + /// + /// Сортировка по сравнителю + /// + /// + private void CompareSPAU(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.ShowSPAUs(); + } + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs new file mode 100644 index 0000000..e557e73 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCollectionInfo.cs @@ -0,0 +1,23 @@ +namespace SelfPropelledArtilleryUnit.Generics +{ + internal class SPAUCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public SPAUCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(SPAUCollectionInfo? other) + { + return Name == other?.Name; + throw new NotImplementedException(); + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByColor.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByColor.cs new file mode 100644 index 0000000..b850a9b --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByColor.cs @@ -0,0 +1,46 @@ +using SelfPropelledArtilleryUnit.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.Generics +{ + internal class SPAUCompareByColor : IComparer + { + public int Compare(DrawningSPAU? x, DrawningSPAU? y) + { + if (x == null || x.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntitySPAU.BodyColor == y.EntitySPAU.BodyColor) + { + return 0; + } + else + { + if (x.EntitySPAU.BodyColor.R.CompareTo(y.EntitySPAU.BodyColor.R) == 0) + { + if (x.EntitySPAU.BodyColor.G.CompareTo(y.EntitySPAU.BodyColor.G) == 0) + { + return x.EntitySPAU.BodyColor.B.CompareTo(y.EntitySPAU.BodyColor.B); + } + else + { + return x.EntitySPAU.BodyColor.G.CompareTo(y.EntitySPAU.BodyColor.G); + } + } + else { + return x.EntitySPAU.BodyColor.R.CompareTo(y.EntitySPAU.BodyColor.R); + } + } + } + + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByType.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByType.cs new file mode 100644 index 0000000..f2034ae --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUCompareByType.cs @@ -0,0 +1,35 @@ +using SelfPropelledArtilleryUnit.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.Generics +{ + internal class SPAUCompareByType : IComparer + { + + public int Compare(DrawningSPAU? x, DrawningSPAU? y) + { + if (x == null || x.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntitySPAU == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntitySPAU.Speed.CompareTo(y.EntitySPAU.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntitySPAU.Weight.CompareTo(y.EntitySPAU.Weight); + } + } +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs index 749640c..a45d839 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericCollection.cs @@ -150,6 +150,14 @@ namespace SelfPropelledArtilleryUnit.Generics current?.DrawTransport(g); } } + + // + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); + } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs index f93289d..f08c95b 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SPAUGenericStorage.cs @@ -16,11 +16,11 @@ namespace SelfPropelledArtilleryUnit.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _SPAUStorages; + readonly Dictionary> _SPAUStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _SPAUStorages.Keys.ToList(); + public List Keys => _SPAUStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -36,7 +36,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// public SPAUGenericStorage(int pictureWidth, int pictureHeight) { - _SPAUStorages = new Dictionary>(); + _SPAUStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -48,7 +48,7 @@ namespace SelfPropelledArtilleryUnit.Generics { try { - _SPAUStorages.Add(name, new SPAUGenericCollection(_pictureWidth, _pictureHeight)); + _SPAUStorages.Add(new SPAUCollectionInfo(name, string.Empty), new SPAUGenericCollection(_pictureWidth, _pictureHeight)); } catch (Exception) { return; } } @@ -58,7 +58,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// Название набора public void DelSet(string name) { - try { _SPAUStorages.Remove(name); } catch (Exception) { return; } + try { _SPAUStorages.Remove(new SPAUCollectionInfo(name, string.Empty)); } catch (Exception) { return; } } /// /// Доступ к набору @@ -69,9 +69,9 @@ namespace SelfPropelledArtilleryUnit.Generics { get { - if (_SPAUStorages.ContainsKey(ind)) + if (_SPAUStorages.ContainsKey(new SPAUCollectionInfo(ind, string.Empty))) { - return _SPAUStorages[ind]; + return _SPAUStorages[new SPAUCollectionInfo(ind, string.Empty)]; } else { @@ -105,7 +105,7 @@ namespace SelfPropelledArtilleryUnit.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _SPAUStorages) + foreach (KeyValuePair> record in _SPAUStorages) { StringBuilder records = new(); foreach (DrawningSPAU? elem in record.Value.GetCars.Reverse()) @@ -178,7 +178,7 @@ namespace SelfPropelledArtilleryUnit.Generics } } } - _SPAUStorages.Add(record[0], collection); + _SPAUStorages.Add(new SPAUCollectionInfo(record[0], string.Empty), collection); } return true; } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs index 97ecf9b..1afdda9 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs @@ -42,7 +42,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// /// Добавляемый автомобиль /// - public int Insert(T spau) + public int Insert(T spau, IEqualityComparer? equal = null) { return Insert(spau, 0); } @@ -52,7 +52,7 @@ namespace SelfPropelledArtilleryUnit.Generics /// Добавляемый автомобиль /// Позиция /// - public int Insert(T spau, int position) + public int Insert(T spau, int position, IEqualityComparer? equal = null) { if (Count == _maxCount) throw new StorageOverflowException(Count); @@ -124,6 +124,12 @@ namespace SelfPropelledArtilleryUnit.Generics } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => + _places.Sort(comparer); } }