Bondarenko M.S. Lab Work 8 #15

Closed
maxnes3 wants to merge 4 commits from LabWork_08 into LabWork_07
10 changed files with 268 additions and 13 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
@ -117,5 +117,26 @@ namespace WarmlyShip
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;
}
}
}

View File

@ -17,6 +17,8 @@ namespace WarmlyShip
public float Step => _warmlyShip?.warmlyShip?.Step ?? 0;
public DrawingWarmlyShip GetWarmlyShip => _warmlyShip;
public void DrawningObject(Graphics g)
{
_warmlyShip?.DrawTransport(g);
@ -40,5 +42,55 @@ namespace WarmlyShip
public string GetInfo() => _warmlyShip?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherShip = other as DrawningObjectShip;
if (otherShip == null)
{
return false;
}
var ship = _warmlyShip.warmlyShip;
var otherShipShip = otherShip._warmlyShip.warmlyShip;
if (ship.Speed != otherShipShip.Speed)
{
return false;
}
if (ship.Weight != otherShipShip.Weight)
{
return false;
}
if (ship.BodyColor != otherShipShip.BodyColor)
{
return false;
}
if ((ship is EntityMotorShip) && !(otherShipShip is EntityMotorShip)
|| !(ship is EntityMotorShip) && (otherShipShip is EntityMotorShip))
{
return false;
}
if (ship is EntityMotorShip motorShip && otherShipShip is EntityMotorShip otherMotorShip)
{
if (motorShip.DopColor != otherMotorShip.DopColor)
{
return false;
}
if (motorShip.Tubes != otherMotorShip.Tubes)
{
return false;
}
if (motorShip.Cistern != otherMotorShip.Cistern)
{
return false;
}
}
return true;
}
}
}

View File

@ -17,8 +17,8 @@ namespace WarmlyShip
public EntityWarmlyShip(int speed, float weight, Color bodyColor)
{
Random random = new Random();
Speed = speed <= 0 ? random.Next(100, 300) : speed;
Weight = weight <= 0 ? random.Next(1000, 2000) : weight;
Speed = speed <= 0 ? 100 : speed;
Weight = weight <= 0 ? 1000 : weight;
BodyColor = bodyColor;
}
}

View File

@ -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.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonSortByColor);
this.groupBox1.Controls.Add(this.buttonSortByType);
this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonDown);
@ -142,7 +146,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 345);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 375);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
@ -198,7 +202,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 432);
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 462);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23);
this.buttonShowOnMap.TabIndex = 5;
@ -208,7 +212,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 403);
this.buttonShowStorage.Location = new System.Drawing.Point(6, 433);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(188, 23);
this.buttonShowStorage.TabIndex = 4;
@ -218,7 +222,7 @@
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 374);
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 404);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23);
this.buttonRemoveShip.TabIndex = 3;
@ -228,7 +232,7 @@
//
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(6, 316);
this.buttonAddShip.Location = new System.Drawing.Point(6, 346);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(188, 23);
this.buttonAddShip.TabIndex = 1;
@ -266,14 +270,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);
//
@ -285,6 +289,26 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(6, 272);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(188, 23);
this.buttonSortByType.TabIndex = 13;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(6, 301);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(188, 23);
this.buttonSortByColor.TabIndex = 14;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetShip
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -333,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -113,6 +113,7 @@ namespace WarmlyShip
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Добавлен объект: {ship} на карте: {listBoxMaps.SelectedItem}");
}
else MessageBox.Show("Объект не добавлен");
}
catch (StorageOverflowException ex)
{
@ -250,5 +251,25 @@ namespace WarmlyShip
}
}
}
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();
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal class MapWithSetShipGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -83,6 +83,11 @@ namespace WarmlyShip
}
}
public void Sort(IComparer<T> comparer)
{
_setShips.SortSet(comparer);
}
private void Shaking()
{
int j = _setShips.Count - 1;

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal class SetShipGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -27,6 +27,10 @@ namespace WarmlyShip
public int Insert(T ship, int position)
{
if (_places.Contains(ship))
{
return -1;
}
if (position < 0 || position > Count || Count == _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, ship);
return position;
@ -69,5 +73,13 @@ namespace WarmlyShip
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class ShipCompareByColor : IComparer<IDrawningObject>
{
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 DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
string xAirplaneColor = xShip.GetWarmlyShip.warmlyShip.BodyColor.Name;
string yAirplaneColor = yShip.GetWarmlyShip.warmlyShip.BodyColor.Name;
if (xAirplaneColor != yAirplaneColor)
{
return xAirplaneColor.CompareTo(yAirplaneColor);
}
if (xShip.GetWarmlyShip.warmlyShip is EntityMotorShip xAirbus && yShip.GetWarmlyShip.warmlyShip is EntityMotorShip yAirbus)
{
string xAirplaneDopColor = xAirbus.DopColor.Name;
string yAirplaneDopColor = yAirbus.DopColor.Name;
var dopColorCompare = xAirplaneDopColor.CompareTo(yAirplaneDopColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class ShipCompareByType : IComparer<IDrawningObject>
{
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 DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
if (xShip.GetWarmlyShip.GetType().Name != yShip.GetWarmlyShip.GetType().Name)
{
if (xShip.GetWarmlyShip.GetType().Name == "DrawingWarmlyShip")
{
return -1;
}
return 1;
}
var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight);
}
}
}