PIbd-21 Potapov N.S. LabWork08 #8
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
public class DrawingPlaneEqutables : IEqualityComparer<DrawingPlane>
|
||||
{
|
||||
public bool Equals(DrawingPlane? x, DrawingPlane? y)
|
||||
{
|
||||
if (x == null || x.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityPlane.Speed != y.EntityPlane.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityPlane.Weight != y.EntityPlane.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x is DrawingStormtrooper && y is DrawingStormtrooper)
|
||||
{
|
||||
var xStormtrooper = (x.EntityPlane as EntityStormtrooper);
|
||||
var yStormtrooper = (y.EntityPlane as EntityStormtrooper);
|
||||
if (xStormtrooper?.AdditionalColor != yStormtrooper?.AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (xStormtrooper?.Bombs != yStormtrooper?.Bombs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (xStormtrooper?.Rockets != yStormtrooper?.Rockets)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetHashCode([DisallowNull] DrawingPlane obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -29,6 +29,8 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
groupBoxTools = new GroupBox();
|
||||
buttonSortByType = new Button();
|
||||
buttonSortByColor = new Button();
|
||||
groupBoxStorages = new GroupBox();
|
||||
buttonRemoveStorage = new Button();
|
||||
listBoxStorages = new ListBox();
|
||||
@ -53,6 +55,8 @@
|
||||
//
|
||||
// groupBoxTools
|
||||
//
|
||||
groupBoxTools.Controls.Add(buttonSortByType);
|
||||
groupBoxTools.Controls.Add(buttonSortByColor);
|
||||
groupBoxTools.Controls.Add(groupBoxStorages);
|
||||
groupBoxTools.Controls.Add(maskedTextBoxNumber);
|
||||
groupBoxTools.Controls.Add(buttonRefreshCollection);
|
||||
@ -66,6 +70,26 @@
|
||||
groupBoxTools.TabStop = false;
|
||||
groupBoxTools.Text = "Инструменты";
|
||||
//
|
||||
// buttonSortByType
|
||||
//
|
||||
buttonSortByType.Location = new Point(6, 273);
|
||||
buttonSortByType.Name = "buttonSortByType";
|
||||
buttonSortByType.Size = new Size(218, 29);
|
||||
buttonSortByType.TabIndex = 7;
|
||||
buttonSortByType.Text = "Сортировка по типу";
|
||||
buttonSortByType.UseVisualStyleBackColor = true;
|
||||
buttonSortByType.Click += buttonSortByType_Click;
|
||||
//
|
||||
// buttonSortByColor
|
||||
//
|
||||
buttonSortByColor.Location = new Point(6, 305);
|
||||
buttonSortByColor.Name = "buttonSortByColor";
|
||||
buttonSortByColor.Size = new Size(218, 29);
|
||||
buttonSortByColor.TabIndex = 6;
|
||||
buttonSortByColor.Text = "Сортировка по цвету";
|
||||
buttonSortByColor.UseVisualStyleBackColor = true;
|
||||
buttonSortByColor.Click += buttonSortByColor_Click;
|
||||
//
|
||||
// groupBoxStorages
|
||||
//
|
||||
groupBoxStorages.Controls.Add(buttonRemoveStorage);
|
||||
@ -246,5 +270,7 @@
|
||||
private ToolStripMenuItem загрузитьToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private Button buttonSortByType;
|
||||
private Button buttonSortByColor;
|
||||
}
|
||||
}
|
@ -140,9 +140,14 @@ namespace ProjectStormtrooper
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
MessageBox.Show(ex.Message, "Ошибка добавления", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogWarning("Ошибка добавления: " + ex.Message);
|
||||
}
|
||||
}
|
||||
catch (ApplicationException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка добавления", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogWarning("Ошибка добавления: " + ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора
|
||||
@ -246,5 +251,26 @@ namespace ProjectStormtrooper
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Сортировка по сравнителю
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
private void ComparePlanes(IComparer<DrawingPlane?> comparer)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
obj.Sort(comparer);
|
||||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||||
}
|
||||
private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType());
|
||||
|
||||
private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
public class PlaneCompareByColor : IComparer<DrawingPlane?>
|
||||
{
|
||||
public int Compare(DrawingPlane? x, DrawingPlane? y)
|
||||
|
||||
{
|
||||
if (x == null || x.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor)
|
||||
{
|
||||
return x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name);
|
||||
}
|
||||
if (x.EntityPlane is EntityStormtrooper xStormtrooper && y.EntityPlane is EntityStormtrooper yStormtrooper)
|
||||
{
|
||||
if (xStormtrooper.AdditionalColor != yStormtrooper.AdditionalColor)
|
||||
{
|
||||
return xStormtrooper.AdditionalColor.Name.CompareTo(yStormtrooper.AdditionalColor.Name);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
public class PlaneCompareByType : IComparer<DrawingPlane?>
|
||||
{
|
||||
public int Compare(DrawingPlane? x, DrawingPlane? y)
|
||||
{
|
||||
if (x == null || x.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityPlane == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return x.GetType().Name.CompareTo(y.GetType().Name);
|
||||
}
|
||||
var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
public class PlanesCollectionInfo : IEquatable<PlanesCollectionInfo>
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public PlanesCollectionInfo(string name, string description)
|
||||
{
|
||||
Name = name;
|
||||
Description = description;
|
||||
}
|
||||
public bool Equals(PlanesCollectionInfo? other)
|
||||
{
|
||||
return Name == other.Name;
|
||||
eegov
commented
Нет проверки, что other не равен null Нет проверки, что other не равен null
|
||||
}
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Name.GetHashCode();
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
@ -49,6 +49,11 @@ namespace ProjectStormtrooper
|
||||
_collection = new SetGeneric<T>(horizontalObjectsCount * verticalObjectsCount);
|
||||
}
|
||||
/// <summary>
|
||||
/// Сортировка
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
/// <summary>
|
||||
/// Перегрузка оператора сложения
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
@ -60,7 +65,7 @@ namespace ProjectStormtrooper
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return collect?._collection.Insert(obj) ?? -1;
|
||||
return collect?._collection.Insert(obj, new DrawingPlaneEqutables()) ?? -1;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ProjectStormtrooper
|
||||
{
|
||||
@ -23,11 +24,11 @@ namespace ProjectStormtrooper
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
|
||||
readonly Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _planeStorages.Keys.ToList();
|
||||
public List<PlanesCollectionInfo> Keys => _planeStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
@ -43,7 +44,7 @@ namespace ProjectStormtrooper
|
||||
/// <param name="pictureHeight"></param>
|
||||
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_planeStorages = new Dictionary<string, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>>();
|
||||
_planeStorages = new Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -53,7 +54,7 @@ namespace ProjectStormtrooper
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
_planeStorages.Add(name, new PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>(_pictureWidth, _pictureHeight));
|
||||
_planeStorages.Add(new PlanesCollectionInfo(name, ""), new PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
@ -61,8 +62,9 @@ namespace ProjectStormtrooper
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
if (_planeStorages.ContainsKey(name))
|
||||
_planeStorages.Remove(name);
|
||||
var info = new PlanesCollectionInfo(name, "");
|
||||
if (_planeStorages.ContainsKey(info))
|
||||
_planeStorages.Remove(info);
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
@ -73,8 +75,9 @@ namespace ProjectStormtrooper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_planeStorages.ContainsKey(ind))
|
||||
return _planeStorages[ind];
|
||||
var info = new PlanesCollectionInfo(ind, "");
|
||||
if (_planeStorages.ContainsKey(info))
|
||||
return _planeStorages[info];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -104,7 +107,7 @@ namespace ProjectStormtrooper
|
||||
{
|
||||
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
||||
}
|
||||
sw.WriteLine($"{storage.Key}{_separatorForKeyValue}{storageString}");
|
||||
sw.WriteLine($"{storage.Key.Name}{_separatorForKeyValue}{storageString}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -152,7 +155,7 @@ namespace ProjectStormtrooper
|
||||
}
|
||||
}
|
||||
}
|
||||
_planeStorages.Add(record[0], collection);
|
||||
_planeStorages.Add(new PlanesCollectionInfo(record[0], ""), collection);
|
||||
currentLine = sr.ReadLine();
|
||||
}
|
||||
}
|
||||
|
@ -34,13 +34,18 @@ namespace ProjectStormtrooper
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
/// <summary>
|
||||
/// Сортировка набора объектов
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||||
/// <summary>
|
||||
/// Добавления объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="plane"></param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T plane)
|
||||
public int Insert(T plane, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
return Insert(plane, 0);
|
||||
return Insert(plane, 0, equal);
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
@ -48,7 +53,7 @@ namespace ProjectStormtrooper
|
||||
/// <param name="plane"></param>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T plane, int position)
|
||||
public int Insert(T plane, int position, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
{
|
||||
@ -59,6 +64,16 @@ namespace ProjectStormtrooper
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (equal != null)
|
||||
{
|
||||
foreach (var otherPlane in _places)
|
||||
{
|
||||
if (equal.Equals(otherPlane, plane))
|
||||
{
|
||||
throw new ApplicationException("Такой объект уже есть в коллекции!");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Вставка по позиции
|
||||
_places.Insert(position, plane);
|
||||
return position;
|
||||
|
Loading…
Reference in New Issue
Block a user
Требовалось сортировать по критериям: цвет, скорость, вес