Nugaev D.N. LabWork08 #11

Closed
Damir_Nugaev_ISEbd-22 wants to merge 4 commits from Lab_work08 into Lab_work07
10 changed files with 276 additions and 36 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -164,5 +164,30 @@ namespace Bus
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)
{
return false;
}
if (other == null)
{
return false;
}
if (_width != other._width) return false;
if (_height != other._height) return false;
if (_size_x != other._size_x) return false;
if (_size_y != other._size_y) 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

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class BusCompareByColor : IComparer<IDrawingObject>
{
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.Bus.BodyColor.R.CompareTo(xBus.GetBus.Bus.BodyColor.R) != 0)
{
return xBus.GetBus.Bus.BodyColor.R.CompareTo(yBus.GetBus.Bus.BodyColor.R);
}
if (xBus.GetBus.Bus.BodyColor.G.CompareTo(yBus.GetBus.Bus.BodyColor.G) != 0)
{
return xBus.GetBus.Bus.BodyColor.G.CompareTo(yBus.GetBus.Bus.BodyColor.G);
}
if (xBus.GetBus.Bus.BodyColor.B.CompareTo(yBus.GetBus.Bus.BodyColor.B) != 0)
{
return xBus.GetBus.Bus.BodyColor.B.CompareTo(yBus.GetBus.Bus.BodyColor.B);
}
if (xBus.GetBus.Bus is EntitySportBus xSportEntity && yBus.GetBus.Bus is EntitySportBus ySportEntity)
{
if (xSportEntity.DopColor.R.CompareTo(ySportEntity.DopColor.R) != 0)
{
return xSportEntity.DopColor.R.CompareTo(ySportEntity.DopColor.R);
}
if (xSportEntity.DopColor.G.CompareTo(ySportEntity.DopColor.G) != 0)
{
return xSportEntity.DopColor.G.CompareTo(ySportEntity.DopColor.G);
}
if (xSportEntity.DopColor.B.CompareTo(ySportEntity.DopColor.B) != 0)
{
return xSportEntity.DopColor.B.CompareTo(ySportEntity.DopColor.B);
}
}
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);
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class BusCompareByType : IComparer<IDrawingObject>
{
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 == "DrawningBus")
{
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);
}
}
}

View File

@ -31,6 +31,8 @@ namespace Bus
_bus.SetPosition(x, y, width, height);
}
public DrawingBus GetBus => _bus;
void IDrawingObject.DrawingObject(Graphics g)
{
_bus.DrawTransport(g);
@ -39,6 +41,31 @@ namespace Bus
public string GetInfo() => _bus?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBus(data.CreateDrawingBus());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectBus otherBus)
{
return false;
}
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;
}
if (entity is EntitySportBus entityWarmlyShip &&
otherEntity is EntitySportBus otherEntityWarmlyShip && (
entityWarmlyShip.Wing != otherEntityWarmlyShip.Wing ||
entityWarmlyShip.DopColor != otherEntityWarmlyShip.DopColor ||
entityWarmlyShip.Sportline != otherEntityWarmlyShip.Sportline))
{
return false;
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = 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.buttonSortByType);
this.groupBox1.Controls.Add(this.buttonSortByColor);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonLeft);
@ -76,6 +80,26 @@
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(15, 463);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(193, 29);
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(15, 428);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(193, 29);
this.buttonSortByColor.TabIndex = 12;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -126,7 +150,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 426);
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 393);
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, 358);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(193, 29);
this.buttonShowStorage.TabIndex = 4;
@ -146,16 +170,16 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 320);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 287);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(100, 27);
this.maskedTextBoxPosition.Size = new System.Drawing.Size(193, 27);
this.maskedTextBoxPosition.TabIndex = 11;
//
// buttonRemoveBus
//
this.buttonRemoveBus.Location = new System.Drawing.Point(15, 353);
this.buttonRemoveBus.Location = new System.Drawing.Point(15, 320);
this.buttonRemoveBus.Name = "buttonRemoveBus";
this.buttonRemoveBus.Size = new System.Drawing.Size(129, 32);
this.buttonRemoveBus.Size = new System.Drawing.Size(193, 32);
this.buttonRemoveBus.TabIndex = 2;
this.buttonRemoveBus.Text = "Удалить автобус";
this.buttonRemoveBus.UseVisualStyleBackColor = true;
@ -163,9 +187,9 @@
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(15, 281);
this.buttonAddBus.Location = new System.Drawing.Point(15, 248);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(129, 33);
this.buttonAddBus.Size = new System.Drawing.Size(193, 33);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автобус";
this.buttonAddBus.UseVisualStyleBackColor = true;
@ -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);
//
@ -333,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -13,8 +13,6 @@ namespace Bus
{
public partial class FormMapWithSetDoubleDeckerBus : Form
{
private MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> _mapBusCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{"Простая карта", new SimpleMap() },
@ -70,14 +68,6 @@ namespace Bus
map = new MyMap();
break;
}
if(map != null)
{
_mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBusCollectionGeneric = null;
}
}
private void ButtonAddBus_Click(object sender, EventArgs e)
@ -276,5 +266,25 @@ namespace Bus
}
}
}
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -66,4 +66,7 @@
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>311, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

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

View File

@ -7,8 +7,8 @@ using System.Threading.Tasks;
namespace Bus
{
internal class MapWithSetDoubleDeckerBusGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
where T : class, IEquatable<T>, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
@ -158,5 +158,10 @@ namespace Bus
_setBus.Insert(DrawingObjectBus.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setBus.SortSet(comparer);
}
}
}

View File

@ -7,7 +7,8 @@ using System.Threading.Tasks;
namespace Bus
{
internal class SetDoubleDeckerBusGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -25,21 +26,21 @@ namespace Bus
{
return Insert(bus, 0);
}
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
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;
}
@ -92,5 +93,14 @@ namespace Bus
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}