Kashin M.I. Lab work 8 #8

Closed
Sosees04ka wants to merge 6 commits from LabWork08 into LabWork07
10 changed files with 269 additions and 90 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace GasolineTanker
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawningObject = null;
protected int[,] _map = null;
@ -143,6 +143,33 @@ namespace GasolineTanker
return bmp;
}
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;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);

View File

@ -15,6 +15,8 @@ namespace GasolineTanker
_gasolineTanker = gasolineTanker;
}
public DrawingGasolineTanker GetGasolineTanker => _gasolineTanker;
public float Step => _gasolineTanker?.GasolineTanker?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
@ -38,5 +40,31 @@ namespace GasolineTanker
}
public string GetInfo() => _gasolineTanker?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectGasolineTanker(data.CreateDrawingGasolineTanker());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectGasolineTanker otherGasolineTanker)
{
return false;
}
var entity = _gasolineTanker.GasolineTanker;
var otherEntity = otherGasolineTanker._gasolineTanker.GasolineTanker;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityImprovedGasolineTanker entityImprovedGasolineTanker &&
otherEntity is EntityImprovedGasolineTanker otherEntityWarmlyGasolineTanker && (
entityImprovedGasolineTanker.BodyColor != otherEntityWarmlyGasolineTanker.BodyColor ||
entityImprovedGasolineTanker.DopColor != otherEntityWarmlyGasolineTanker.DopColor ||
entityImprovedGasolineTanker.BodyKit != otherEntityWarmlyGasolineTanker.BodyKit))
{
return false;
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.keyRight);
@ -70,13 +74,35 @@
this.groupBoxTools.Controls.Add(this.buttonRemoveGasolineTanker);
this.groupBoxTools.Controls.Add(this.buttonAddGasolineTanker);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(694, 24);
this.groupBoxTools.Location = new System.Drawing.Point(793, 30);
this.groupBoxTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 631);
this.groupBoxTools.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBoxTools.Size = new System.Drawing.Size(229, 924);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(16, 504);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(198, 29);
this.buttonSortByColor.TabIndex = 15;
this.buttonSortByColor.Text = "Sort by color";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(16, 457);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(198, 29);
this.buttonSortByType.TabIndex = 14;
this.buttonSortByType.Text = "Sort by type";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
@ -84,18 +110,21 @@
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 22);
this.groupBoxMaps.Location = new System.Drawing.Point(7, 29);
this.groupBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(190, 305);
this.groupBoxMaps.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBoxMaps.Size = new System.Drawing.Size(217, 407);
this.groupBoxMaps.TabIndex = 13;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Maps";
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(6, 241);
this.buttonDeleteMap.Location = new System.Drawing.Point(7, 321);
this.buttonDeleteMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(175, 49);
this.buttonDeleteMap.Size = new System.Drawing.Size(200, 65);
this.buttonDeleteMap.TabIndex = 16;
this.buttonDeleteMap.Text = "Delete Map";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
@ -104,18 +133,20 @@
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(6, 141);
this.listBoxMaps.ItemHeight = 20;
this.listBoxMaps.Location = new System.Drawing.Point(7, 188);
this.listBoxMaps.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(176, 94);
this.listBoxMaps.Size = new System.Drawing.Size(201, 124);
this.listBoxMaps.TabIndex = 15;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged);
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(6, 84);
this.buttonAddMap.Location = new System.Drawing.Point(7, 112);
this.buttonAddMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(176, 51);
this.buttonAddMap.Size = new System.Drawing.Size(201, 68);
this.buttonAddMap.TabIndex = 14;
this.buttonAddMap.Text = "Add Map";
this.buttonAddMap.UseVisualStyleBackColor = true;
@ -123,30 +154,26 @@
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 24);
this.textBoxNewMapName.Location = new System.Drawing.Point(4, 68);
this.textBoxNewMapName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(178, 23);
this.textBoxNewMapName.Size = new System.Drawing.Size(203, 27);
this.textBoxNewMapName.TabIndex = 13;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Simple map",
"Long map"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 53);
this.comboBoxSelectorMap.Location = new System.Drawing.Point(4, 33);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(178, 23);
this.comboBoxSelectorMap.TabIndex = 12;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
this.comboBoxSelectorMap.Size = new System.Drawing.Size(204, 28);
this.comboBoxSelectorMap.TabIndex = 17;
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(10, 391);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 611);
this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(184, 23);
this.maskedTextBoxPosition.Size = new System.Drawing.Size(210, 27);
this.maskedTextBoxPosition.TabIndex = 11;
//
// keyRight
@ -154,9 +181,10 @@
this.keyRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyRight.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyRight;
this.keyRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyRight.Location = new System.Drawing.Point(122, 589);
this.keyRight.Location = new System.Drawing.Point(139, 868);
this.keyRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyRight.Name = "keyRight";
this.keyRight.Size = new System.Drawing.Size(30, 30);
this.keyRight.Size = new System.Drawing.Size(34, 40);
this.keyRight.TabIndex = 10;
this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
@ -166,9 +194,10 @@
this.keyLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyLeft;
this.keyLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyLeft.Location = new System.Drawing.Point(50, 589);
this.keyLeft.Location = new System.Drawing.Point(57, 868);
this.keyLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyLeft.Name = "keyLeft";
this.keyLeft.Size = new System.Drawing.Size(30, 30);
this.keyLeft.Size = new System.Drawing.Size(34, 40);
this.keyLeft.TabIndex = 9;
this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
@ -178,9 +207,10 @@
this.keyUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyUp.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyUp;
this.keyUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyUp.Location = new System.Drawing.Point(86, 553);
this.keyUp.Location = new System.Drawing.Point(98, 820);
this.keyUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyUp.Name = "keyUp";
this.keyUp.Size = new System.Drawing.Size(30, 30);
this.keyUp.Size = new System.Drawing.Size(34, 40);
this.keyUp.TabIndex = 8;
this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
@ -190,18 +220,20 @@
this.keyDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.keyDown.BackgroundImage = global::GasolineTanker.Properties.Resources.KeyDown;
this.keyDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.keyDown.Location = new System.Drawing.Point(86, 589);
this.keyDown.Location = new System.Drawing.Point(98, 868);
this.keyDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.keyDown.Name = "keyDown";
this.keyDown.Size = new System.Drawing.Size(30, 30);
this.keyDown.Size = new System.Drawing.Size(34, 40);
this.keyDown.TabIndex = 7;
this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(8, 500);
this.buttonShowOnMap.Location = new System.Drawing.Point(14, 757);
this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(186, 34);
this.buttonShowOnMap.Size = new System.Drawing.Size(213, 45);
this.buttonShowOnMap.TabIndex = 4;
this.buttonShowOnMap.Text = "Show on map";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
@ -209,9 +241,10 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(8, 460);
this.buttonShowStorage.Location = new System.Drawing.Point(14, 703);
this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(186, 34);
this.buttonShowStorage.Size = new System.Drawing.Size(213, 45);
this.buttonShowStorage.TabIndex = 3;
this.buttonShowStorage.Text = "Show storage";
this.buttonShowStorage.UseVisualStyleBackColor = true;
@ -219,9 +252,10 @@
//
// buttonRemoveGasolineTanker
//
this.buttonRemoveGasolineTanker.Location = new System.Drawing.Point(8, 420);
this.buttonRemoveGasolineTanker.Location = new System.Drawing.Point(14, 650);
this.buttonRemoveGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonRemoveGasolineTanker.Name = "buttonRemoveGasolineTanker";
this.buttonRemoveGasolineTanker.Size = new System.Drawing.Size(186, 34);
this.buttonRemoveGasolineTanker.Size = new System.Drawing.Size(213, 45);
this.buttonRemoveGasolineTanker.TabIndex = 2;
this.buttonRemoveGasolineTanker.Text = "Remove gasoline tanker";
this.buttonRemoveGasolineTanker.UseVisualStyleBackColor = true;
@ -229,9 +263,10 @@
//
// buttonAddGasolineTanker
//
this.buttonAddGasolineTanker.Location = new System.Drawing.Point(10, 351);
this.buttonAddGasolineTanker.Location = new System.Drawing.Point(16, 558);
this.buttonAddGasolineTanker.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddGasolineTanker.Name = "buttonAddGasolineTanker";
this.buttonAddGasolineTanker.Size = new System.Drawing.Size(186, 34);
this.buttonAddGasolineTanker.Size = new System.Drawing.Size(213, 45);
this.buttonAddGasolineTanker.TabIndex = 1;
this.buttonAddGasolineTanker.Text = "Add gasoline tanker";
this.buttonAddGasolineTanker.UseVisualStyleBackColor = true;
@ -240,19 +275,22 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Location = new System.Drawing.Point(0, 30);
this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(694, 631);
this.pictureBox.Size = new System.Drawing.Size(793, 924);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// menuStrip
//
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(894, 24);
this.menuStrip.Padding = new System.Windows.Forms.Padding(7, 3, 0, 3);
this.menuStrip.Size = new System.Drawing.Size(1022, 30);
this.menuStrip.TabIndex = 2;
//
// fileToolStripMenuItem
@ -261,20 +299,20 @@
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24);
this.fileToolStripMenuItem.Text = "File";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(125, 26);
this.SaveToolStripMenuItem.Text = "Save";
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(125, 26);
this.LoadToolStripMenuItem.Text = "Load";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -288,13 +326,14 @@
//
// FormMapWithSetGasolineTanker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(894, 655);
this.ClientSize = new System.Drawing.Size(1022, 954);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormMapWithSetGasolineTanker";
this.Text = "FormMapWithSetGasolineTanker";
this.groupBoxTools.ResumeLayout(false);
@ -334,5 +373,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -17,7 +17,6 @@ namespace GasolineTanker
{ "Simple map", new SimpleMap() },
{ "Long map", new LongMap() }
};
private MapWithSetGasolienTankerGeneric<DrawingObjectGasolineTanker, AbstractMap> _mapGasolineTankerCollectionGeneric;
private readonly MapsCollection _mapsCollection;
private readonly ILogger _logger;
public FormMapWithSetGasolineTanker(ILogger<FormMapWithSetGasolineTanker> logger)
@ -106,6 +105,7 @@ namespace GasolineTanker
_logger.LogWarning($"Storage full error: {ex.Message}");
MessageBox.Show($"Storage full error: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)
@ -114,7 +114,7 @@ namespace GasolineTanker
{
return;
}
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
@ -148,30 +148,9 @@ namespace GasolineTanker
dir = Direction.Right;
break;
}
pictureBox.Image = _mapGasolineTankerCollectionGeneric.MoveObject(dir);
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Simple map":
map = new SimpleMap();
break;
case "Long map":
map = new LongMap();
break;
}
if (map != null)
{
_mapGasolineTankerCollectionGeneric = new MapWithSetGasolienTankerGeneric<DrawingObjectGasolineTanker, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapGasolineTankerCollectionGeneric = null;
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void buttonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
@ -287,5 +266,18 @@ namespace GasolineTanker
}
}
}
private void SortBy(IComparer<IDrawingObject> comparer)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void buttonSortByType_Click(object sender, EventArgs e) => SortBy(new GasolineTankerCompareByType());
private void buttonSortByColor_Click(object sender, EventArgs e) => SortBy(new GasolineTankerCompareByColor());
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class GasolineTankerCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xGasolineTanker = x as DrawingObjectGasolineTanker;
var yGasolineTanker = y as DrawingObjectGasolineTanker;
if (xGasolineTanker == yGasolineTanker)
{
return 0;
}
if (xGasolineTanker == null)
{
return 1;
}
if (yGasolineTanker == null)
{
return -1;
}
var xEntity = xGasolineTanker.GetGasolineTanker.GasolineTanker;
var yEntity = yGasolineTanker.GetGasolineTanker.GasolineTanker;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntityImprovedGasolineTanker xEntityImprovedGasolineTanker ||
yEntity is not EntityImprovedGasolineTanker yEntityImprovedGasolineTanker)
{
return colorWeight;
}
return xEntityImprovedGasolineTanker.DopColor.ToArgb().CompareTo(yEntityImprovedGasolineTanker.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasolineTanker
{
internal class GasolineTankerCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xGasolineTanker = x as DrawingObjectGasolineTanker;
var yGasolineTanker = y as DrawingObjectGasolineTanker;
if (xGasolineTanker == yGasolineTanker)
{
return 0;
}
if (xGasolineTanker == null)
{
return 1;
}
if (yGasolineTanker == null)
{
return -1;
}
if (xGasolineTanker.GetGasolineTanker.GetType().Name != yGasolineTanker.GetGasolineTanker.GetType().Name)
{
if (xGasolineTanker.GetGasolineTanker.GetType() == typeof(DrawingGasolineTanker))
{
return -1;
}
return 1;
}
var speedCompare = xGasolineTanker.GetGasolineTanker.GasolineTanker.Speed.CompareTo(yGasolineTanker.GetGasolineTanker.GasolineTanker.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xGasolineTanker.GetGasolineTanker.GasolineTanker.Weight.CompareTo(yGasolineTanker.GetGasolineTanker.GasolineTanker.Weight);
}
}
}

View File

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

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace GasolineTanker
{
internal class MapWithSetGasolienTankerGeneric<T, U>
where T : class, IDrawingObject
where T : class, IEquatable<T>, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -134,5 +134,9 @@ namespace GasolineTanker
i++;
}
}
public void Sort(IComparer<T> comparer)
{
_setGasolineTanker.SortSet(comparer);
}
}
}

View File

@ -30,7 +30,7 @@ namespace GasolineTanker
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "C:\\Users\\kashi\\Desktop\\RPP\\GasolineTankerBase\\Base\\GasolineTanker\\GasolineTanker\\appSetting.json", optional: false, reloadOnChange: true)
.AddJsonFile(path: "C:\\Users\\kashi\\Desktop\\RPP\\GasolineTanker\\GasolineTanker\\GasolineTanker\\appSetting.json", optional: false, reloadOnChange: true)
.Build();
var logger = new LoggerConfiguration()

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace GasolineTanker
{
internal class SetGasolineTankerGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -19,15 +19,7 @@ namespace GasolineTanker
}
public int Insert(T gasolineTanker)
{
if (_places.Count < _maxCount)
{
_places.Add(gasolineTanker);
for (int i = 0; i < _places.Count; i++)
{
if (_places[i] == gasolineTanker) return i;
}
}
return -1;
return Insert(gasolineTanker, 0);
}
private bool isCorrectPosition(int position)
{
@ -37,11 +29,15 @@ namespace GasolineTanker
{
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
{
if (position < 0 || position > Count)
return -1;
}
if (_places.Contains(gasolineTanker))
return -1;
_places.Insert(position, gasolineTanker);
return position;
}
public T Remove(int position)
@ -81,5 +77,13 @@ namespace GasolineTanker
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}