LabWork8 Complete

This commit is contained in:
Павел Путилин 2022-11-23 22:47:28 +04:00
parent d5e461cf6b
commit f4091409de
8 changed files with 239 additions and 16 deletions

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace AirplaneWithRadar
{
internal class AirplaneCompareByColor : 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 xAirplane = x as DrawningObjectAirplane;
var yAirplane = y as DrawningObjectAirplane;
if (xAirplane == null && yAirplane == null)
{
return 0;
}
if (xAirplane == null && yAirplane != null)
{
return 1;
}
if (xAirplane != null && yAirplane == null)
{
return -1;
}
var color = xAirplane.GetAirplane.Airplane.BodyColor.ToArgb().CompareTo(yAirplane.GetAirplane.Airplane.BodyColor.ToArgb());
if (color != 0 ||
xAirplane.GetAirplane.Airplane is not EntityAirplaneWithRadar xEntityAirplaneWithRadar ||
yAirplane.GetAirplane.Airplane is not EntityAirplaneWithRadar yEntityAirplaneWithRadar)
{
return color;
}
return xEntityAirplaneWithRadar.DopColor.ToArgb().CompareTo(yEntityAirplaneWithRadar.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal class AirplaneCompareByType : 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 xAirplane = x as DrawningObjectAirplane;
var yAirplane = y as DrawningObjectAirplane;
if (xAirplane == null && yAirplane == null)
{
return 0;
}
if (xAirplane == null && yAirplane != null)
{
return 1;
}
if (xAirplane != null && yAirplane == null)
{
return -1;
}
if (xAirplane.GetAirplane.GetType().Name != yAirplane.GetAirplane.GetType().Name)
{
if (xAirplane.GetAirplane.GetType().Name == "DrawningAirplane")
{
return -1;
}
return 1;
}
var speedCompare = xAirplane.GetAirplane.Airplane.Speed.CompareTo(yAirplane.GetAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.GetAirplane.Airplane.Weight.CompareTo(yAirplane.GetAirplane.Airplane.Weight);
}
}
}

View File

@ -13,6 +13,9 @@ namespace AirplaneWithRadar
{
_airplane = airplane;
}
public DrawningAirplane GetAirplane => _airplane;
public float Step => _airplane?.Airplane?.Step ?? 0;
public (float Left, float Top, float Right, float Bottom)
GetCurrentPosition()
@ -35,5 +38,45 @@ namespace AirplaneWithRadar
public string GetInfo() => _airplane?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectAirplane(data.CreateDrawningAirplane());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherAirplane = other as DrawningObjectAirplane;
if (otherAirplane == null)
{
return false;
}
var airplane = _airplane.Airplane;
var otherAirplaneAirplane = otherAirplane._airplane.Airplane;
if (airplane.Speed != otherAirplaneAirplane.Speed)
{
return false;
}
if (airplane.Weight != otherAirplaneAirplane.Weight)
{
return false;
}
if (airplane.BodyColor != otherAirplaneAirplane.BodyColor)
{
return false;
}
if (airplane.GetType().Name != otherAirplaneAirplane.GetType().Name)
{
return false;
}
if (airplane is EntityAirplaneWithRadar entityAirplaneWithRadar &&
otherAirplaneAirplane is EntityAirplaneWithRadar otherAirplaneWithRadar && (
entityAirplaneWithRadar.Radar != otherAirplaneWithRadar.Radar ||
entityAirplaneWithRadar.DopColor != otherAirplaneWithRadar.DopColor ||
entityAirplaneWithRadar.FuelTanks != otherAirplaneWithRadar.FuelTanks))
{
return false;
}
return true;
}
}
}

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRight);
@ -72,7 +76,7 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(801, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 545);
this.groupBoxTools.Size = new System.Drawing.Size(200, 637);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -141,7 +145,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 337);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 420);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(174, 23);
@ -152,7 +156,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(121, 509);
this.buttonRight.Location = new System.Drawing.Point(121, 601);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 8;
@ -164,7 +168,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(49, 509);
this.buttonLeft.Location = new System.Drawing.Point(49, 601);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 7;
@ -176,7 +180,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(85, 509);
this.buttonDown.Location = new System.Drawing.Point(85, 601);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 6;
@ -188,7 +192,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(85, 473);
this.buttonUp.Location = new System.Drawing.Point(85, 565);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 5;
@ -197,7 +201,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(14, 440);
this.buttonShowOnMap.Location = new System.Drawing.Point(14, 523);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(174, 34);
this.buttonShowOnMap.TabIndex = 4;
@ -207,7 +211,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(14, 402);
this.buttonShowStorage.Location = new System.Drawing.Point(14, 485);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(174, 32);
this.buttonShowStorage.TabIndex = 3;
@ -217,7 +221,7 @@
//
// buttonRemoveAirplane
//
this.buttonRemoveAirplane.Location = new System.Drawing.Point(14, 366);
this.buttonRemoveAirplane.Location = new System.Drawing.Point(14, 449);
this.buttonRemoveAirplane.Name = "buttonRemoveAirplane";
this.buttonRemoveAirplane.Size = new System.Drawing.Size(174, 30);
this.buttonRemoveAirplane.TabIndex = 2;
@ -227,7 +231,7 @@
//
// buttonAddAirplane
//
this.buttonAddAirplane.Location = new System.Drawing.Point(14, 298);
this.buttonAddAirplane.Location = new System.Drawing.Point(14, 381);
this.buttonAddAirplane.Name = "buttonAddAirplane";
this.buttonAddAirplane.Size = new System.Drawing.Size(175, 33);
this.buttonAddAirplane.TabIndex = 1;
@ -240,7 +244,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(801, 545);
this.pictureBox.Size = new System.Drawing.Size(801, 637);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -285,11 +289,31 @@
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(13, 287);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 35);
this.buttonSortByType.TabIndex = 16;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(13, 328);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 35);
this.buttonSortByColor.TabIndex = 15;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetAirplanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1001, 569);
this.ClientSize = new System.Drawing.Size(1001, 661);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -333,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -154,6 +154,11 @@ namespace AirplaneWithRadar
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, airplane);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Удаление объекта
@ -296,5 +301,27 @@ namespace AirplaneWithRadar
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AirplaneCompareByType());
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 AirplaneCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -6,7 +6,7 @@
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetAirplanesGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -136,7 +136,14 @@
_setAirplanes.Insert(DrawningObjectAirplane.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setAirplanes.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>

View File

@ -5,7 +5,7 @@
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetAirplanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -48,6 +48,8 @@
/// <returns></returns>
public int Insert(T airplane, int position)
{
if (_places.Contains(airplane))
throw new ArgumentException($"Такой объект: {airplane}, уже есть в наборе");
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
@ -104,5 +106,17 @@
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}