From c5101e33b0d6d3840a7b5c28ac014ea0c94b1e5a Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Thu, 22 Dec 2022 23:14:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=208?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjectLocomotive.cs | 30 ++++++++- .../ElectricLocomotive - Backup (1).csproj | 43 +++++++++++++ .../ElectricLocomotive.csproj | 5 ++ .../FormMapWithSetLocomotive.Designer.cs | 27 +++++++- .../FormMapWithSetLocomotive.cs | 50 +++++++-------- .../ElectricLocomotive/IDrawningObject.cs | 2 +- .../LocomotiveCompareByColor.cs | 62 +++++++++++++++++++ .../LocomotiveCompareByType.cs | 56 +++++++++++++++++ .../MapWithSetLocomotivGeneric.cs | 7 ++- .../ElectricLocomotive/SetLocomotivGeneric.cs | 18 +++++- 10 files changed, 266 insertions(+), 34 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup (1).csproj create mode 100644 ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs create mode 100644 ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs index ac8d69b..e1ee713 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs @@ -8,7 +8,8 @@ namespace ElectricLocomotive { internal class DrawningObjectLocomotive : IDrawningObject { - private DrawningLocomotive _locomotive = null; + public DrawningLocomotive _locomotive = null; + public DrawningLocomotive GetLocomotive => _locomotive; public DrawningObjectLocomotive(DrawningLocomotive locomotive) { _locomotive = locomotive; @@ -30,6 +31,33 @@ namespace ElectricLocomotive { _locomotive.DrawTransport(g); } + + public bool Equals(IDrawningObject? other) + { + if (other is not DrawningObjectLocomotive otherShip) + { + return false; + } + var entity = _locomotive.Locomotive; + var otherEntity = otherShip._locomotive.Locomotive; + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) + { + return false; + } + if (entity is EntityElectricLocomotive entityWarmlyShip && + otherEntity is EntityElectricLocomotive otherEntityWarmlyShip && ( + entityWarmlyShip.BodyKit != otherEntityWarmlyShip.BodyKit || + entityWarmlyShip.DopColor != otherEntityWarmlyShip.DopColor || + entityWarmlyShip.SportLine != otherEntityWarmlyShip.SportLine)) + { + return false; + } + return true; + } + public string GetInfo() => _locomotive?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningCar()); } diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup (1).csproj b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup (1).csproj new file mode 100644 index 0000000..b5d9331 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive - Backup (1).csproj @@ -0,0 +1,43 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + + + + + + + + + + + + + + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj index b5d9331..eff36cf 100644 --- a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj +++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.csproj @@ -8,6 +8,11 @@ enable + + + + + diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index f00674f..2aed019 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -50,6 +50,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -148,7 +150,6 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 23); this.comboBoxSelectorMap.TabIndex = 22; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // maskedTextBoxPosition // @@ -268,11 +269,33 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(837, 288); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(151, 40); + this.ButtonSortByType.TabIndex = 32; + this.ButtonSortByType.Text = "Сортировать по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(837, 334); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(151, 40); + this.ButtonSortByColor.TabIndex = 33; + this.ButtonSortByColor.Text = "Сортировать по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetLocomotive // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1000, 722); + this.Controls.Add(this.ButtonSortByColor); + this.Controls.Add(this.ButtonSortByType); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.textBoxNewMapName); this.Controls.Add(this.ButtonAddMap); @@ -324,5 +347,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs index b2f4918..07e2445 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -13,7 +13,6 @@ namespace ElectricLocomotive { public partial class FormMapWithSetLocomotive : Form { - private MapWithSetLocomotivGeneric _mapLocomotiveCollectionGeneric; private readonly Dictionary _mapDict = new() { {"Простая карта", new SimpleMap() }, @@ -52,32 +51,7 @@ namespace ElectricLocomotive ListBoxMaps.SelectedIndex = index; } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, - EventArgs e) - { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Карта с грязью": - map = new FieldMap(); - break; - case "Карта с кустами": - map = new BushesMap(); - break; - } - if (map != null) - { - _mapLocomotiveCollectionGeneric = new MapWithSetLocomotivGeneric( - pictureBox1.Width, pictureBox1.Height, map); - } - else - { - _mapLocomotiveCollectionGeneric = null; - } - } + private void ButtonAddLocomotive_Click(object sender, EventArgs e) { var FormLocmotiveConfig = new FormLocomotiveConfig(); @@ -271,6 +245,28 @@ namespace ElectricLocomotive } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new CarCompareByType()); + pictureBox1.Image = + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ShipCompareByColor()); + pictureBox1.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs b/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs index 70ae7c7..63005d9 100644 --- a/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs +++ b/ElectricLocomotive/ElectricLocomotive/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..7273ae8 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class ShipCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xShip = x as DrawningObjectLocomotive; + var yShip = y as DrawningObjectLocomotive; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + var xEntityShip = xShip.GetLocomotive.Locomotive; + var yEntityShip = yShip.GetLocomotive.Locomotive; + var baseColorCompare = xEntityShip.BodyColor.ToArgb().CompareTo(yEntityShip.BodyColor.ToArgb()); + if (baseColorCompare != 0) + { + return baseColorCompare; + } + if (xEntityShip is EntityElectricLocomotive xWarmlyShip && yEntityShip is EntityElectricLocomotive yWarmlyShip) + { + var dopColorCompare = xWarmlyShip.DopColor.ToArgb().CompareTo(yWarmlyShip.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xShip.GetLocomotive.Locomotive.Speed.CompareTo(yShip.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetLocomotive.Locomotive.Weight.CompareTo(yShip.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs new file mode 100644 index 0000000..8018bef --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class CarCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xCar = x as DrawningObjectLocomotive; + var yCar = y as DrawningObjectLocomotive; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + if (xCar.GetLocomotive.GetType().Name != yCar.GetLocomotive.GetType().Name) + { + if (xCar.GetLocomotive.GetType().Name == "DrawningLocomotive") + { + return -1; + } + return 1; + } + var speedCompare = + xCar.GetLocomotive.Locomotive.Speed.CompareTo(yCar.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetLocomotive.Locomotive.Weight.CompareTo(yCar.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs index 7e7c322..942d5f6 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -1,7 +1,7 @@ namespace ElectricLocomotive { internal class MapWithSetLocomotivGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { // Ширина окна отрисовки @@ -148,5 +148,10 @@ _setLocomotive.Insert(DrawningObjectLocomotive.Create(rec) as T); } } + + public void Sort(IComparer comparer) + { + _setLocomotive.SortSet(comparer); + } } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs index db34f66..d73eb9e 100644 --- a/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { internal class SetLocomotivGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -23,11 +23,14 @@ namespace ElectricLocomotive } public int Insert(T locomotive, int position) { - if (Count == _maxCount) + if (_places.Contains(locomotive)) + { + throw new ArgumentException($"Объект {locomotive} уже присутствует в наборе"); + } + if (position < 0 || position > Count || _maxCount == Count) { throw new StorageOverflowException(_maxCount); } - if (position < 0 || position > _maxCount) return -1; _places.Insert(position, locomotive); return position; } @@ -75,5 +78,14 @@ namespace ElectricLocomotive } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + } }