8 базовая лабораторная

This commit is contained in:
katana 2023-12-12 22:59:01 +04:00
parent 745fe9e98f
commit b132dc1455
9 changed files with 285 additions and 29 deletions

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ProjectSeaplane.DrawningObjects;
using ProjectSeaplane.Entities;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Generics
{
internal class DrawiningPlaneEqutables : IEqualityComparer<DrawningPlane?>
{
public bool Equals(DrawningPlane? x, DrawningPlane? 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 DrawningSeaplane && y is DrawningSeaplane)
{
EntitySeaplane _seplaneX = (EntitySeaplane)x.EntityPlane;
EntitySeaplane _seplaneY = (EntitySeaplane)y.EntityPlane;
if (_seplaneX.Boat != _seplaneY.Boat)
{
return false;
}
if (_seplaneX.Floater != _seplaneY.Floater)
{
return false;
}
if (_seplaneX.AdditionalColor != _seplaneY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningPlane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -30,6 +30,8 @@
{
pictureBoxCollection = new PictureBox();
groupBox1 = new GroupBox();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
groupBox2 = new GroupBox();
buttonDeleteObject = new Button();
listBoxStorages = new ListBox();
@ -62,6 +64,8 @@
//
// groupBox1
//
groupBox1.Controls.Add(ButtonSortByColor);
groupBox1.Controls.Add(ButtonSortByType);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(buttonRefreshCollection);
groupBox1.Controls.Add(buttonRemovePlane);
@ -74,6 +78,26 @@
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(14, 291);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(149, 23);
ButtonSortByColor.TabIndex = 6;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(14, 264);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(149, 23);
ButtonSortByType.TabIndex = 5;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// groupBox2
//
groupBox2.Controls.Add(buttonDeleteObject);
@ -180,14 +204,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Size = new Size(133, 22);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Size = new Size(133, 22);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -241,5 +265,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -34,6 +34,26 @@ namespace ProjectSeaplane
_logger = logger;
}
private void ButtonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor());
private void ComparePlanes(IComparer<DrawningPlane?> 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();
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
@ -41,20 +61,22 @@ namespace ProjectSeaplane
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
foreach (var key in _storage.Keys)
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(key);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
if (listBoxStorages.Items.Count > 0 && (index == -1 ||
index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
@ -149,6 +171,12 @@ namespace ProjectSeaplane
MessageBox.Show(ex.Message);
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
}
catch (ArgumentException ex)
{
_logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(planeDelegate);
@ -177,7 +205,7 @@ namespace ProjectSeaplane
{
return;
}
try
{
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectSeaplane.DrawningObjects;
using ProjectSeaplane.Entities;
namespace ProjectSeaplane.Generics
{
internal class PlaneCompareByColor : IComparer<DrawningPlane?>
{
public int Compare(DrawningPlane? x, DrawningPlane? 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.Name != y.EntityPlane.BodyColor.Name)
{
return x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawningPlane)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningSeaplane)
{
EntitySeaplane _seplaneX = (EntitySeaplane)x.EntityPlane;
EntitySeaplane _seplaneY = (EntitySeaplane)y.EntityPlane;
if (_seplaneX.AdditionalColor.Name != _seplaneY.AdditionalColor.Name)
{
return _seplaneX.AdditionalColor.Name.CompareTo(_seplaneY.AdditionalColor.Name);
}
}
var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight);
}
}
}

View File

@ -0,0 +1,28 @@
using ProjectSeaplane.DrawningObjects;
namespace ProjectSeaplane.Generics
{
internal class PlaneCompareByType : IComparer<DrawningPlane?>
{
public int Compare(DrawningPlane? x, DrawningPlane? 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);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSeaplane.Generics
{
internal 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)
{
// TODO прописать логику сравнения по свойству Name
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -12,6 +12,8 @@ namespace ProjectSeaplane.Generics
where T : DrawningPlane
where U : IMoveableObject
{
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Получение объектов коллекции
/// </summary>
@ -62,7 +64,7 @@ namespace ProjectSeaplane.Generics
{
return false;
}
return (bool)collect?._collection.Insert(obj);
return (bool)collect?._collection.Insert(obj, new DrawiningPlaneEqutables());
}
/// <summary>

View File

@ -8,11 +8,24 @@ using ProjectSeaplane.MovementStrategy;
using ProjectSeaplane.Generics;
using System.IO;
using ProjectSeaplane.Exceptions;
using System.Diagnostics.Eventing.Reader;
namespace ProjectSeaplane.Generics
{
internal class PlanesGenericStorage
{
readonly Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> _planeStorages;
public List<PlanesCollectionInfo> Keys => _planeStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
{
_planeStorages = new Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Разделитель для записи ключа и значения элемента словаря
/// </summary>
@ -38,7 +51,7 @@ namespace ProjectSeaplane.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<PlanesCollectionInfo,
PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> record in _planeStorages)
{
StringBuilder records = new();
@ -118,31 +131,32 @@ namespace ProjectSeaplane.Generics
}
}
_planeStorages.Add(name, collection);
_planeStorages.Add(new PlanesCollectionInfo(name, string.Empty), collection);
}
}
}
readonly Dictionary<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> _planeStorages;
public List<string> Keys => _planeStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
{
_planeStorages = new Dictionary<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_planeStorages.ContainsKey(name)) return;
_planeStorages[name] = new PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>(_pictureWidth, _pictureHeight);
if (_planeStorages.ContainsKey(new PlanesCollectionInfo(name, string.Empty)))
{
MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
_planeStorages.Add(new PlanesCollectionInfo(name, string.Empty), new PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (!_planeStorages.ContainsKey(name)) return;
_planeStorages.Remove(name);
PlanesGenericCollection<DrawningPlane, DrawningObjectPlane> plane;
if (_planeStorages.TryGetValue(new PlanesCollectionInfo(name, string.Empty), out plane))
{
_planeStorages.Remove(new PlanesCollectionInfo(name, string.Empty));
}
}
public PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>?
@ -150,7 +164,8 @@ namespace ProjectSeaplane.Generics
{
get
{
if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind];
PlanesCollectionInfo infPlane = new PlanesCollectionInfo(ind, string.Empty);
if (_planeStorages.ContainsKey(infPlane)) return _planeStorages[infPlane];
return null;
}
}

View File

@ -35,14 +35,16 @@ namespace ProjectSeaplane.Generics
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public bool Insert(T plane)
public bool Insert(T plane, IEqualityComparer<T?>? equal = null)
{
return Insert(plane, 0);
return Insert(plane, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -50,15 +52,21 @@ namespace ProjectSeaplane.Generics
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T plane, int position)
public bool Insert(T plane, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new PlaneNotFoundException(position);
if (Count > _maxCount)
{
throw new StorageOverflowException(Count);
}
if (equal != null && _places.Contains(plane, equal))
{
throw new ArgumentException("Добавляемый объект уже сущесвует в коллекции");
}
_places.Insert(0, plane);
return true;
}