From 56bcc98d9ffb7a609124e915007d6156f7eea108 Mon Sep 17 00:00:00 2001 From: kirin Date: Tue, 12 Dec 2023 13:58:32 +0400 Subject: [PATCH] all done --- .../DrawingLocomotivEqutables.cs | 48 +++++++++++++++++ lab1/FormLocomotivCollection.Designer.cs | 32 +++++++++-- lab1/FormLocomotivCollection.cs | 54 +++++++++++-------- lab1/Generics/LocomotivCollectionInfo.cs | 22 ++++++++ lab1/Generics/LocomotivCompareByColor.cs | 40 ++++++++++++++ lab1/Generics/LocomotivCompareByType.cs | 28 ++++++++++ lab1/Generics/LocosGenericCollection.cs | 8 +-- lab1/Generics/LocosGenericStorage.cs | 21 ++++---- lab1/Generics/SetGeneric.cs | 12 +++-- 9 files changed, 224 insertions(+), 41 deletions(-) create mode 100644 lab1/DrawingObjects/DrawingLocomotivEqutables.cs create mode 100644 lab1/Generics/LocomotivCollectionInfo.cs create mode 100644 lab1/Generics/LocomotivCompareByColor.cs create mode 100644 lab1/Generics/LocomotivCompareByType.cs diff --git a/lab1/DrawingObjects/DrawingLocomotivEqutables.cs b/lab1/DrawingObjects/DrawingLocomotivEqutables.cs new file mode 100644 index 0000000..0f07e87 --- /dev/null +++ b/lab1/DrawingObjects/DrawingLocomotivEqutables.cs @@ -0,0 +1,48 @@ +using System.Diagnostics.CodeAnalysis; + +namespace ElectricLocomotive; + +public class DrawingLocomotivEqutables: IEqualityComparer +{ + public bool Equals(DrawingLocomotiv? x, DrawingLocomotiv? y) + { + if (x == null || x.EntityLocomotiv == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLocomotiv == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityLocomotiv.Speed != y.EntityLocomotiv.Speed) + { + return false; + } + if (x.EntityLocomotiv.Weight != y.EntityLocomotiv.Weight) + { + return false; + } + if (x.EntityLocomotiv.ColorBody != y.EntityLocomotiv.ColorBody) + { + return false; + } + if (x is DrawingElectricLocomotiv && y is DrawingElectricLocomotiv) + { + EntityElectricLocomotiv EntityX = (EntityElectricLocomotiv)x.EntityLocomotiv; + EntityElectricLocomotiv EntityY = (EntityElectricLocomotiv)y.EntityLocomotiv; + if (EntityX.isBattery != EntityY.isBattery) + return false; + if (EntityX.isRoga != EntityY.isRoga) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingLocomotiv obj) + { + return obj.GetHashCode(); + } +} \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.Designer.cs b/lab1/FormLocomotivCollection.Designer.cs index bd6df2a..a70f120 100644 --- a/lab1/FormLocomotivCollection.Designer.cs +++ b/lab1/FormLocomotivCollection.Designer.cs @@ -44,6 +44,8 @@ partial class FormLocomotivCollection { LoadToolStripMenuItem = new ToolStripMenuItem(); loadFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); toolsBox.SuspendLayout(); collectionGroupBoxes.SuspendLayout(); ((ISupportInitialize)collectionPictureBox).BeginInit(); @@ -52,6 +54,8 @@ partial class FormLocomotivCollection { // // toolsBox // + toolsBox.Controls.Add(ButtonSortByType); + toolsBox.Controls.Add(ButtonSortByColor); toolsBox.Controls.Add(collectionGroupBoxes); toolsBox.Controls.Add(refreshCollection); toolsBox.Controls.Add(deleteLoco); @@ -126,7 +130,7 @@ partial class FormLocomotivCollection { // // deleteLoco // - deleteLoco.Location = new Point(21, 559); + deleteLoco.Location = new Point(21, 568); deleteLoco.Name = "deleteLoco"; deleteLoco.Size = new Size(271, 59); deleteLoco.TabIndex = 2; @@ -136,14 +140,14 @@ partial class FormLocomotivCollection { // // locoIndexInput // - locoIndexInput.Location = new Point(53, 512); + locoIndexInput.Location = new Point(54, 526); locoIndexInput.Name = "locoIndexInput"; locoIndexInput.Size = new Size(214, 27); locoIndexInput.TabIndex = 1; // // addLocomotiv // - addLocomotiv.Location = new Point(21, 431); + addLocomotiv.Location = new Point(21, 461); addLocomotiv.Name = "addLocomotiv"; addLocomotiv.Size = new Size(271, 59); addLocomotiv.TabIndex = 0; @@ -199,6 +203,26 @@ partial class FormLocomotivCollection { // saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(21, 369); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(271, 32); + ButtonSortByColor.TabIndex = 7; + ButtonSortByColor.Text = "Сортировать по Цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(21, 407); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(271, 32); + ButtonSortByType.TabIndex = 8; + ButtonSortByType.Text = "Сортировать по Типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // FormLocomotivCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -240,4 +264,6 @@ partial class FormLocomotivCollection { private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog loadFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.cs b/lab1/FormLocomotivCollection.cs index 8c813ce..fcc6114 100644 --- a/lab1/FormLocomotivCollection.cs +++ b/lab1/FormLocomotivCollection.cs @@ -14,7 +14,7 @@ public partial class FormLocomotivCollection : Form { int index = storageListBox.SelectedIndex; storageListBox.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - storageListBox.Items.Add(_storage.Keys[i]); + storageListBox.Items.Add(_storage.Keys[i].Name); } if (storageListBox.Items.Count > 0 && (index == -1 || index >= storageListBox.Items.Count)) { @@ -37,19 +37,21 @@ public partial class FormLocomotivCollection : Form { FormLocoConfig form = new(); form.Show(); Action? monorailDelegate = new((m) => { - try - { + try { bool q = obj + m; MessageBox.Show("Объект добавлен"); Log.Information($"Добавлен объект в коллекцию {storageListBox.SelectedItem.ToString() ?? string.Empty}"); m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height); collectionPictureBox.Image = obj.ShowLocos(); } - catch (StorageOverflowException ex) - { + catch (StorageOverflowException ex) { Log.Warning($"Коллекция {storageListBox.SelectedItem.ToString() ?? string.Empty} переполнена"); MessageBox.Show(ex.Message); } + catch (ArgumentException ex) { + Log.Warning($"Добавляемый объект уже существует в коллекции {storageListBox.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + } }); form.AddEvent(monorailDelegate); } @@ -66,22 +68,19 @@ public partial class FormLocomotivCollection : Form { MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } - - try - { + + try { int pos = Convert.ToInt32(locoIndexInput.Text); var q = obj - pos; MessageBox.Show("Объект удален"); Log.Information($"Удален объект из коллекции {storageListBox.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); collectionPictureBox.Image = obj.ShowLocos(); } - catch(LocoNotFoundException ex) - { + catch (LocoNotFoundException ex) { Log.Warning($"Не получилось удалить объект из коллекции {storageListBox.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show(ex.Message); } - catch(FormatException ex) - { + catch (FormatException ex) { Log.Warning($"Было введено не число"); MessageBox.Show("Введите число"); } @@ -130,15 +129,13 @@ public partial class FormLocomotivCollection : Form { private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - try - { + try { _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен"); } - catch (Exception ex) - { + catch (Exception ex) { Log.Warning("Не удалось сохранить"); MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -148,24 +145,37 @@ public partial class FormLocomotivCollection : Form { private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { if (loadFileDialog.ShowDialog() == DialogResult.OK) { - try - { + try { _storage.LoadData(loadFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); Log.Information($"Файл {loadFileDialog.FileName} успешно загружен"); - foreach (var collection in _storage.Keys) - { + foreach (var collection in _storage.Keys) { storageListBox.Items.Add(collection); } ReloadObjects(); } - catch (Exception ex) - { + catch (Exception ex) { Log.Warning("Не удалось загрузить"); MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } + private void CompareLocos(IComparer comparer) { + if (storageListBox.SelectedIndex == -1) { + return; + } + var obj = _storage[storageListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) { + return; + } + obj.Sort(comparer); + collectionPictureBox.Image = obj.ShowLocos(); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocos(new LocomotivCompareByType()); + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocos(new LocomotivCompareByColor()); } \ No newline at end of file diff --git a/lab1/Generics/LocomotivCollectionInfo.cs b/lab1/Generics/LocomotivCollectionInfo.cs new file mode 100644 index 0000000..a08eb08 --- /dev/null +++ b/lab1/Generics/LocomotivCollectionInfo.cs @@ -0,0 +1,22 @@ +namespace ElectricLocomotive; + +public class LocomotivCollectionInfo : IEquatable +{ + public string Name { get; private set; } + public string Description { get; private set; } + public LocomotivCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(LocomotivCollectionInfo? other) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + +} \ No newline at end of file diff --git a/lab1/Generics/LocomotivCompareByColor.cs b/lab1/Generics/LocomotivCompareByColor.cs new file mode 100644 index 0000000..c1d5cc9 --- /dev/null +++ b/lab1/Generics/LocomotivCompareByColor.cs @@ -0,0 +1,40 @@ +namespace ElectricLocomotive; + +public class LocomotivCompareByColor : IComparer +{ + public int Compare(DrawingLocomotiv? x, DrawingLocomotiv? y) + { + if (x == null || x.EntityLocomotiv == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityLocomotiv == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityLocomotiv.ColorBody.Name != y.EntityLocomotiv.ColorBody.Name) + { + return x.EntityLocomotiv.ColorBody.GetBrightness().CompareTo(y.EntityLocomotiv.ColorBody.GetBrightness()); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawingLocomotiv) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawingLocomotiv) + { + EntityElectricLocomotiv EntityX = (EntityElectricLocomotiv)x.EntityLocomotiv; + EntityElectricLocomotiv EntityY = (EntityElectricLocomotiv)y.EntityLocomotiv; + if(EntityX.RogaColor.Name != EntityY.RogaColor.Name) + { + return EntityX.RogaColor.Name.CompareTo(EntityY.RogaColor.Name); + } + } + var speedCompare = x.EntityLocomotiv.Speed.CompareTo(y.EntityLocomotiv.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityLocomotiv.Weight.CompareTo(y.EntityLocomotiv.Weight); + } +} \ No newline at end of file diff --git a/lab1/Generics/LocomotivCompareByType.cs b/lab1/Generics/LocomotivCompareByType.cs new file mode 100644 index 0000000..e68e637 --- /dev/null +++ b/lab1/Generics/LocomotivCompareByType.cs @@ -0,0 +1,28 @@ +namespace ElectricLocomotive; + +public class LocomotivCompareByType: IComparer +{ + public int Compare(DrawingLocomotiv? x, DrawingLocomotiv? y) + { + if (x == null || x.EntityLocomotiv == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLocomotiv == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityLocomotiv.Speed.CompareTo(y.EntityLocomotiv.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLocomotiv.Weight.CompareTo(y.EntityLocomotiv.Weight); + } + +} \ No newline at end of file diff --git a/lab1/Generics/LocosGenericCollection.cs b/lab1/Generics/LocosGenericCollection.cs index 11a8aea..518371d 100644 --- a/lab1/Generics/LocosGenericCollection.cs +++ b/lab1/Generics/LocosGenericCollection.cs @@ -8,6 +8,8 @@ public class LocosGenericCollection where T : DrawingLocomotiv where U : private readonly int _placeSizeHeight = 110; private readonly SetGeneric _collection; public IEnumerable GetLocos => _collection.GetElectricLocos(); + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); public LocosGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -19,9 +21,9 @@ public class LocosGenericCollection where T : DrawingLocomotiv where U : public static bool operator +(LocosGenericCollection collect, T? obj) { - if (obj == null) return false; - - return collect?._collection.Insert(obj) ?? false; + if (obj == null) + return false; + return collect?._collection.Insert(obj, new DrawingLocomotivEqutables()) ?? false; } public static T? operator -(LocosGenericCollection collect, int diff --git a/lab1/Generics/LocosGenericStorage.cs b/lab1/Generics/LocosGenericStorage.cs index 5026eea..21704e6 100644 --- a/lab1/Generics/LocosGenericStorage.cs +++ b/lab1/Generics/LocosGenericStorage.cs @@ -4,8 +4,8 @@ namespace ElectricLocomotive; public class LocosGenericStorage { - readonly Dictionary> _electricLocoStorages; - public List Keys => _electricLocoStorages.Keys.ToList(); + readonly Dictionary> _electricLocoStorages; + public List Keys => _electricLocoStorages.Keys.ToList(); private readonly int _pictureWidth; @@ -18,7 +18,7 @@ public class LocosGenericStorage public LocosGenericStorage(int pictureWidth, int pictureHeight) { - _electricLocoStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -26,22 +26,23 @@ public class LocosGenericStorage public void AddSet(string name) { - _electricLocoStorages.Add(name, new LocosGenericCollection (_pictureWidth, _pictureHeight)); + _electricLocoStorages.Add(new LocomotivCollectionInfo(name, string.Empty), new LocosGenericCollection (_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_electricLocoStorages.ContainsKey(name)) + if (!_electricLocoStorages.ContainsKey(new LocomotivCollectionInfo(name, string.Empty))) return; - _electricLocoStorages.Remove(name); + _electricLocoStorages.Remove(new LocomotivCollectionInfo(name, string.Empty)); } public LocosGenericCollection? this[string ind] { get { - if (_electricLocoStorages.ContainsKey(ind)) - return _electricLocoStorages[ind]; + LocomotivCollectionInfo indObj = new LocomotivCollectionInfo(ind, string.Empty); + if (_electricLocoStorages.ContainsKey(indObj)) + return _electricLocoStorages[indObj]; return null; } } @@ -52,7 +53,7 @@ public class LocosGenericStorage File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _electricLocoStorages) { StringBuilder records = new(); @@ -125,7 +126,7 @@ public class LocosGenericStorage } } } - _electricLocoStorages.Add(record[0], collection); + _electricLocoStorages.Add(new LocomotivCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); diff --git a/lab1/Generics/SetGeneric.cs b/lab1/Generics/SetGeneric.cs index 0ad1009..1fb7875 100644 --- a/lab1/Generics/SetGeneric.cs +++ b/lab1/Generics/SetGeneric.cs @@ -14,21 +14,27 @@ public class SetGeneric where T : class _maxCount = count; _places = new List(count); } - public bool Insert(T electricLocomotiv) + public void SortSet(IComparer comparer) => _places.Sort(comparer); + public bool Insert(T electricLocomotiv, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(electricLocomotiv, 0); + Insert(electricLocomotiv, 0, equal); return true; } - public bool Insert(T electricLocomotiv, int position) + public bool Insert(T electricLocomotiv, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) return false; + if(equal != null) + { + if (_places.Contains(electricLocomotiv, equal)) + throw new ArgumentException(nameof(electricLocomotiv)); + } _places.Insert(position, electricLocomotiv); return true; }