From a8bc30a07904c3e8cb3192c612e69c0fd83d1fd1 Mon Sep 17 00:00:00 2001 From: platoff aeeee Date: Sun, 24 Dec 2023 14:57:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=208=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/Drawings/DrawingTankEqutables.cs | 76 +++++++++++++++++++ .../Tank/FormArmoredCarCollection.Designer.cs | 40 ++++++++-- Tank/Tank/FormArmoredCarCollection.cs | 16 +++- Tank/Tank/FormArmoredCarCollection.resx | 2 +- Tank/Tank/Generics/SetGeneric.cs | 24 ++++-- Tank/Tank/Generics/TanksGenericCollection.cs | 3 +- Tank/Tank/Generics/TanksGenericStorage.cs | 8 +- Tank/Tank/TankCollectionInfo.cs | 30 ++++++++ Tank/Tank/TankCompareByColor.cs | 39 ++++++++++ Tank/Tank/TankCompareByType.cs | 34 +++++++++ 10 files changed, 252 insertions(+), 20 deletions(-) create mode 100644 Tank/Tank/Drawings/DrawingTankEqutables.cs create mode 100644 Tank/Tank/TankCollectionInfo.cs create mode 100644 Tank/Tank/TankCompareByColor.cs create mode 100644 Tank/Tank/TankCompareByType.cs diff --git a/Tank/Tank/Drawings/DrawingTankEqutables.cs b/Tank/Tank/Drawings/DrawingTankEqutables.cs new file mode 100644 index 0000000..7ccb1ee --- /dev/null +++ b/Tank/Tank/Drawings/DrawingTankEqutables.cs @@ -0,0 +1,76 @@ +using global::Tank.Entites; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawingObjects; +using Tank.Entites; + +namespace Tank.Drawings +{ + internal class DrawiningTankEqutables : IEqualityComparer + { + public bool Equals(DrawingArmoredCar? x, DrawingArmoredCar? 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 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 DrawingTank && y is DrawingTank) + { + if (x.Tank as EntityTank == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y.Tank as EntityTank == null) + { + throw new ArgumentNullException(nameof(y)); + } + 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).Trunk != (y.Tank as EntityTank).Trunk) + { + return false; + } + if ((x.Tank as EntityTank).Line != (y.Tank as EntityTank).Line) + { + return false; + } + + } + return true; + } + + public int GetHashCode([DisallowNull] DrawingArmoredCar obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Tank/Tank/FormArmoredCarCollection.Designer.cs b/Tank/Tank/FormArmoredCarCollection.Designer.cs index ed18cea..10ac94c 100644 --- a/Tank/Tank/FormArmoredCarCollection.Designer.cs +++ b/Tank/Tank/FormArmoredCarCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -55,6 +57,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.ButtonSortByColor); + this.groupBox1.Controls.Add(this.ButtonSortByType); this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.maskedTextBoxNumber); this.groupBox1.Controls.Add(this.ButtonRefreshCollection); @@ -62,11 +66,31 @@ this.groupBox1.Controls.Add(this.ButtonAddArmoredCar); this.groupBox1.Location = new System.Drawing.Point(579, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(194, 343); + this.groupBox1.Size = new System.Drawing.Size(194, 400); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(18, 211); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(144, 23); + this.ButtonSortByColor.TabIndex = 10; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(18, 182); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(144, 23); + this.ButtonSortByType.TabIndex = 9; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBox2 // this.groupBox2.Controls.Add(this.textBoxStorageName); @@ -119,14 +143,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(18, 235); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(18, 292); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(129, 23); this.maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(6, 310); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(6, 367); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(172, 27); this.ButtonRefreshCollection.TabIndex = 2; @@ -136,7 +160,7 @@ // // ButtonRemoveArmoredCar // - this.ButtonRemoveArmoredCar.Location = new System.Drawing.Point(6, 264); + this.ButtonRemoveArmoredCar.Location = new System.Drawing.Point(6, 321); this.ButtonRemoveArmoredCar.Name = "ButtonRemoveArmoredCar"; this.ButtonRemoveArmoredCar.Size = new System.Drawing.Size(172, 40); this.ButtonRemoveArmoredCar.TabIndex = 1; @@ -146,7 +170,7 @@ // // ButtonAddArmoredCar // - this.ButtonAddArmoredCar.Location = new System.Drawing.Point(6, 190); + this.ButtonAddArmoredCar.Location = new System.Drawing.Point(6, 247); this.ButtonAddArmoredCar.Name = "ButtonAddArmoredCar"; this.ButtonAddArmoredCar.Size = new System.Drawing.Size(172, 39); this.ButtonAddArmoredCar.TabIndex = 0; @@ -165,9 +189,9 @@ // panelStrip // this.panelStrip.Controls.Add(this.StripMenu); - this.panelStrip.Location = new System.Drawing.Point(585, 349); + this.panelStrip.Location = new System.Drawing.Point(585, 406); this.panelStrip.Name = "panelStrip"; - this.panelStrip.Size = new System.Drawing.Size(172, 89); + this.panelStrip.Size = new System.Drawing.Size(172, 44); this.panelStrip.TabIndex = 2; // // StripMenu @@ -255,5 +279,7 @@ private ToolStripMenuItem openFileDialog; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog1; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/Tank/Tank/FormArmoredCarCollection.cs b/Tank/Tank/FormArmoredCarCollection.cs index b611e74..7dc4f14 100644 --- a/Tank/Tank/FormArmoredCarCollection.cs +++ b/Tank/Tank/FormArmoredCarCollection.cs @@ -132,8 +132,7 @@ namespace Tank { return; } - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? - string.Empty]; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; @@ -213,5 +212,18 @@ namespace Tank } } } + private void CompareTank(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.ShowTanks(); + } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTank(new TankCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTank(new TankCompareByColor()); } } diff --git a/Tank/Tank/FormArmoredCarCollection.resx b/Tank/Tank/FormArmoredCarCollection.resx index abc27ca..0e807d5 100644 --- a/Tank/Tank/FormArmoredCarCollection.resx +++ b/Tank/Tank/FormArmoredCarCollection.resx @@ -67,6 +67,6 @@ 294, 7 - 67 + 39 \ No newline at end of file diff --git a/Tank/Tank/Generics/SetGeneric.cs b/Tank/Tank/Generics/SetGeneric.cs index 02cf88e..ea81956 100644 --- a/Tank/Tank/Generics/SetGeneric.cs +++ b/Tank/Tank/Generics/SetGeneric.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tank.Exceptions; namespace Tank.Generics { @@ -17,30 +18,37 @@ namespace Tank.Generics public int Count => _places.Count; private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; _places = new List(_maxCount); } - 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) - return false; + if (position < 0 || position >= _maxCount) + throw new TankNotFoundException(position); + if (Count >= _maxCount) - return false; + throw new TankStorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(tank, equal)) + throw new ApplicationException("Уже есть"); + _places.Insert(0, tank); return true; } public bool Remove(int position) { - if (position < 0 || position > _maxCount) - return false; + if (position < 0 || position > _maxCount || position >= Count) + throw new TankNotFoundException(position); + _places.RemoveAt(position); return true; } diff --git a/Tank/Tank/Generics/TanksGenericCollection.cs b/Tank/Tank/Generics/TanksGenericCollection.cs index fb43f71..f1d4c6e 100644 --- a/Tank/Tank/Generics/TanksGenericCollection.cs +++ b/Tank/Tank/Generics/TanksGenericCollection.cs @@ -84,7 +84,8 @@ namespace Tank.Generics } return obj; } - + // Сортировка + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Получение объекта IMoveableObject /// diff --git a/Tank/Tank/Generics/TanksGenericStorage.cs b/Tank/Tank/Generics/TanksGenericStorage.cs index 14328f1..69066c8 100644 --- a/Tank/Tank/Generics/TanksGenericStorage.cs +++ b/Tank/Tank/Generics/TanksGenericStorage.cs @@ -64,6 +64,8 @@ namespace Tank.Generics /// Название набора public void AddSet(string name) { + TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty); + if (_tankStorages.ContainsKey(name)) { return; @@ -79,6 +81,8 @@ namespace Tank.Generics /// Название набора public void DelSet(string name) { + TankCollectionInfo Info = new TankCollectionInfo(name, string.Empty); + if (_tankStorages.ContainsKey(name)) { _tankStorages.Remove(name); @@ -98,6 +102,8 @@ namespace Tank.Generics { get { + TankCollectionInfo Info = new TankCollectionInfo(ind, string.Empty); + if (_tankStorages.ContainsKey(ind)) { return _tankStorages[ind]; @@ -125,7 +131,7 @@ namespace Tank.Generics } if (data.Length == 0) { - throw new Exception("Невалиданя операция, нет данных для сохранения"); + throw new Exception("Невалидная операция, нет данных для сохранения"); } using (StreamWriter writer = new StreamWriter(filename)) { diff --git a/Tank/Tank/TankCollectionInfo.cs b/Tank/Tank/TankCollectionInfo.cs new file mode 100644 index 0000000..863a97c --- /dev/null +++ b/Tank/Tank/TankCollectionInfo.cs @@ -0,0 +1,30 @@ +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 override int GetHashCode() + { + return Name?.GetHashCode() ?? 0; + } + public bool Equals(TankCollectionInfo? other) + { + if (other == null || Name == null || other.Name == null) return false; + if (Name == other?.Name) return true; + throw new NotImplementedException(); + } + } +} diff --git a/Tank/Tank/TankCompareByColor.cs b/Tank/Tank/TankCompareByColor.cs new file mode 100644 index 0000000..14a4606 --- /dev/null +++ b/Tank/Tank/TankCompareByColor.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.DrawingObjects; +using Tank.Entites; + +namespace Tank +{ + internal class TankCompareByColor : IComparer + { + public int Compare(DrawingArmoredCar? x, DrawingArmoredCar? 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); + } + if (x.GetType() == y.GetType() && x is DrawingTank) + { + return (x.Tank as EntityTank).AdditionalColor.Name.CompareTo((y.Tank as EntityTank).AdditionalColor.Name); + } + var speedCompare = x.Tank.Speed.CompareTo(y.Tank.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.Tank.Weight.CompareTo(y.Tank.Weight); + } + } +} diff --git a/Tank/Tank/TankCompareByType.cs b/Tank/Tank/TankCompareByType.cs new file mode 100644 index 0000000..acfcce1 --- /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.DrawingObjects; + +namespace Tank +{ + internal class TankCompareByType : IComparer + { + public int Compare(DrawingArmoredCar? x, DrawingArmoredCar? 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); + } + } + }