diff --git a/ContainerShip/ContainerShip/DrawingObjectShip.cs b/ContainerShip/ContainerShip/DrawingObjectShip.cs index c2a9e09..f689372 100644 --- a/ContainerShip/ContainerShip/DrawingObjectShip.cs +++ b/ContainerShip/ContainerShip/DrawingObjectShip.cs @@ -14,7 +14,7 @@ namespace ContainerShip _ship = ship; } public float Step => _ship?.Ship?.Step ?? 0; - + public DrawingShip GetShip => _ship; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _ship?.GetCurrentPosition() ?? default; @@ -36,5 +36,49 @@ namespace ContainerShip public string GetInfo() => _ship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherShip = other as DrawingObjectShip; + if (otherShip == null) + { + return false; + } + var ship = _ship.Ship; + var otherShipShip = otherShip._ship.Ship; + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + if (ship is EntityContainerShip containerShip && otherShipShip is EntityContainerShip otherContainerShip) + { + if (containerShip.DopColor != otherContainerShip.DopColor) + { + return false; + } + if (containerShip.Crane != otherContainerShip.Crane) + { + return false; + } + if (containerShip.Containers != otherContainerShip.Containers) + { + return false; + } + } + return true; + + } } } diff --git a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs index 83fed4a..6c71f53 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs @@ -51,6 +51,8 @@ this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.buttonSortByType); + this.groupBox1.Controls.Add(this.buttonSortByColor); this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonAddShip); @@ -72,7 +76,7 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(600, 24); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 512); + this.groupBox1.Size = new System.Drawing.Size(200, 666); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -139,11 +143,11 @@ this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); - this.comboBoxSelectorMap.TabIndex = 18; + this.comboBoxSelectorMap.TabIndex = 18; // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 294); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 374); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); @@ -151,7 +155,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(6, 265); + this.buttonAddShip.Location = new System.Drawing.Point(6, 345); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(188, 23); this.buttonAddShip.TabIndex = 17; @@ -161,7 +165,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(6, 323); + this.buttonRemoveShip.Location = new System.Drawing.Point(6, 403); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23); this.buttonRemoveShip.TabIndex = 15; @@ -171,7 +175,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 376); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 456); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(188, 23); this.buttonShowStorage.TabIndex = 14; @@ -181,7 +185,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 405); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 485); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23); this.buttonShowOnMap.TabIndex = 13; @@ -194,7 +198,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(89, 470); + this.buttonDown.Location = new System.Drawing.Point(89, 624); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -206,7 +210,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(53, 470); + this.buttonLeft.Location = new System.Drawing.Point(53, 624); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; @@ -218,7 +222,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(125, 470); + this.buttonRight.Location = new System.Drawing.Point(125, 624); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -230,7 +234,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(89, 434); + this.buttonUp.Location = new System.Drawing.Point(89, 588); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 9; @@ -242,7 +246,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(600, 512); + this.pictureBox.Size = new System.Drawing.Size(600, 666); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -268,14 +272,14 @@ // saveToolStripMenuItem // this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - this.saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.saveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.saveToolStripMenuItem.Text = "Сохранение"; this.saveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // loadToolStripMenuItem // this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - this.loadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.loadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.loadToolStripMenuItem.Text = "Загрузка"; this.loadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -288,11 +292,31 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(6, 305); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(188, 34); + this.buttonSortByColor.TabIndex = 21; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(6, 265); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(188, 34); + this.buttonSortByType.TabIndex = 22; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // FormMapWithSetShip // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 536); + this.ClientSize = new System.Drawing.Size(800, 690); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Controls.Add(this.menuStrip); @@ -336,5 +360,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/ContainerShip/ContainerShip/FormMapWithSetShip.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.cs index ff6fd62..6a2cff5 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.cs @@ -261,5 +261,27 @@ namespace ContainerShip } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new ShipCompareByType()); + 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 ShipCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/ContainerShip/ContainerShip/IDrawingObject.cs b/ContainerShip/ContainerShip/IDrawingObject.cs index 79d3c43..2bdd688 100644 --- a/ContainerShip/ContainerShip/IDrawingObject.cs +++ b/ContainerShip/ContainerShip/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } diff --git a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs index ecd61f2..0586f46 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ContainerShip { internal class MapWithSetShipGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { @@ -175,5 +175,9 @@ namespace ContainerShip _setShip.Insert(DrawingObjectShip.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setShip.SortSet(comparer); + } } } diff --git a/ContainerShip/ContainerShip/SetShipGeneric.cs b/ContainerShip/ContainerShip/SetShipGeneric.cs index bbba05b..cc2fef9 100644 --- a/ContainerShip/ContainerShip/SetShipGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ContainerShip { internal class SetShipGeneric - where T : class + where T : class, IEquatable { private readonly List _places; @@ -28,6 +28,10 @@ namespace ContainerShip public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } if (position < 0 || position > Count || Count == _maxCount) { throw new StorageOverflowException(_maxCount); @@ -80,5 +84,13 @@ namespace ContainerShip } } } + public void SortSet(IComparer comparer) + { + if(comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/ContainerShip/ContainerShip/ShipCompareByColor.cs b/ContainerShip/ContainerShip/ShipCompareByColor.cs new file mode 100644 index 0000000..bec65e2 --- /dev/null +++ b/ContainerShip/ContainerShip/ShipCompareByColor.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class ShipCompareByColor : 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 xShip = x as DrawingObjectShip; + var yShip = y as DrawingObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + string xShipColor = xShip.GetShip.Ship.BodyColor.Name; + string yShipColor = yShip.GetShip.Ship.BodyColor.Name; + if (xShipColor != yShipColor) + { + return xShipColor.CompareTo(yShipColor); + } + if (xShip.GetShip.Ship is EntityContainerShip xContainerShip && yShip.GetShip.Ship is EntityContainerShip yContainerShip) + { + string xShipDopColor = xContainerShip.DopColor.Name; + string yShipDopColor = yContainerShip.DopColor.Name; + var dopColorCompare = xShipDopColor.CompareTo(yShipDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + } + } +} diff --git a/ContainerShip/ContainerShip/ShipCompareByType.cs b/ContainerShip/ContainerShip/ShipCompareByType.cs new file mode 100644 index 0000000..a94bec6 --- /dev/null +++ b/ContainerShip/ContainerShip/ShipCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class ShipCompareByType : 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 xShip = x as DrawingObjectShip; + var yShip = y as DrawingObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name) + { + if (xShip.GetShip.GetType().Name == "DrawingShip") + { + return -1; + } + return 1; + } + var speedCompare = + xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + } + } +}