From 5ec3ff18fbb1fed16a6e670eb3f48dcacfab22b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80=20=D0=9D=D1=83=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Tue, 22 Nov 2022 11:34:09 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=B0=D1=8F=20=D1=84=D0=B8=D0=BA=D1=81?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=201.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bus/Bus/DrawingObjectBus.cs | 30 +++++++++++++++++++++++++++++- Bus/Bus/IDrawingObject.cs | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index a3b541d..ae73512 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -39,6 +39,34 @@ namespace Bus public string GetInfo() => _bus?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectBus(data.CreateDrawingBus()); - + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherBus = other as DrawingObjectBus; + if (otherBus == null) + { + return false; + } + var bus = _bus.Bus; + var otherBusBus = otherBus._bus.Bus; + if (bus.Speed != otherBusBus.Speed) + { + return false; + } + if (bus.Weight != otherBusBus.Weight) + { + return false; + } + if (bus.BodyColor != otherBusBus.BodyColor) + { + return false; + } + // TODO доделать проверки в случае продвинутого объекта + return true; + } } } diff --git a/Bus/Bus/IDrawingObject.cs b/Bus/Bus/IDrawingObject.cs index 321b39f..f41e4b4 100644 --- a/Bus/Bus/IDrawingObject.cs +++ b/Bus/Bus/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Bus { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); -- 2.25.1 From e3ab4bf429d0db86d89108ae586189a284d5cdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80=20=D0=9D=D1=83=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Sat, 3 Dec 2022 20:37:58 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bus/Bus/DrawingObjectBus.cs | 26 ++++++++++----------- Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs | 2 +- Bus/Bus/SetDoubleDeckerBusGeneric.cs | 17 ++++++-------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index ae73512..0cdc7b7 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -47,25 +47,25 @@ namespace Bus return false; } var otherBus = other as DrawingObjectBus; - if (otherBus == null) + var entity = _bus.Bus; + var otherEntity = otherBus._bus.Bus; + + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) { return false; } - var bus = _bus.Bus; - var otherBusBus = otherBus._bus.Bus; - if (bus.Speed != otherBusBus.Speed) + + if (entity is EntitySportBus entitySportBus && + otherEntity is EntitySportBus otherEntitySportBus && ( + entitySportBus.Wing != otherEntitySportBus.Wing || + entitySportBus.DopColor != otherEntitySportBus.DopColor || + entitySportBus.Sportline != otherEntitySportBus.Sportline)) { return false; } - if (bus.Weight != otherBusBus.Weight) - { - return false; - } - if (bus.BodyColor != otherBusBus.BodyColor) - { - return false; - } - // TODO доделать проверки в случае продвинутого объекта return true; } } diff --git a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs index 635c3f8..c709b6e 100644 --- a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Bus { internal class MapWithSetDoubleDeckerBusGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; diff --git a/Bus/Bus/SetDoubleDeckerBusGeneric.cs b/Bus/Bus/SetDoubleDeckerBusGeneric.cs index e34e350..dfefa76 100644 --- a/Bus/Bus/SetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/SetDoubleDeckerBusGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Bus { internal class SetDoubleDeckerBusGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -33,18 +33,15 @@ namespace Bus public int Insert(T bus, int position) { - if (position > _maxCount && position < 0) + + if (_places.Contains(bus)) + throw new ArgumentException($"Объект {bus} уже есть в наборе"); + if (Count == _maxCount) + throw new StorageOverflowException(_maxCount); + if (!isCorrectPosition(position)) { return -1; } - if (_places.Contains(bus)) - { - throw new ArgumentException($"Объект {bus} уже есть в наборе"); - } - if (Count == _maxCount) - { - throw new StorageOverflowException(_maxCount); - } _places.Insert(position, bus); return position; } -- 2.25.1 From 75aeb9e0ec33878671dc3abf0d7bc3088ea47078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80=20=D0=9D=D1=83=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Sat, 3 Dec 2022 21:43:09 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A1=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bus/Bus/BusComperyByColor.cs | 78 +++++++++++++++++++ Bus/Bus/BusComperyByType.cs | 63 +++++++++++++++ Bus/Bus/DrawingObjectBus.cs | 2 + .../FormMapWithSetDoubleDeckerBus.Designer.cs | 54 +++++++++---- Bus/Bus/FormMapWithSetDoubleDeckerBus.cs | 24 +++++- Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs | 5 ++ Bus/Bus/SetDoubleDeckerBusGeneric.cs | 10 +++ 7 files changed, 221 insertions(+), 15 deletions(-) create mode 100644 Bus/Bus/BusComperyByColor.cs create mode 100644 Bus/Bus/BusComperyByType.cs diff --git a/Bus/Bus/BusComperyByColor.cs b/Bus/Bus/BusComperyByColor.cs new file mode 100644 index 0000000..139651f --- /dev/null +++ b/Bus/Bus/BusComperyByColor.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusComperyByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + + if (x == null && y != null) + { + return 1; + } + + if (x != null && y != null) + { + return -1; + } + + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + + if (xBus == null && yBus == null) + { + return 0; + } + + if (xBus == null && yBus != null) + { + return 1; + } + + if (xBus != null && yBus == null) + { + return 1; + } + + var xEntity = xBus.GetBus.Bus; + var yEntity = yBus.GetBus.Bus; + //var colorCompare = xEntity.DopColor.ToArgb().CompareTo(yEntity.DopColor.ToArgb()); + + //int i = xEntity.DopColor.ToArgb(); + //int j = yEntity.DopColor.ToArgb(); + + /*if (colorCompare != 0) + { + return colorCompare; + }*/ + + if (xEntity is EntitySportBus xEntityAirbus && yEntity is EntitySportBus yEntityAirbus) + { + var addColorCompare = xEntityAirbus.DopColor.ToArgb().CompareTo(yEntityAirbus.DopColor.ToArgb()); + + if (addColorCompare != 0) + { + return addColorCompare; + } + } + + var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); + } + } +} diff --git a/Bus/Bus/BusComperyByType.cs b/Bus/Bus/BusComperyByType.cs new file mode 100644 index 0000000..9027a5a --- /dev/null +++ b/Bus/Bus/BusComperyByType.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusComperyByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y != null) + { + return -1; + } + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + + if (xBus == null && yBus == null) + { + return 0; + } + + if (xBus == null && yBus != null) + { + return 1; + } + + if (xBus != null && yBus == null) + { + return 1; + } + + if (xBus.GetBus.GetType().Name != yBus.GetBus.GetType().Name) + { + if (xBus.GetBus.GetType().Name == "DrawningAirbus") + { + return -1; + } + + return -1; + } + + var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); + } + } +} diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index 0cdc7b7..09f8c8d 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -11,6 +11,8 @@ namespace Bus private DrawingBus _bus = null; public float Step => _bus?.Bus?.Step ?? 0; + public DrawingBus GetBus => _bus; + public DrawingObjectBus(DrawingBus bus) { _bus = bus; diff --git a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs index be73e42..6d616f8 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.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.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); @@ -59,6 +61,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.buttonSortByColor); + this.groupBox1.Controls.Add(this.buttonSortByType); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); this.groupBox1.Controls.Add(this.buttonLeft); @@ -71,17 +75,37 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(598, 28); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(250, 590); + this.groupBox1.Size = new System.Drawing.Size(250, 650); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(17, 299); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(173, 29); + this.buttonSortByColor.TabIndex = 13; + this.buttonSortByColor.Text = "Сохранить по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(16, 260); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(174, 29); + this.buttonSortByType.TabIndex = 12; + this.buttonSortByType.Text = "Сохранить по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Bus.Properties.Resources.Frame_4; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(95, 538); + this.buttonRight.Location = new System.Drawing.Point(95, 598); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(40, 40); this.buttonRight.TabIndex = 10; @@ -93,7 +117,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Bus.Properties.Resources.Frame_5; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(55, 538); + this.buttonDown.Location = new System.Drawing.Point(55, 598); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(40, 40); this.buttonDown.TabIndex = 9; @@ -105,7 +129,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Bus.Properties.Resources.Frame_6; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(15, 538); + this.buttonLeft.Location = new System.Drawing.Point(15, 598); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(40, 40); this.buttonLeft.TabIndex = 8; @@ -117,7 +141,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Bus.Properties.Resources.Frame_3; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(55, 498); + this.buttonUp.Location = new System.Drawing.Point(55, 558); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(40, 40); this.buttonUp.TabIndex = 7; @@ -126,7 +150,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(15, 426); + this.buttonShowOnMap.Location = new System.Drawing.Point(15, 523); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(193, 29); this.buttonShowOnMap.TabIndex = 5; @@ -136,7 +160,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(15, 391); + this.buttonShowStorage.Location = new System.Drawing.Point(15, 488); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(193, 29); this.buttonShowStorage.TabIndex = 4; @@ -146,14 +170,14 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 320); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 417); this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(100, 27); this.maskedTextBoxPosition.TabIndex = 11; // // buttonRemoveBus // - this.buttonRemoveBus.Location = new System.Drawing.Point(15, 353); + this.buttonRemoveBus.Location = new System.Drawing.Point(15, 450); this.buttonRemoveBus.Name = "buttonRemoveBus"; this.buttonRemoveBus.Size = new System.Drawing.Size(129, 32); this.buttonRemoveBus.TabIndex = 2; @@ -163,7 +187,7 @@ // // buttonAddBus // - this.buttonAddBus.Location = new System.Drawing.Point(15, 281); + this.buttonAddBus.Location = new System.Drawing.Point(15, 378); this.buttonAddBus.Name = "buttonAddBus"; this.buttonAddBus.Size = new System.Drawing.Size(129, 33); this.buttonAddBus.TabIndex = 1; @@ -188,7 +212,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(598, 590); + this.pictureBox.Size = new System.Drawing.Size(598, 650); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -265,14 +289,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -288,7 +312,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(848, 618); + this.ClientSize = new System.Drawing.Size(848, 678); this.Controls.Add(this.groupBox2); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); @@ -333,5 +357,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/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs index 79205ec..7fcdbdd 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs @@ -66,7 +66,7 @@ namespace Bus map = new MyMap(); break; } - if(map != null) + if (map != null) { _mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric(pictureBox.Width, pictureBox.Height, map); } @@ -272,5 +272,27 @@ namespace Bus } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusComperyByType()); + pictureBox.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 BusComperyByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs index c709b6e..a22b684 100644 --- a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs @@ -92,6 +92,11 @@ namespace Bus } } + public void Sort(IComparer comparer) + { + _setBus.SortSet(comparer); + } + private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); diff --git a/Bus/Bus/SetDoubleDeckerBusGeneric.cs b/Bus/Bus/SetDoubleDeckerBusGeneric.cs index dfefa76..2cc8965 100644 --- a/Bus/Bus/SetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/SetDoubleDeckerBusGeneric.cs @@ -94,5 +94,15 @@ namespace Bus } } } + + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + + _places.Sort(comparer); + } } } -- 2.25.1 From f73284338db2f303e61bf3ad5a5295eb3cb186c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80=20=D0=9D=D1=83=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Mon, 5 Dec 2022 23:02:12 +0400 Subject: [PATCH 4/4] fix --- Bus/Bus/AbstractMap.cs | 50 ++++++------ Bus/Bus/BusCompareByColor.cs | 39 ++++++++++ Bus/Bus/BusCompareByType.cs | 43 ++++++++++ Bus/Bus/BusComperyByColor.cs | 78 ------------------- Bus/Bus/BusComperyByType.cs | 63 --------------- Bus/Bus/DrawingObjectBus.cs | 3 +- .../FormMapWithSetDoubleDeckerBus.Designer.cs | 2 +- Bus/Bus/FormMapWithSetDoubleDeckerBus.cs | 72 ++++++----------- Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs | 2 +- Bus/Bus/SetDoubleDeckerBusGeneric.cs | 36 +++------ 10 files changed, 146 insertions(+), 242 deletions(-) create mode 100644 Bus/Bus/BusCompareByColor.cs create mode 100644 Bus/Bus/BusCompareByType.cs delete mode 100644 Bus/Bus/BusComperyByColor.cs delete mode 100644 Bus/Bus/BusComperyByType.cs diff --git a/Bus/Bus/AbstractMap.cs b/Bus/Bus/AbstractMap.cs index 17d77f0..a2eb582 100644 --- a/Bus/Bus/AbstractMap.cs +++ b/Bus/Bus/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Bus { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -54,7 +54,6 @@ namespace Bus public Bitmap MoveObject(Direction direction) { - _drawingObject.MoveObject(direction); bool collision = CheckCollision(); @@ -80,23 +79,6 @@ namespace Bus return DrawMapWithObject(); } - private Direction MoveObjectBack(Direction direction) - { - switch (direction) - { - case Direction.Up: - return Direction.Down; - case Direction.Down: - return Direction.Up; - case Direction.Left: - return Direction.Right; - case Direction.Right: - return Direction.Left; - } - return Direction.None; - } - - private bool SetObjectOnMap() { if (_drawingObject == null || _map == null) @@ -135,8 +117,6 @@ namespace Bus return bmp; } - - private bool CheckCollision() { var pos = _drawingObject.GetCurrentPosition(); @@ -157,12 +137,38 @@ namespace Bus } } } - return false; } protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public bool Equals(AbstractMap? other) + { + if (other == null || + _map != other._map || + _width != other._width || + _size_x != other._size_x || + _size_y != other._size_y || + _height != other._height || + GetType() != other.GetType() || + _map.GetLength(0) != other._map.GetLength(0) || + _map.GetLength(1) != other._map.GetLength(1)) + { + return false; + } + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != other._map[i, j]) + { + return false; + } + } + } + return true; + } } } diff --git a/Bus/Bus/BusCompareByColor.cs b/Bus/Bus/BusCompareByColor.cs new file mode 100644 index 0000000..f2c7492 --- /dev/null +++ b/Bus/Bus/BusCompareByColor.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + if (xBus == yBus) + { + return 0; + } + if (xBus == null) + { + return 1; + } + if (yBus == null) + { + return -1; + } + var xEntity = xBus._bus.Bus; + var yEntity = yBus._bus.Bus; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || + xEntity is not EntitySportBus xEntitySportBus || + yEntity is not EntitySportBus yEntitySportBus) + { + return colorWeight; + } + return xEntitySportBus.DopColor.ToArgb().CompareTo(yEntitySportBus.DopColor.ToArgb()); + } + } +} diff --git a/Bus/Bus/BusCompareByType.cs b/Bus/Bus/BusCompareByType.cs new file mode 100644 index 0000000..a70003e --- /dev/null +++ b/Bus/Bus/BusCompareByType.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + if (xBus == yBus) + { + return 0; + } + if (xBus == null) + { + return 1; + } + if (yBus == null) + { + return -1; + } + if (xBus._bus.GetType().Name != yBus._bus.GetType().Name) + { + if (xBus._bus.GetType() == typeof(DrawingBus)) + { + return -1; + } + return 1; + } + var speedCompare = xBus._bus.Bus.Speed.CompareTo(yBus._bus.Bus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBus._bus.Bus.Weight.CompareTo(yBus._bus.Bus.Weight); + } + } +} diff --git a/Bus/Bus/BusComperyByColor.cs b/Bus/Bus/BusComperyByColor.cs deleted file mode 100644 index 139651f..0000000 --- a/Bus/Bus/BusComperyByColor.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Bus -{ - internal class BusComperyByColor : IComparer - { - public int Compare(IDrawingObject? x, IDrawingObject? y) - { - if (x == null && y == null) - { - return 0; - } - - if (x == null && y != null) - { - return 1; - } - - if (x != null && y != null) - { - return -1; - } - - var xBus = x as DrawingObjectBus; - var yBus = y as DrawingObjectBus; - - if (xBus == null && yBus == null) - { - return 0; - } - - if (xBus == null && yBus != null) - { - return 1; - } - - if (xBus != null && yBus == null) - { - return 1; - } - - var xEntity = xBus.GetBus.Bus; - var yEntity = yBus.GetBus.Bus; - //var colorCompare = xEntity.DopColor.ToArgb().CompareTo(yEntity.DopColor.ToArgb()); - - //int i = xEntity.DopColor.ToArgb(); - //int j = yEntity.DopColor.ToArgb(); - - /*if (colorCompare != 0) - { - return colorCompare; - }*/ - - if (xEntity is EntitySportBus xEntityAirbus && yEntity is EntitySportBus yEntityAirbus) - { - var addColorCompare = xEntityAirbus.DopColor.ToArgb().CompareTo(yEntityAirbus.DopColor.ToArgb()); - - if (addColorCompare != 0) - { - return addColorCompare; - } - } - - var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); - - if (speedCompare != 0) - { - return speedCompare; - } - - return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); - } - } -} diff --git a/Bus/Bus/BusComperyByType.cs b/Bus/Bus/BusComperyByType.cs deleted file mode 100644 index 9027a5a..0000000 --- a/Bus/Bus/BusComperyByType.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Bus -{ - internal class BusComperyByType : IComparer - { - public int Compare(IDrawingObject? x, IDrawingObject? y) - { - if (x == null && y == null) - { - return 0; - } - if (x == null && y != null) - { - return 1; - } - if (x != null && y != null) - { - return -1; - } - var xBus = x as DrawingObjectBus; - var yBus = y as DrawingObjectBus; - - if (xBus == null && yBus == null) - { - return 0; - } - - if (xBus == null && yBus != null) - { - return 1; - } - - if (xBus != null && yBus == null) - { - return 1; - } - - if (xBus.GetBus.GetType().Name != yBus.GetBus.GetType().Name) - { - if (xBus.GetBus.GetType().Name == "DrawningAirbus") - { - return -1; - } - - return -1; - } - - var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); - - if (speedCompare != 0) - { - return speedCompare; - } - - return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); - } - } -} diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index 09f8c8d..696a203 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -8,9 +8,8 @@ namespace Bus { internal class DrawingObjectBus : IDrawingObject { - private DrawingBus _bus = null; + public DrawingBus _bus = null; public float Step => _bus?.Bus?.Step ?? 0; - public DrawingBus GetBus => _bus; public DrawingObjectBus(DrawingBus bus) diff --git a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs index 6d616f8..00eaa34 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs @@ -88,7 +88,7 @@ this.buttonSortByColor.TabIndex = 13; this.buttonSortByColor.Text = "Сохранить по цвету"; this.buttonSortByColor.UseVisualStyleBackColor = true; - this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); // // buttonSortByType // diff --git a/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs index 7fcdbdd..53ab154 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs @@ -13,8 +13,6 @@ namespace Bus { public partial class FormMapWithSetDoubleDeckerBus : Form { - private MapWithSetDoubleDeckerBusGeneric _mapBusCollectionGeneric; - private readonly Dictionary _mapsDict = new() { {"Простая карта", new SimpleMap() }, @@ -31,9 +29,8 @@ namespace Bus comboBoxSelectorMap.Items.Clear(); foreach (var item in _mapsDict) { - comboBoxSelectorMap.Items.Add(item.Key);// + comboBoxSelectorMap.Items.Add(item.Key); } - } private void ReloadMaps() @@ -65,21 +62,13 @@ namespace Bus case "Водная карта": map = new MyMap(); break; - } - if (map != null) - { - _mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapBusCollectionGeneric = null; - } + } } private void ButtonAddBus_Click(object sender, EventArgs e) { var formBusConfig = new FormBusConfig(); - formBusConfig.AddEvent(AddBus); + formBusConfig.AddEvent(new(AddBus)); formBusConfig.Show(); } @@ -89,15 +78,14 @@ namespace Bus { if (listBoxMaps.SelectedIndex == -1) { - return; + MessageBox.Show("Перед добавлением объекта необходимо создать карту"); } - DrawingObjectBus boat = new(bus); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0) + else if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectBus(bus) != -1) { MessageBox.Show("Объект добавлен"); - _logger.LogInformation("Добавлен объект {@Airbus}", bus); + _logger.LogInformation("Добавлен объект {@bus}", bus); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } + } else { MessageBox.Show("Не удалось добавить объект"); @@ -106,39 +94,31 @@ namespace Bus } catch (StorageOverflowException ex) { - _logger.LogWarning("Ошибка, переполнение хранилища: {0}", ex.Message); - MessageBox.Show($"Ошибка, хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + MessageBox.Show("Не удалось добавить объект"); + _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); + MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } catch (ArgumentException ex) { - _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airbus}", ex.Message, bus); + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@bus}", ex.Message, bus); MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonRemoveBus_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) - { - return; - } - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); try { - if (_mapBusCollectionGeneric - pos != null) + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBusCollectionGeneric.ShowSet(); + return; } - else + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { - MessageBox.Show("Не удалось удалить объект"); + return; } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); } catch (BusNotFoundException ex) { @@ -273,26 +253,18 @@ namespace Bus } } - private void ButtonSortByType_Click(object sender, EventArgs e) + private void SortBy(IComparer comparer) { if (listBoxMaps.SelectedIndex == -1) { return; } - - _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusComperyByType()); + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - private void buttonSortByColor_Click(object sender, EventArgs e) - { - if (listBoxMaps.SelectedIndex == -1) - { - return; - } + private void ButtonSortByType_Click(object sender, EventArgs e) => SortBy(new BusCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new BusCompareByColor()); - _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusComperyByColor()); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } } } diff --git a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs index a22b684..9954710 100644 --- a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Bus { internal class MapWithSetDoubleDeckerBusGeneric - where T : class, IDrawingObject, IEquatable + where T : class, IEquatable, IDrawingObject where U : AbstractMap { private readonly int _pictureWidth; diff --git a/Bus/Bus/SetDoubleDeckerBusGeneric.cs b/Bus/Bus/SetDoubleDeckerBusGeneric.cs index 2cc8965..861e5f3 100644 --- a/Bus/Bus/SetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/SetDoubleDeckerBusGeneric.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; @@ -11,8 +12,6 @@ namespace Bus { private readonly List _places; public int Count => _places.Count; - private int BusPlaces = 0; - private readonly int _maxCount; public SetDoubleDeckerBusGeneric(int count) @@ -33,30 +32,25 @@ namespace Bus public int Insert(T bus, int position) { - - if (_places.Contains(bus)) - throw new ArgumentException($"Объект {bus} уже есть в наборе"); - if (Count == _maxCount) - throw new StorageOverflowException(_maxCount); - if (!isCorrectPosition(position)) + if (_places.Contains(bus)) return 0; + if (position < 0) return -1; + if (Count >= _maxCount) { - return -1; + + throw new StorageOverflowException(_maxCount); } _places.Insert(position, bus); + return position; } public T Remove(int position) { - if (position < 0 || position >= _maxCount) - { + if (!isCorrectPosition(position)) return null; - } - if (position >= Count || position < 0) - { + var result = this[position]; + if (result == null) throw new BusNotFoundException(position); - } - var result = _places[position]; _places.RemoveAt(position); return result; } @@ -65,14 +59,7 @@ namespace Bus { get { - if (position >= 0 && position < _maxCount && position < Count) - { - return _places[position]; - } - else - { - return null; - } + return isCorrectPosition(position) && position < Count ? _places[position] : null; } set { @@ -101,7 +88,6 @@ namespace Bus { return; } - _places.Sort(comparer); } } -- 2.25.1