From 4eeaa3ae6fce3bf102f0eb7127f4e3488508face Mon Sep 17 00:00:00 2001 From: "[USERNAME]" Date: Sun, 17 Dec 2023 00:11:59 +0400 Subject: [PATCH] Lab8 --- .../Bulldozer/BulldozerCompareByColor.cs | 49 ++++++++++++++++ Bulldozer/Bulldozer/BulldozerCompareByType.cs | 32 ++++++++++ .../Bulldozer/BulldozerGenericCollection.cs | 13 +++-- .../Bulldozer/BulldozersCollectionInfo.cs | 27 +++++++++ .../Bulldozer/BulldozersGenericStorage.cs | 23 ++++---- .../Bulldozer/DrawiningBulldozerEqutables.cs | 58 +++++++++++++++++++ .../FormBulldozerCollection.Designer.cs | 42 +++++++++++--- .../Bulldozer/FormBulldozerCollection.cs | 17 ++++++ Bulldozer/Bulldozer/SetGeneric.cs | 47 +++++---------- 9 files changed, 249 insertions(+), 59 deletions(-) create mode 100644 Bulldozer/Bulldozer/BulldozerCompareByColor.cs create mode 100644 Bulldozer/Bulldozer/BulldozerCompareByType.cs create mode 100644 Bulldozer/Bulldozer/BulldozersCollectionInfo.cs create mode 100644 Bulldozer/Bulldozer/DrawiningBulldozerEqutables.cs diff --git a/Bulldozer/Bulldozer/BulldozerCompareByColor.cs b/Bulldozer/Bulldozer/BulldozerCompareByColor.cs new file mode 100644 index 0000000..d5e5979 --- /dev/null +++ b/Bulldozer/Bulldozer/BulldozerCompareByColor.cs @@ -0,0 +1,49 @@ +using Bulldozer.DrawningObjects; +using Bulldozer.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCompareByColor : IComparer + { + public int Compare(DrawningBulldozer? x, DrawningBulldozer? y) + { + if (x == null || x.EntityTractor == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityTractor == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityTractor.MainColor.Name != y.EntityTractor.MainColor.Name) + { + return x.EntityTractor.MainColor.Name.CompareTo(y.EntityTractor.MainColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is EntityBulldozer) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningFastBulldozer) + { + EntityFastBulldozer EntityX = (EntityFastBulldozer)x.EntityTractor; + EntityFastBulldozer EntityY = (EntityFastBulldozer)y.EntityTractor; + if (EntityX.OptionalColor.Name != EntityY.OptionalColor.Name) + { + return EntityX.OptionalColor.Name.CompareTo(EntityY.OptionalColor.Name); + } + } + var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight); + } + } +} diff --git a/Bulldozer/Bulldozer/BulldozerCompareByType.cs b/Bulldozer/Bulldozer/BulldozerCompareByType.cs new file mode 100644 index 0000000..6122987 --- /dev/null +++ b/Bulldozer/Bulldozer/BulldozerCompareByType.cs @@ -0,0 +1,32 @@ +using Bulldozer.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCompareByType : IComparer + { + public int Compare(DrawningBulldozer? x, DrawningBulldozer? y) + { + if (x == null || x.EntityTractor == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityTractor == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight); + } + } +} diff --git a/Bulldozer/Bulldozer/BulldozerGenericCollection.cs b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs index f2879f5..8ca558e 100644 --- a/Bulldozer/Bulldozer/BulldozerGenericCollection.cs +++ b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs @@ -37,6 +37,7 @@ namespace Bulldozer.Generics /// Набор объектов /// private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Конструктор /// @@ -56,14 +57,14 @@ namespace Bulldozer.Generics /// /// /// - public static int? operator +(BulldozerGenericCollection collect, T? + public static bool operator +(BulldozerGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawiningBulldozerEqutables()) ?? false; } /// /// Перегрузка оператора вычитания @@ -71,15 +72,15 @@ namespace Bulldozer.Generics /// /// /// - public static bool operator -(BulldozerGenericCollection collect, int + public static T? operator -(BulldozerGenericCollection collect, int pos) { T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } /// /// Получение объекта IMoveableObject diff --git a/Bulldozer/Bulldozer/BulldozersCollectionInfo.cs b/Bulldozer/Bulldozer/BulldozersCollectionInfo.cs new file mode 100644 index 0000000..72808e0 --- /dev/null +++ b/Bulldozer/Bulldozer/BulldozersCollectionInfo.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozersCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BulldozersCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BulldozersCollectionInfo? other) + { + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Bulldozer/Bulldozer/BulldozersGenericStorage.cs b/Bulldozer/Bulldozer/BulldozersGenericStorage.cs index 9b5f663..2626c1d 100644 --- a/Bulldozer/Bulldozer/BulldozersGenericStorage.cs +++ b/Bulldozer/Bulldozer/BulldozersGenericStorage.cs @@ -16,12 +16,12 @@ namespace Bulldozer.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _tractorStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _tractorStorages.Keys.ToList(); + public List Keys => _tractorStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -49,7 +49,7 @@ namespace Bulldozer.Generics /// public BulldozersGenericStorage(int pictureWidth, int pictureHeight) { - _tractorStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -61,10 +61,10 @@ namespace Bulldozer.Generics public void AddSet(string name) { // TODO Прописать логику для добавления - if (!_tractorStorages.ContainsKey(name)) + if (!_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty))) { var tractorCollection = new BulldozerGenericCollection(_pictureWidth, _pictureHeight); - _tractorStorages.Add(name, tractorCollection); + _tractorStorages.Add(new BulldozersCollectionInfo(name, string.Empty), tractorCollection); } } /// @@ -74,9 +74,9 @@ namespace Bulldozer.Generics public void DelSet(string name) { // TODO Прописать логику для удаления - if (_tractorStorages.ContainsKey(name)) + if (_tractorStorages.ContainsKey(new BulldozersCollectionInfo(name, string.Empty))) { - _tractorStorages.Remove(name); + _tractorStorages.Remove(new BulldozersCollectionInfo(name, string.Empty)); } } /// @@ -88,10 +88,11 @@ namespace Bulldozer.Generics { get { + BulldozersCollectionInfo indObj = new BulldozersCollectionInfo(ind, string.Empty); // TODO Продумать логику получения набора - if (_tractorStorages.ContainsKey(ind)) + if (_tractorStorages.ContainsKey(indObj)) { - return _tractorStorages[ind]; + return _tractorStorages[indObj]; } return null; } @@ -108,7 +109,7 @@ namespace Bulldozer.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _tractorStorages) + foreach (KeyValuePair> record in _tractorStorages) { StringBuilder records = new(); foreach (DrawningBulldozer? elem in record.Value.GetBulldozer) @@ -181,7 +182,7 @@ namespace Bulldozer.Generics } } } - _tractorStorages.Add(name, collection); + _tractorStorages.Add(new BulldozersCollectionInfo(name, string.Empty), collection); } } } diff --git a/Bulldozer/Bulldozer/DrawiningBulldozerEqutables.cs b/Bulldozer/Bulldozer/DrawiningBulldozerEqutables.cs new file mode 100644 index 0000000..39affbe --- /dev/null +++ b/Bulldozer/Bulldozer/DrawiningBulldozerEqutables.cs @@ -0,0 +1,58 @@ +using Bulldozer.DrawningObjects; +using Bulldozer.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class DrawiningBulldozerEqutables : IEqualityComparer + { + public bool Equals(DrawningBulldozer? x, DrawningBulldozer? y) + { + if (x == null || x.EntityTractor == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTractor == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityTractor.Speed != y.EntityTractor.Speed) + { + return false; + } + if (x.EntityTractor.Weight != y.EntityTractor.Weight) + { + return false; + } + if (x.EntityTractor.MainColor != y.EntityTractor.MainColor) + { + return false; + } + if (x is EntityFastBulldozer && y is EntityFastBulldozer) + { + EntityFastBulldozer EntityX = (EntityFastBulldozer)x.EntityTractor; + EntityFastBulldozer EntityY = (EntityFastBulldozer)y.EntityTractor; + if (EntityX.OptionalColor != EntityY.OptionalColor) + return false; + if (EntityX.Covsh != EntityY.Covsh) + return false; + if (EntityX.Rearbucket != EntityY.Rearbucket) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningBulldozer obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs index aded9be..63ca33e 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -45,6 +45,8 @@ LoadToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); @@ -53,7 +55,7 @@ // // ButtonAddBulldozer // - ButtonAddBulldozer.Location = new Point(26, 314); + ButtonAddBulldozer.Location = new Point(27, 373); ButtonAddBulldozer.Name = "ButtonAddBulldozer"; ButtonAddBulldozer.Size = new Size(166, 40); ButtonAddBulldozer.TabIndex = 0; @@ -63,7 +65,7 @@ // // ButtonRemoveBulldozer // - ButtonRemoveBulldozer.Location = new Point(26, 400); + ButtonRemoveBulldozer.Location = new Point(27, 459); ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer"; ButtonRemoveBulldozer.Size = new Size(166, 39); ButtonRemoveBulldozer.TabIndex = 1; @@ -73,7 +75,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(26, 478); + ButtonRefreshCollection.Location = new Point(26, 505); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(166, 40); ButtonRefreshCollection.TabIndex = 2; @@ -93,7 +95,7 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); - maskedTextBoxNumber.Location = new Point(56, 371); + maskedTextBoxNumber.Location = new Point(57, 430); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 22); @@ -102,14 +104,16 @@ // // groupBox1 // + groupBox1.Controls.Add(ButtonSortByColor); groupBox1.Controls.Add(groupBox2); + groupBox1.Controls.Add(ButtonSortByType); groupBox1.Controls.Add(ButtonAddBulldozer); groupBox1.Controls.Add(ButtonRefreshCollection); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(ButtonRemoveBulldozer); groupBox1.Location = new Point(643, 30); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(209, 533); + groupBox1.Size = new Size(209, 552); groupBox1.TabIndex = 5; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; @@ -170,7 +174,7 @@ menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); menuStrip.Location = new Point(649, 3); menuStrip.Name = "menuStrip"; - menuStrip.Size = new Size(176, 24); + menuStrip.Size = new Size(56, 24); menuStrip.TabIndex = 6; menuStrip.Text = "menuStrip1"; // @@ -184,14 +188,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Size = new Size(133, 22); SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(133, 22); LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -206,6 +210,26 @@ saveFileDialog.Filter = "txt file | *.txt"; saveFileDialog.Title = "Выберите текстовый файл"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(26, 342); + ButtonSortByColor.Margin = new Padding(3, 2, 3, 2); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(166, 28); + ButtonSortByColor.TabIndex = 10; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(27, 310); + ButtonSortByType.Margin = new Padding(3, 2, 3, 2); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(166, 28); + ButtonSortByType.TabIndex = 9; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + // // FormBulldozerCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -247,5 +271,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/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs index d152d1b..7e3e0bd 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -250,6 +250,23 @@ namespace Bulldozer } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByColor()); + private void CompareBulldozers(IComparer comparer) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowBulldozer(); + } } } diff --git a/Bulldozer/Bulldozer/SetGeneric.cs b/Bulldozer/Bulldozer/SetGeneric.cs index 0c39b78..08d8b56 100644 --- a/Bulldozer/Bulldozer/SetGeneric.cs +++ b/Bulldozer/Bulldozer/SetGeneric.cs @@ -30,36 +30,16 @@ namespace Bulldozer.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемая установка /// - public int Insert(T tractor) + public bool Insert(T tractor, IEqualityComparer? equal = null) { - if (_places.Count == 0) - { - _places.Add(tractor); - return 0; - } - else - { - if (_places.Count < _maxCount) - { - _places.Add(tractor); - for (int i = 0; i < _places.Count; i++) - { - T temp = _places[i]; - _places[i] = _places[_places.Count - 1]; - _places[_places.Count - 1] = temp; - } - return 0; - } - else - { - throw new StorageOverflowException(_places.Count); - } - } + Insert(tractor, 0, equal); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -67,7 +47,7 @@ namespace Bulldozer.Generics /// Добавляемая установкаь /// Позиция /// - public bool Insert(T tractor, int position) + public bool Insert(T tractor, int position, IEqualityComparer? equal = null) { // TODO проверка позиции if (position < 0 || position >= _maxCount) @@ -77,9 +57,13 @@ namespace Bulldozer.Generics } if (Count >= _maxCount) throw new StorageOverflowException(position); - _places.Insert(0, tractor); + if (equal != null) + { + if (_places.Contains(tractor, equal)) + throw new ArgumentException(nameof(tractor)); + } + _places.Insert(position, tractor); return true; - } /// /// Удаление объекта из набора с конкретной позиции @@ -90,13 +74,8 @@ namespace Bulldozer.Generics { // TODO проверка позиции // Проверка позиции - if (position < 0 || position > _maxCount || position >= Count) - throw new BulldozerNotFoundException(); - if (_places[position] == null) - { - throw new BulldozerNotFoundException(); - } - _places[position] = null; + if ((position < 0) || (position > _maxCount) || (position >= Count)) throw new BulldozerNotFoundException(position); + _places.RemoveAt(position); return true; } ///