PIbd-22. Safiulova K.N. LabWork_08 #12
50
base/Catamaran/Catamaran/CatamaranCompareByColor.cs
Normal file
50
base/Catamaran/Catamaran/CatamaranCompareByColor.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Catamaran.DrawningObjects;
|
||||
using Catamaran.Entities;
|
||||
|
||||
namespace Catamaran.Generics
|
||||
{
|
||||
internal class CatamaranCompareByColor : IComparer<DrawningCatamaran?>
|
||||
{
|
||||
public int Compare(DrawningCatamaran? x, DrawningCatamaran? y)
|
||||
{
|
||||
if (x == null || x.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
var bodyColorCompare = x.EntityCatamaran.BodyColor.Name.CompareTo(y.EntityCatamaran.BodyColor.Name);
|
||||
if (bodyColorCompare != 0)
|
||||
{
|
||||
return bodyColorCompare;
|
||||
}
|
||||
if (x.EntityCatamaran is EntitySailCatamaran xEntitySailCatamaran && y.EntityCatamaran is EntitySailCatamaran yEntitySailCatamaran)
|
||||
{
|
||||
var dumpBoxColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name);
|
||||
if (dumpBoxColorCompare != 0)
|
||||
{
|
||||
return dumpBoxColorCompare;
|
||||
}
|
||||
var tentColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name);
|
||||
if (tentColorCompare != 0)
|
||||
{
|
||||
return tentColorCompare;
|
||||
}
|
||||
}
|
||||
var speedCompare = x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight);
|
||||
}
|
||||
}
|
||||
}
|
35
base/Catamaran/Catamaran/CatamaranCompareByType.cs
Normal file
35
base/Catamaran/Catamaran/CatamaranCompareByType.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using Catamaran.DrawningObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Catamaran.Generics
|
||||
{
|
||||
internal class CatamaranCompareByType : IComparer<DrawningCatamaran?>
|
||||
{
|
||||
public int Compare(DrawningCatamaran? x, DrawningCatamaran? y)
|
||||
{
|
||||
if (x == null || x.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return x.GetType().Name.CompareTo(y.GetType().Name);
|
||||
}
|
||||
var speedCompare =
|
||||
x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed);
|
||||
if (speedCompare != 0)
|
||||
{
|
||||
return speedCompare;
|
||||
}
|
||||
return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight);
|
||||
}
|
||||
}
|
||||
}
|
30
base/Catamaran/Catamaran/CatamaransCollectionInfo.cs
Normal file
30
base/Catamaran/Catamaran/CatamaransCollectionInfo.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Catamaran.Generics
|
||||
{
|
||||
internal class CatamaransCollectionInfo : IEquatable<CatamaransCollectionInfo>
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public CatamaransCollectionInfo(string name, string description)
|
||||
{
|
||||
Name = name;
|
||||
Description = description;
|
||||
}
|
||||
public bool Equals(CatamaransCollectionInfo? other)
|
||||
{
|
||||
if (Name == other?.Name)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.Name.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -41,6 +41,8 @@ namespace Catamaran.Generics
|
||||
/// Получение объектов коллекции
|
||||
/// </summary>
|
||||
public IEnumerable<T?> GetCatamarans => _collection.GetCatamarans();
|
||||
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -66,7 +68,7 @@ namespace Catamaran.Generics
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
return (bool)collect?._collection.Insert(obj, new DrawingCatamaranEqutables());
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
|
@ -12,12 +12,12 @@ namespace Catamaran.Generics
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, CatamaransGenericCollection<DrawningCatamaran,
|
||||
readonly Dictionary<CatamaransCollectionInfo, CatamaransGenericCollection<DrawningCatamaran,
|
||||
DrawningObjectCatamaran>> _catamaranStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _catamaranStorages.Keys.ToList();
|
||||
public List<CatamaransCollectionInfo> Keys => _catamaranStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
@ -45,7 +45,7 @@ namespace Catamaran.Generics
|
||||
/// <param name="pictureHeight"></param>
|
||||
public CatamaransGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_catamaranStorages = new Dictionary<string,
|
||||
_catamaranStorages = new Dictionary<CatamaransCollectionInfo,
|
||||
CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
@ -57,8 +57,7 @@ namespace Catamaran.Generics
|
||||
public void AddSet(string name)
|
||||
{
|
||||
// TODO Прописать логику для добавления
|
||||
if (_catamaranStorages.ContainsKey(name)) return;
|
||||
_catamaranStorages[name] = new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight);
|
||||
_catamaranStorages.Add(new CatamaransCollectionInfo(name, string.Empty), new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
@ -67,9 +66,9 @@ namespace Catamaran.Generics
|
||||
public void DelSet(string name)
|
||||
{
|
||||
// TODO Прописать логику для удаления
|
||||
if (!_catamaranStorages.ContainsKey(name))
|
||||
if (!_catamaranStorages.ContainsKey(new CatamaransCollectionInfo(name, string.Empty)))
|
||||
return;
|
||||
_catamaranStorages.Remove(name);
|
||||
_catamaranStorages.Remove(new CatamaransCollectionInfo(name, string.Empty));
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
@ -82,8 +81,9 @@ namespace Catamaran.Generics
|
||||
get
|
||||
{
|
||||
// TODO Продумать логику получения набора
|
||||
if (_catamaranStorages.ContainsKey(ind))
|
||||
return _catamaranStorages[ind];
|
||||
CatamaransCollectionInfo indObj = new CatamaransCollectionInfo(ind, string.Empty);
|
||||
if (_catamaranStorages.ContainsKey(indObj))
|
||||
return _catamaranStorages[indObj];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -99,14 +99,14 @@ namespace Catamaran.Generics
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>> record in _catamaranStorages)
|
||||
foreach (KeyValuePair<CatamaransCollectionInfo, CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>> record in _catamaranStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningCatamaran? elem in record.Value.GetCatamarans)
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
@ -173,7 +173,7 @@ namespace Catamaran.Generics
|
||||
}
|
||||
}
|
||||
}
|
||||
_catamaranStorages.Add(record[0], collection);
|
||||
_catamaranStorages.Add(new CatamaransCollectionInfo(record[0], string.Empty), collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
59
base/Catamaran/Catamaran/DrawingCatamaranEqutables.cs
Normal file
59
base/Catamaran/Catamaran/DrawingCatamaranEqutables.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Catamaran.DrawningObjects;
|
||||
using Catamaran.Entities;
|
||||
|
||||
namespace Catamaran.Generics
|
||||
{
|
||||
internal class DrawingCatamaranEqutables : IEqualityComparer<DrawningCatamaran?>
|
||||
{
|
||||
public bool Equals(DrawningCatamaran? x, DrawningCatamaran? y)
|
||||
{
|
||||
if (x == null || x.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
}
|
||||
if (y == null || y.EntityCatamaran == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
}
|
||||
if (x.GetType().Name != y.GetType().Name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityCatamaran.Speed != y.EntityCatamaran.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityCatamaran.Weight != y.EntityCatamaran.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x.EntityCatamaran.BodyColor != y.EntityCatamaran.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (x is DrawningSailCatamaran && y is DrawningSailCatamaran)
|
||||
{
|
||||
EntitySailCatamaran EntityX = (EntitySailCatamaran)x.EntityCatamaran;
|
||||
EntitySailCatamaran EntityY = (EntitySailCatamaran)y.EntityCatamaran;
|
||||
if (EntityX.Sail != EntityY.Sail)
|
||||
return false;
|
||||
if (EntityX.FloatDetail != EntityY.FloatDetail)
|
||||
return false;
|
||||
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public int GetHashCode([DisallowNull] DrawningCatamaran obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -45,6 +45,8 @@
|
||||
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
|
||||
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||
this.SortByType = new System.Windows.Forms.Button();
|
||||
this.SortByColor = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxSets.SuspendLayout();
|
||||
@ -61,6 +63,8 @@
|
||||
//
|
||||
// groupBoxTools
|
||||
//
|
||||
this.groupBoxTools.Controls.Add(this.SortByColor);
|
||||
this.groupBoxTools.Controls.Add(this.SortByType);
|
||||
this.groupBoxTools.Controls.Add(this.groupBoxSets);
|
||||
this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber);
|
||||
this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection);
|
||||
@ -80,7 +84,7 @@
|
||||
this.groupBoxSets.Controls.Add(this.buttonDelObject);
|
||||
this.groupBoxSets.Controls.Add(this.listBoxStorages);
|
||||
this.groupBoxSets.Controls.Add(this.buttonAddObject);
|
||||
this.groupBoxSets.Location = new System.Drawing.Point(3, 63);
|
||||
this.groupBoxSets.Location = new System.Drawing.Point(3, 46);
|
||||
this.groupBoxSets.Name = "groupBoxSets";
|
||||
this.groupBoxSets.Size = new System.Drawing.Size(138, 203);
|
||||
this.groupBoxSets.TabIndex = 4;
|
||||
@ -126,7 +130,7 @@
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 321);
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(0, 351);
|
||||
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23);
|
||||
this.maskedTextBoxNumber.TabIndex = 3;
|
||||
@ -143,7 +147,7 @@
|
||||
//
|
||||
// ButtonRemoveCatamaran
|
||||
//
|
||||
this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 363);
|
||||
this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(-1, 380);
|
||||
this.ButtonRemoveCatamaran.Name = "ButtonRemoveCatamaran";
|
||||
this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(140, 34);
|
||||
this.ButtonRemoveCatamaran.TabIndex = 1;
|
||||
@ -153,9 +157,9 @@
|
||||
//
|
||||
// ButtonAddCatamaran
|
||||
//
|
||||
this.ButtonAddCatamaran.Location = new System.Drawing.Point(3, 270);
|
||||
this.ButtonAddCatamaran.Location = new System.Drawing.Point(-1, 311);
|
||||
this.ButtonAddCatamaran.Name = "ButtonAddCatamaran";
|
||||
this.ButtonAddCatamaran.Size = new System.Drawing.Size(137, 34);
|
||||
this.ButtonAddCatamaran.Size = new System.Drawing.Size(140, 34);
|
||||
this.ButtonAddCatamaran.TabIndex = 0;
|
||||
this.ButtonAddCatamaran.Text = "Добавить катамаран";
|
||||
this.ButtonAddCatamaran.UseVisualStyleBackColor = true;
|
||||
@ -183,14 +187,14 @@
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
|
||||
this.SaveToolStripMenuItem.Text = "Сохранение";
|
||||
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
|
||||
this.LoadToolStripMenuItem.Text = "Загрузка";
|
||||
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
|
||||
//
|
||||
@ -198,6 +202,26 @@
|
||||
//
|
||||
this.openFileDialog.FileName = "openFileDialog1";
|
||||
//
|
||||
// SortByType
|
||||
//
|
||||
this.SortByType.Location = new System.Drawing.Point(-1, 255);
|
||||
this.SortByType.Name = "SortByType";
|
||||
this.SortByType.Size = new System.Drawing.Size(140, 23);
|
||||
this.SortByType.TabIndex = 6;
|
||||
this.SortByType.Text = "Сортировка по типу";
|
||||
this.SortByType.UseVisualStyleBackColor = true;
|
||||
this.SortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
|
||||
//
|
||||
// SortByColor
|
||||
//
|
||||
this.SortByColor.Location = new System.Drawing.Point(0, 282);
|
||||
this.SortByColor.Name = "SortByColor";
|
||||
this.SortByColor.Size = new System.Drawing.Size(139, 23);
|
||||
this.SortByColor.TabIndex = 7;
|
||||
this.SortByColor.Text = "Сортировка по цвету";
|
||||
this.SortByColor.UseVisualStyleBackColor = true;
|
||||
this.SortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
|
||||
//
|
||||
// FormCatamaranCollection
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
@ -238,5 +262,7 @@
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private Button SortByColor;
|
||||
private Button SortByType;
|
||||
}
|
||||
}
|
@ -49,7 +49,7 @@ namespace Catamaran
|
||||
listBoxStorages.Items.Clear();
|
||||
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))
|
||||
@ -106,6 +106,10 @@ namespace Catamaran
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
@ -277,5 +281,22 @@ namespace Catamaran
|
||||
ReloadObjects();
|
||||
}
|
||||
}
|
||||
private void buttonSortByType_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByType());
|
||||
private void CompareCatamarans(IComparer<DrawningCatamaran?> comparer)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
obj.Sort(comparer);
|
||||
pictureBoxCollection.Image = obj.ShowCatamarans();
|
||||
}
|
||||
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByColor());
|
||||
}
|
||||
}
|
@ -67,6 +67,6 @@
|
||||
<value>254, 17</value>
|
||||
</metadata>
|
||||
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>96</value>
|
||||
<value>25</value>
|
||||
</metadata>
|
||||
</root>
|
@ -27,6 +27,8 @@ namespace Catamaran.Generics
|
||||
/// Максимальное количество объектов в списке
|
||||
/// </summary>
|
||||
private readonly int _maxCount;
|
||||
|
||||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -41,23 +43,24 @@ namespace Catamaran.Generics
|
||||
/// </summary>
|
||||
/// <param name="car">Добавляемый катамаран</param>
|
||||
/// <returns></returns>
|
||||
public bool Insert(T catamaran)
|
||||
public bool Insert(T catamaran, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
return Insert(catamaran, 0);
|
||||
return Insert(catamaran, 0, equal);
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="boat">Добавляемая лодка</param>
|
||||
/// <param name="catamaran">Добавляемая лодка</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public bool Insert(T catamaran, int position)
|
||||
{
|
||||
public bool Insert(T catamaran, int position, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
throw new CatamaranNotFoundException(position);
|
||||
|
||||
if (Count >= _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
if (equal != null && _places.Contains(catamaran, equal))
|
||||
throw new ArgumentException("Данный объект уже есть в коллекции");
|
||||
_places.Insert(0, catamaran);
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user