Фулл 8 лаба надеюсь

This commit is contained in:
Marselchi 2023-12-18 16:11:55 +04:00
parent 8c7a4a2c6b
commit 5af581e096
9 changed files with 265 additions and 21 deletions

View File

@ -29,6 +29,8 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
buttonSortByType = new Button();
buttonSortByColor = new Button();
Storages = new GroupBox(); Storages = new GroupBox();
listBoxStorages = new ListBox(); listBoxStorages = new ListBox();
buttonDeleteSet = new Button(); buttonDeleteSet = new Button();
@ -53,6 +55,8 @@
// //
// groupBoxTools // groupBoxTools
// //
groupBoxTools.Controls.Add(buttonSortByType);
groupBoxTools.Controls.Add(buttonSortByColor);
groupBoxTools.Controls.Add(Storages); groupBoxTools.Controls.Add(Storages);
groupBoxTools.Controls.Add(buttonRefreshCollection); groupBoxTools.Controls.Add(buttonRefreshCollection);
groupBoxTools.Controls.Add(buttonDeleteLiner); groupBoxTools.Controls.Add(buttonDeleteLiner);
@ -67,6 +71,28 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Tools"; groupBoxTools.Text = "Tools";
// //
// buttonSortByType
//
buttonSortByType.Location = new Point(28, 413);
buttonSortByType.Margin = new Padding(3, 4, 3, 4);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(190, 41);
buttonSortByType.TabIndex = 8;
buttonSortByType.Text = "Sort By Type";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(28, 462);
buttonSortByColor.Margin = new Padding(3, 4, 3, 4);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(190, 41);
buttonSortByColor.TabIndex = 7;
buttonSortByColor.Text = "Sort By Color";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// Storages // Storages
// //
Storages.Controls.Add(listBoxStorages); Storages.Controls.Add(listBoxStorages);
@ -125,7 +151,7 @@
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
buttonRefreshCollection.Location = new Point(13, 668); buttonRefreshCollection.Location = new Point(13, 691);
buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4); buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4);
buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(222, 53); buttonRefreshCollection.Size = new Size(222, 53);
@ -136,7 +162,7 @@
// //
// buttonDeleteLiner // buttonDeleteLiner
// //
buttonDeleteLiner.Location = new Point(13, 544); buttonDeleteLiner.Location = new Point(13, 607);
buttonDeleteLiner.Margin = new Padding(3, 4, 3, 4); buttonDeleteLiner.Margin = new Padding(3, 4, 3, 4);
buttonDeleteLiner.Name = "buttonDeleteLiner"; buttonDeleteLiner.Name = "buttonDeleteLiner";
buttonDeleteLiner.Size = new Size(222, 53); buttonDeleteLiner.Size = new Size(222, 53);
@ -147,7 +173,7 @@
// //
// textBoxNumber // textBoxNumber
// //
textBoxNumber.Location = new Point(47, 491); textBoxNumber.Location = new Point(47, 572);
textBoxNumber.Margin = new Padding(3, 4, 3, 4); textBoxNumber.Margin = new Padding(3, 4, 3, 4);
textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(158, 27); textBoxNumber.Size = new Size(158, 27);
@ -155,7 +181,7 @@
// //
// buttonAddLiner // buttonAddLiner
// //
buttonAddLiner.Location = new Point(13, 417); buttonAddLiner.Location = new Point(13, 511);
buttonAddLiner.Margin = new Padding(3, 4, 3, 4); buttonAddLiner.Margin = new Padding(3, 4, 3, 4);
buttonAddLiner.Name = "buttonAddLiner"; buttonAddLiner.Name = "buttonAddLiner";
buttonAddLiner.Size = new Size(222, 53); buttonAddLiner.Size = new Size(222, 53);
@ -256,5 +282,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
} }
} }

View File

@ -85,6 +85,11 @@ namespace Liner
_logger.LogWarning($"Добавление лайнера в {listBoxStorages.SelectedItem.ToString()} не удалось (переполнение коллекции)"); _logger.LogWarning($"Добавление лайнера в {listBoxStorages.SelectedItem.ToString()} не удалось (переполнение коллекции)");
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
} }
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
}
} }
/// <summary> /// <summary>
/// Удаление объекта из набора /// Удаление объекта из набора
@ -224,7 +229,7 @@ namespace Liner
listBoxStorages.Items.Clear(); listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++) for (int i = 0; i < _storage.Keys.Count; i++)
{ {
listBoxStorages.Items.Add(_storage.Keys[i]); 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))
{ {
@ -272,5 +277,35 @@ namespace Liner
} }
} }
} }
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareLiners(IComparer<DrawingLiner?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowLiners();
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLiners(new LinerCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLiners(new LinerCompareByColor());
} }
} }

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liner.Drawing;
using Liner.Entities;
namespace Liner.Generics
{
internal class DrawingLinerEqutables : IEqualityComparer<DrawingLiner?>
{
public bool Equals(DrawingLiner? x, DrawingLiner? y)
{
if (x == null || x.EntityLiner == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLiner == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityLiner.Speed != y.EntityLiner.Speed)
{
return false;
}
if (x.EntityLiner.Weight != y.EntityLiner.Weight)
{
return false;
}
if (x.EntityLiner.BottomColor != y.EntityLiner.BottomColor)
{
return false;
}
if (x is DrawingBigLiner && y is DrawingBigLiner)
{
EntityBigLiner _LinerX = (EntityBigLiner)x.EntityLiner;
EntityBigLiner _LinerY = (EntityBigLiner)y.EntityLiner;
if (_LinerX.SwimmingPool != _LinerY.SwimmingPool)
{
return false;
}
if (_LinerX.Deck != _LinerY.Deck)
{
return false;
}
if (_LinerX.BodyColor != _LinerY.BodyColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawingLiner obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Liner.Generics
{
public class LinerCollectionInfo : IEquatable<LinerCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public LinerCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(LinerCollectionInfo? other)
{
if (Name == other?.Name)
return true;
return false;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liner.Entities;
using Liner.Drawing;
namespace Liner.Generics
{
internal class LinerCompareByColor : IComparer<DrawingLiner?>
{
public int Compare(DrawingLiner? x, DrawingLiner? y)
{
if (x == null || x.EntityLiner == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLiner == null)
{
throw new ArgumentNullException(nameof(y));
}
var bottomColorCompare = x.EntityLiner.BottomColor.Name.CompareTo(y.EntityLiner.BottomColor.Name);
if (bottomColorCompare != 0)
{
return bottomColorCompare;
}
if (x.EntityLiner is EntityBigLiner _entityBigLinerX && y.EntityLiner is EntityBigLiner _entityBigLinerY)
{
var bodyColorCompare = _entityBigLinerX.BodyColor.Name.CompareTo(_entityBigLinerY.BodyColor.Name);
if (bodyColorCompare != 0)
{
return bodyColorCompare;
}
}
var speedCompare = x.EntityLiner.Speed.CompareTo(y.EntityLiner.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLiner.Weight.CompareTo(y.EntityLiner.Weight);
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liner.Drawing;
using Liner.Entities;
namespace Liner.Generics
{
internal class LinerCompareByType : IComparer<DrawingLiner?>
{
public int Compare(DrawingLiner? x, DrawingLiner? y)
{
if (x == null || x.EntityLiner == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityLiner == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityLiner.Speed.CompareTo(y.EntityLiner.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityLiner.Weight.CompareTo(y.EntityLiner.Weight);
}
}
}

View File

@ -54,7 +54,11 @@ namespace Liner.Generics
/// Получение объектов коллекции /// Получение объектов коллекции
/// <summary> /// <summary>
public IEnumerable<T?> GetLiners => _collection.GetLiners(); public IEnumerable<T?> GetLiners => _collection.GetLiners();
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// Перегрузка оператора сложения /// Перегрузка оператора сложения
/// </summary> /// </summary>
/// <param name="collect"></param> /// <param name="collect"></param>
@ -66,7 +70,7 @@ namespace Liner.Generics
{ {
return -1; return -1;
} }
return collect?._collection.Insert(obj); return collect._collection.Insert(obj, new DrawingLinerEqutables());
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания

View File

@ -16,11 +16,11 @@ namespace Liner.Generics
/// <summary> /// <summary>
/// Словарь (хранилище) /// Словарь (хранилище)
/// </summary> /// </summary>
readonly Dictionary<string, LinerGenericCollection<DrawingLiner, DrawingObjectLiner>> _linerStorages; readonly Dictionary<LinerCollectionInfo, LinerGenericCollection<DrawingLiner, DrawingObjectLiner>> _linerStorages;
/// <summary> /// <summary>
/// Возвращение списка названий наборов /// Возвращение списка названий наборов
/// </summary> /// </summary>
public List<string> Keys => _linerStorages.Keys.ToList(); public List<LinerCollectionInfo> Keys => _linerStorages.Keys.ToList();
/// <summary> /// <summary>
/// Ширина окна отрисовки /// Ширина окна отрисовки
/// </summary> /// </summary>
@ -48,7 +48,7 @@ namespace Liner.Generics
/// <param name="pictureHeight"></param> /// <param name="pictureHeight"></param>
public LinersGenericStorage(int pictureWidth, int pictureHeight) public LinersGenericStorage(int pictureWidth, int pictureHeight)
{ {
_linerStorages = new Dictionary<string, LinerGenericCollection<DrawingLiner, DrawingObjectLiner>>(); _linerStorages = new Dictionary<LinerCollectionInfo, LinerGenericCollection<DrawingLiner, DrawingObjectLiner>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
@ -58,11 +58,11 @@ namespace Liner.Generics
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void AddSet(string name) public void AddSet(string name)
{ {
if(_linerStorages.ContainsKey(name)) if(_linerStorages.ContainsKey(new LinerCollectionInfo(name, string.Empty)))
{ {
return; return;
} }
_linerStorages.Add(name, new LinerGenericCollection<DrawingLiner, DrawingObjectLiner>(_pictureWidth,_pictureHeight)); _linerStorages.Add(new LinerCollectionInfo(name, string.Empty), new LinerGenericCollection<DrawingLiner, DrawingObjectLiner>(_pictureWidth,_pictureHeight));
} }
/// <summary> /// <summary>
/// Удаление набора /// Удаление набора
@ -70,9 +70,9 @@ namespace Liner.Generics
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void DelSet(string name) public void DelSet(string name)
{ {
if (_linerStorages.ContainsKey(name)) if (_linerStorages.ContainsKey(new LinerCollectionInfo(name, string.Empty)))
{ {
_linerStorages.Remove(name); _linerStorages.Remove(new LinerCollectionInfo(name, string.Empty));
} }
} }
/// <summary> /// <summary>
@ -84,7 +84,7 @@ namespace Liner.Generics
{ {
get get
{ {
if (_linerStorages.ContainsKey(ind)) { return _linerStorages[ind]; } if (_linerStorages.ContainsKey(new LinerCollectionInfo(ind, string.Empty))) { return _linerStorages[new LinerCollectionInfo(ind, string.Empty)]; }
return null; return null;
} }
} }
@ -99,7 +99,7 @@ namespace Liner.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<LinerCollectionInfo,
LinerGenericCollection<DrawingLiner, DrawingObjectLiner>> record in _linerStorages) LinerGenericCollection<DrawingLiner, DrawingObjectLiner>> record in _linerStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
@ -107,7 +107,7 @@ namespace Liner.Generics
{ {
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
} }
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
} }
if (data.Length == 0) if (data.Length == 0)
{ {
@ -167,7 +167,7 @@ namespace Liner.Generics
} }
} }
} }
_linerStorages.Add(name, collection); _linerStorages.Add(new LinerCollectionInfo(name, string.Empty), collection);
} }
} }
} }

View File

@ -40,13 +40,13 @@ namespace Liner.Generics
/// </summary> /// </summary>
/// <param name="liner">Добавляемый лайнер</param> /// <param name="liner">Добавляемый лайнер</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T liner) public int Insert(T liner, IEqualityComparer<T?>? equal = null)
{ {
if (_places.Count >= _maxCount) if (_places.Count >= _maxCount)
{ {
throw new StorageOverflowException(_places.Count); throw new StorageOverflowException(_places.Count);
} }
_places.Insert(0, liner); return Insert(liner,0, equal);
return 0; return 0;
} }
@ -56,7 +56,7 @@ namespace Liner.Generics
/// <param name="liner">Добавляемый лайнер</param> /// <param name="liner">Добавляемый лайнер</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T liner, int position) public int Insert(T liner, int position, IEqualityComparer<T?>? equal = null)
{ {
if(_places.Count >= _maxCount) if(_places.Count >= _maxCount)
{ {
@ -66,6 +66,10 @@ namespace Liner.Generics
{ {
throw new LinerNotFoundException(position); throw new LinerNotFoundException(position);
} }
if (equal != null && _places.Contains(liner, equal))
{
throw new ArgumentException("Этот объект уже существует в коллекции");
}
_places.Insert(position, liner); _places.Insert(position, liner);
return position; return position;
} }
@ -83,6 +87,7 @@ namespace Liner.Generics
} }
throw new LinerNotFoundException(position); throw new LinerNotFoundException(position);
} }
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary> /// <summary>
/// Получение объекта из набора по позиции /// Получение объекта из набора по позиции
/// </summary> /// </summary>