Compare commits

...

2 Commits

Author SHA1 Message Date
evasina2312@gmail.com
05d0e55103 Лабараторная 8 2022-12-21 15:34:43 +04:00
evasina2312@gmail.com
4246af5f48 лаб 8 2022-12-21 12:08:03 +04:00
7 changed files with 200 additions and 16 deletions

View File

@ -8,7 +8,7 @@ namespace ProjectMachine
{ {
internal class DrawningObject : IDrawningObject internal class DrawningObject : IDrawningObject
{ {
private DrawningMachine _machine = null; public DrawningMachine _machine = null;
public DrawningObject(DrawningMachine machine) public DrawningObject(DrawningMachine machine)
{ {
@ -17,6 +17,8 @@ namespace ProjectMachine
public float Step => _machine?.Machine?.Step ?? 0; public float Step => _machine?.Machine?.Step ?? 0;
public DrawningMachine GetTank => _machine;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{ {
return _machine?.GetCurrentPosition() ?? default; return _machine?.GetCurrentPosition() ?? default;

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout(); this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout(); this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonUp);
@ -70,9 +74,9 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddTank); this.groupBoxTools.Controls.Add(this.buttonAddTank);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(799, 28); this.groupBoxTools.Location = new System.Drawing.Point(812, 28);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(233, 758); this.groupBoxTools.Size = new System.Drawing.Size(233, 843);
this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; this.groupBoxTools.Text = "Инструменты";
@ -150,7 +154,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ProjectMachine.Properties.Resources.лево; this.buttonLeft.BackgroundImage = global::ProjectMachine.Properties.Resources.лево;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(69, 703); this.buttonLeft.Location = new System.Drawing.Point(69, 788);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(34, 30); this.buttonLeft.Size = new System.Drawing.Size(34, 30);
this.buttonLeft.TabIndex = 13; this.buttonLeft.TabIndex = 13;
@ -163,7 +167,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ProjectMachine.Properties.Resources.вверх; this.buttonUp.BackgroundImage = global::ProjectMachine.Properties.Resources.вверх;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(105, 667); this.buttonUp.Location = new System.Drawing.Point(105, 752);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(34, 30); this.buttonUp.Size = new System.Drawing.Size(34, 30);
this.buttonUp.TabIndex = 12; this.buttonUp.TabIndex = 12;
@ -176,7 +180,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectMachine.Properties.Resources.право; this.buttonRight.BackgroundImage = global::ProjectMachine.Properties.Resources.право;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(141, 703); this.buttonRight.Location = new System.Drawing.Point(141, 788);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(34, 30); this.buttonRight.Size = new System.Drawing.Size(34, 30);
this.buttonRight.TabIndex = 11; this.buttonRight.TabIndex = 11;
@ -189,7 +193,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ProjectMachine.Properties.Resources.вниз; this.buttonDown.BackgroundImage = global::ProjectMachine.Properties.Resources.вниз;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(105, 703); this.buttonDown.Location = new System.Drawing.Point(105, 788);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(34, 30); this.buttonDown.Size = new System.Drawing.Size(34, 30);
this.buttonDown.TabIndex = 10; this.buttonDown.TabIndex = 10;
@ -199,7 +203,7 @@
// //
// buttonShowOnMap // buttonShowOnMap
// //
this.buttonShowOnMap.Location = new System.Drawing.Point(21, 611); this.buttonShowOnMap.Location = new System.Drawing.Point(20, 689);
this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47); this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47);
@ -210,7 +214,7 @@
// //
// buttonShowStorage // buttonShowStorage
// //
this.buttonShowStorage.Location = new System.Drawing.Point(21, 556); this.buttonShowStorage.Location = new System.Drawing.Point(20, 634);
this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(200, 47); this.buttonShowStorage.Size = new System.Drawing.Size(200, 47);
@ -221,7 +225,7 @@
// //
// buttonRemoveTank // buttonRemoveTank
// //
this.buttonRemoveTank.Location = new System.Drawing.Point(21, 501); this.buttonRemoveTank.Location = new System.Drawing.Point(20, 579);
this.buttonRemoveTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonRemoveTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonRemoveTank.Name = "buttonRemoveTank"; this.buttonRemoveTank.Name = "buttonRemoveTank";
this.buttonRemoveTank.Size = new System.Drawing.Size(200, 47); this.buttonRemoveTank.Size = new System.Drawing.Size(200, 47);
@ -232,7 +236,7 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
this.maskedTextBoxPosition.Location = new System.Drawing.Point(21, 466); this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 544);
this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
@ -242,7 +246,7 @@
// //
// buttonAddTank // buttonAddTank
// //
this.buttonAddTank.Location = new System.Drawing.Point(21, 411); this.buttonAddTank.Location = new System.Drawing.Point(20, 489);
this.buttonAddTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddTank.Name = "buttonAddTank"; this.buttonAddTank.Name = "buttonAddTank";
this.buttonAddTank.Size = new System.Drawing.Size(200, 47); this.buttonAddTank.Size = new System.Drawing.Size(200, 47);
@ -256,7 +260,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox"; this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(799, 758); this.pictureBox.Size = new System.Drawing.Size(812, 843);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@ -267,7 +271,7 @@
this.файлToolStripMenuItem}); this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip"; this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(1032, 28); this.menuStrip.Size = new System.Drawing.Size(1045, 28);
this.menuStrip.TabIndex = 2; this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "menuStrip1"; this.menuStrip.Text = "menuStrip1";
// //
@ -302,11 +306,33 @@
// //
this.saveFileDialog.Filter = "txt file | *.txt"; this.saveFileDialog.Filter = "txt file | *.txt";
// //
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(20, 366);
this.buttonSortByType.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(200, 47);
this.buttonSortByType.TabIndex = 15;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(22, 421);
this.buttonSortByColor.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(200, 47);
this.buttonSortByColor.TabIndex = 16;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// FormMapWithSetTank // FormMapWithSetTank
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1032, 786); this.ClientSize = new System.Drawing.Size(1045, 871);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip); this.Controls.Add(this.menuStrip);
@ -350,5 +376,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@ -146,6 +146,11 @@ namespace ProjectMachine
_logger.Warning("Ошибка переполнения хранилища: {0}", ex.Message); _logger.Warning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
catch (Exception ex)
{
_logger.Warning("Такой объект уже есть:");
MessageBox.Show($"Такой объект уже есть:", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
/// <summary> /// <summary>
/// Удаление объекта /// Удаление объекта
@ -294,6 +299,34 @@ namespace ProjectMachine
} }
} }
} }
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TankCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TankCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
} }
} }

View File

@ -139,6 +139,14 @@ namespace ProjectMachine
} }
} }
/// <summary> /// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setTank.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале /// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary> /// </summary>
private void Shaking() private void Shaking()

View File

@ -51,7 +51,7 @@ namespace ProjectMachine
public int Insert(T tank, int position) public int Insert(T tank, int position)
{ {
if (_places.Contains(tank)) if (_places.Contains(tank))
throw new ArgumentException($"Объект {tank} уже есть"); throw new ArgumentException($"Такой объект уже есть");
if (Count >= _maxCount) if (Count >= _maxCount)
throw new StorageOverflowException(Count); throw new StorageOverflowException(Count);
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
@ -112,6 +112,18 @@ namespace ProjectMachine
} }
} }
} }
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
} }
} }

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMachine
{
internal class TankCompareByColor : 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 xTank = x as DrawningObject;
var yTank = y as DrawningObject;
if (xTank == null && yTank == null)
{
return 0;
}
if (xTank == null && yTank != null)
{
return 1;
}
if (xTank != null && yTank == null)
{
return -1;
}
var xColorName = xTank._machine.Machine.BodyColor.Name;
var yColorName = yTank._machine.Machine.BodyColor.Name;
return xColorName.CompareTo(yColorName);
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMachine
{
internal class TankCompareByType : 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 xTank = x as DrawningObject;
var yTank = y as DrawningObject;
if (xTank == null && yTank == null)
{
return 0;
}
if (xTank == null && yTank != null)
{
return 1;
}
if (xTank != null && yTank == null)
{
return -1;
}
if (xTank.GetTank.GetType().Name != yTank.GetTank.GetType().Name)
{
if (xTank.GetTank.GetType().Name == "DrawningMachine")
{
return -1;
}
return 1;
}
var speedCompare = xTank.GetTank.Machine.Speed.CompareTo(yTank.GetTank.Machine.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xTank.GetTank.Machine.Weight.CompareTo(yTank.GetTank.Machine.Weight);
}
}
}