2 Commits
laba7 ... lab_8

Author SHA1 Message Date
Alenka
72dcd86427 Done 2023-12-23 15:46:22 +04:00
Alenka
0f3e9f9a46 Done 2023-12-23 15:06:55 +04:00
10 changed files with 291 additions and 35 deletions

View File

@@ -0,0 +1,24 @@
using Cruiser.Generics;
namespace Cruiser
{
internal class CruiserCollectionInfo : IEquatable<CruiserCollectionInfo?>
{
public string Name { get; private set; }
public string Description { get; private set; }
public CruiserCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(CruiserCollectionInfo? other)
{
if (other == null || other.Name == null)
throw new ArgumentNullException(nameof(other));
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
namespace Cruiser.Generics
{
internal class CruiserCompareByColor : IComparer<DrawningCruiser>
{
public int Compare(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityCruiser == null)
throw new ArgumentNullException(nameof(y));
var xCruiser = x.EntityCruiser;
var yCruiser = y.EntityCruiser;
if (xCruiser.BodyColor != yCruiser.BodyColor)
return xCruiser.BodyColor.Name.CompareTo(yCruiser.BodyColor.Name);
var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight);
}
}
}

View File

@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
namespace Cruiser.Generics
{
internal class CruiserCompareByType : IComparer<DrawningCruiser>
{
public int Compare(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityCruiser == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
return x.GetType().Name.CompareTo(y.GetType().Name);
var speedCompare = x.EntityCruiser.Speed.CompareTo(y.EntityCruiser.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityCruiser.Weight.CompareTo(y.EntityCruiser.Weight);
}
}
}

View File

@@ -18,8 +18,9 @@ namespace Cruiser.Generics
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 170; private readonly int _placeSizeWidth = 170;
private readonly int _placeSizeHeight = 124; private readonly int _placeSizeHeight = 200;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public CruiserGenericCollection(int picWidth, int picHeight) public CruiserGenericCollection(int picWidth, int picHeight)
{ {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
@@ -32,7 +33,7 @@ namespace Cruiser.Generics
{ {
if (obj == null || collect == null) if (obj == null || collect == null)
return false; return false;
collect?._collection.Insert(obj); collect?._collection.Insert(obj, new DrawningCruiserEqutables());
return true; return true;
} }
public static T? operator -(CruiserGenericCollection<T, U> collect, int pos) public static T? operator -(CruiserGenericCollection<T, U> collect, int pos)
@@ -77,7 +78,7 @@ namespace Cruiser.Generics
if (cruiser != null) if (cruiser != null)
{ {
int inRow = _pictureWidth / _placeSizeWidth; int inRow = _pictureWidth / _placeSizeWidth;
cruiser.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight); cruiser.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeHeight / 2 - 8, i / inRow * _placeSizeHeight + 20);
cruiser.DrawTransport(g); cruiser.DrawTransport(g);
} }
i++; i++;

View File

@@ -10,9 +10,9 @@ namespace Cruiser.Generics
{ {
internal class CruiserGenericStorage internal class CruiserGenericStorage
{ {
readonly Dictionary<string, CruiserGenericCollection<DrawningCruiser, readonly Dictionary<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser,
DrawningObjectCruiser>> _cruiserStorages; DrawningObjectCruiser>> _cruiserStorages;
public List<string> Keys => _cruiserStorages.Keys.ToList(); public List<CruiserCollectionInfo> Keys => _cruiserStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|'; private static readonly char _separatorForKeyValue = '|';
@@ -20,31 +20,30 @@ namespace Cruiser.Generics
private static readonly char _separatorForObject = ':'; private static readonly char _separatorForObject = ':';
public CruiserGenericStorage(int pictureWidth, int pictureHeight) public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{ {
_cruiserStorages = new Dictionary<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>(); _cruiserStorages = new Dictionary<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
public void AddSet(string name) public void AddSet(string name)
{ {
_cruiserStorages.Add(name, new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight)); _cruiserStorages.Add(new CruiserCollectionInfo(name, string.Empty), new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight));
} }
public void DelSet(string name) public void DelSet(string name)
{ {
if (!_cruiserStorages.ContainsKey(name)) if (!_cruiserStorages.ContainsKey(new CruiserCollectionInfo(name, string.Empty)));
{ {
return; return;
} }
_cruiserStorages.Remove(name); _cruiserStorages.Remove(new CruiserCollectionInfo(name, string.Empty));
} }
public CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>? this[string ind] public CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>? this[string ind]
{ {
get get
{ {
if (_cruiserStorages.ContainsKey(ind)) CruiserCollectionInfo indObj = new CruiserCollectionInfo(ind, string.Empty);
{ if (_cruiserStorages.ContainsKey(indObj))
return _cruiserStorages[ind]; return _cruiserStorages[indObj];
}
return null; return null;
} }
} }
@@ -55,22 +54,22 @@ namespace Cruiser.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> record in _cruiserStorages) foreach (KeyValuePair<CruiserCollectionInfo, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>> record in _cruiserStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawningCruiser? elem in record.Value.GetCruisers) foreach (DrawningCruiser? elem in record.Value.GetCruisers)
{ {
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)
{ {
throw new IOException("Невалидная операция, нет данных для сохранения"); throw new Exception("Невалидная операция, нет данных для сохранения");
} }
using (StreamWriter sw = new(filename)) using (StreamWriter sw = new(filename))
{ {
sw.WriteLine($"CruiserStorage{Environment.NewLine}{data}"); sw.WriteLine($"PlaneStorage{Environment.NewLine}{data}");
} }
return; return;
} }
@@ -88,7 +87,7 @@ namespace Cruiser.Generics
{ {
throw new IOException("Нет данных для загрузки"); throw new IOException("Нет данных для загрузки");
} }
if (!strs[0].StartsWith("CruiserStorage")) if (!strs[0].StartsWith("PlaneStorage"))
{ {
throw new IOException("Неверный формат данных"); throw new IOException("Неверный формат данных");
} }
@@ -115,7 +114,7 @@ namespace Cruiser.Generics
} }
} }
} }
_cruiserStorages.Add(record[0], collection); _cruiserStorages.Add(new CruiserCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine(); str = sr.ReadLine();
} while (str != null); } while (str != null);
} }

View File

@@ -0,0 +1,57 @@
using Cruiser.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
namespace Cruiser
{
internal class DrawingCruiserEqutables : IEqualityComparer<DrawningCruiser?>
{
public bool Equals(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCruiser.Speed != y.EntityCruiser.Speed)
{
return false;
}
if (x.EntityCruiser.Weight != y.EntityCruiser.Weight)
{
return false;
}
if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor)
{
return false;
}
if (x is DrawningAdvancedCruiser && y is DrawningAdvancedCruiser)
{
EntityAdvancedCruiser EntityX = (EntityAdvancedCruiser)x.EntityCruiser;
EntityAdvancedCruiser EntityY = (EntityAdvancedCruiser)y.EntityCruiser;
if (EntityX.HelicopterPad != EntityY.HelicopterPad)
return false;
if (EntityX.Coating != EntityY.Coating)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCruiser? obj)
{
return obj.GetHashCode();
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
namespace Cruiser
{
internal class DrawningCruiserEqutables : IEqualityComparer<DrawningCruiser?>
{
public bool Equals(DrawningCruiser? x, DrawningCruiser? y)
{
if (x == null || x.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCruiser == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCruiser.Speed != y.EntityCruiser.Speed)
{
return false;
}
if (x.EntityCruiser.Weight != y.EntityCruiser.Weight)
{
return false;
}
if (x.EntityCruiser.BodyColor != y.EntityCruiser.BodyColor)
{
return false;
}
if (x is DrawningAdvancedCruiser && y is DrawningAdvancedCruiser)
{
EntityAdvancedCruiser EntityX = (EntityAdvancedCruiser)x.EntityCruiser;
EntityAdvancedCruiser EntityY = (EntityAdvancedCruiser)y.EntityCruiser;
if (EntityX.HelicopterPad != EntityY.HelicopterPad)
return false;
if (EntityX.Coating != EntityY.Coating)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCruiser? obj)
{
return obj.GetHashCode();
}
}
}

View File

@@ -32,6 +32,8 @@
this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit();
this.panelCruiser.SuspendLayout(); this.panelCruiser.SuspendLayout();
this.panelSet.SuspendLayout(); this.panelSet.SuspendLayout();
@@ -43,26 +45,28 @@
this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 33); this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 33);
this.pictureBoxCruiser.Name = "pictureBoxCruiser"; this.pictureBoxCruiser.Name = "pictureBoxCruiser";
this.pictureBoxCruiser.Size = new System.Drawing.Size(904, 477); this.pictureBoxCruiser.Size = new System.Drawing.Size(1083, 556);
this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCruiser.TabIndex = 0; this.pictureBoxCruiser.TabIndex = 0;
this.pictureBoxCruiser.TabStop = false; this.pictureBoxCruiser.TabStop = false;
// //
// panelCruiser // panelCruiser
// //
this.panelCruiser.Controls.Add(this.buttonSortByColor);
this.panelCruiser.Controls.Add(this.buttonSortByType);
this.panelCruiser.Controls.Add(this.ButtonRefreshCollection); this.panelCruiser.Controls.Add(this.ButtonRefreshCollection);
this.panelCruiser.Controls.Add(this.ButtonRemoveCruiser); this.panelCruiser.Controls.Add(this.ButtonRemoveCruiser);
this.panelCruiser.Controls.Add(this.textBoxCruiser); this.panelCruiser.Controls.Add(this.textBoxCruiser);
this.panelCruiser.Controls.Add(this.ButtonAddCruiser); this.panelCruiser.Controls.Add(this.ButtonAddCruiser);
this.panelCruiser.Controls.Add(this.panelSet); this.panelCruiser.Controls.Add(this.panelSet);
this.panelCruiser.Location = new System.Drawing.Point(655, 12); this.panelCruiser.Location = new System.Drawing.Point(786, 36);
this.panelCruiser.Name = "panelCruiser"; this.panelCruiser.Name = "panelCruiser";
this.panelCruiser.Size = new System.Drawing.Size(221, 486); this.panelCruiser.Size = new System.Drawing.Size(221, 553);
this.panelCruiser.TabIndex = 1; this.panelCruiser.TabIndex = 1;
// //
// ButtonRefreshCollection // ButtonRefreshCollection
// //
this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 435); this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 500);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(138, 41); this.ButtonRefreshCollection.Size = new System.Drawing.Size(138, 41);
this.ButtonRefreshCollection.TabIndex = 2; this.ButtonRefreshCollection.TabIndex = 2;
@@ -72,7 +76,7 @@
// //
// ButtonRemoveCruiser // ButtonRemoveCruiser
// //
this.ButtonRemoveCruiser.Location = new System.Drawing.Point(45, 391); this.ButtonRemoveCruiser.Location = new System.Drawing.Point(45, 456);
this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser";
this.ButtonRemoveCruiser.Size = new System.Drawing.Size(138, 38); this.ButtonRemoveCruiser.Size = new System.Drawing.Size(138, 38);
this.ButtonRemoveCruiser.TabIndex = 2; this.ButtonRemoveCruiser.TabIndex = 2;
@@ -82,7 +86,7 @@
// //
// textBoxCruiser // textBoxCruiser
// //
this.textBoxCruiser.Location = new System.Drawing.Point(45, 330); this.textBoxCruiser.Location = new System.Drawing.Point(50, 419);
this.textBoxCruiser.Name = "textBoxCruiser"; this.textBoxCruiser.Name = "textBoxCruiser";
this.textBoxCruiser.Size = new System.Drawing.Size(133, 31); this.textBoxCruiser.Size = new System.Drawing.Size(133, 31);
this.textBoxCruiser.TabIndex = 3; this.textBoxCruiser.TabIndex = 3;
@@ -161,7 +165,7 @@
this.FileToolStripMenuItem}); this.FileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip"; this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(904, 33); this.menuStrip.Size = new System.Drawing.Size(1083, 33);
this.menuStrip.TabIndex = 2; this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "menuStrip"; this.menuStrip.Text = "menuStrip";
// //
@@ -177,22 +181,42 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(270, 34); this.SaveToolStripMenuItem.Size = new System.Drawing.Size(200, 34);
this.SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); this.LoadToolStripMenuItem.Size = new System.Drawing.Size(200, 34);
this.LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(50, 320);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(128, 33);
this.buttonSortByType.TabIndex = 4;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(50, 372);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(133, 27);
this.buttonSortByColor.TabIndex = 5;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormCruiserCollection // FormCruiserCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(904, 510); this.ClientSize = new System.Drawing.Size(1083, 589);
this.Controls.Add(this.panelCruiser); this.Controls.Add(this.panelCruiser);
this.Controls.Add(this.pictureBoxCruiser); this.Controls.Add(this.pictureBoxCruiser);
this.Controls.Add(this.menuStrip); this.Controls.Add(this.menuStrip);
@@ -229,5 +253,7 @@
private ToolStripMenuItem FileToolStripMenuItem; private ToolStripMenuItem FileToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem; private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@@ -32,7 +32,7 @@ namespace Cruiser
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))
@@ -102,6 +102,11 @@ namespace Cruiser
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
} }
catch (ArgumentException)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
}); });
form.AddEvent(cruiserDelegate); form.AddEvent(cruiserDelegate);
} }
@@ -198,5 +203,24 @@ namespace Cruiser
} }
} }
} }
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByColor());
private void CompareCruiser(IComparer<DrawningCruiser?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCruiser.Image = obj.ShowCruisers();
}
} }
} }

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cruiser.Exceptions; using Cruiser.Exceptions;
using System.Numerics;
namespace Cruiser.Generics namespace Cruiser.Generics
{ {
@@ -20,19 +21,25 @@ namespace Cruiser.Generics
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public void Insert(T cruiser) public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public void Insert(T cruiser, IEqualityComparer<T>? equal = null)
{ {
if (_places.Count == _maxCount) if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);
Insert(cruiser, 0); Insert(cruiser, 0, equal);
} }
public void Insert(T cruiser, int position, IEqualityComparer<T>? equal = null)
public void Insert(T cruiser, int position)
{ {
if (_places.Count == _maxCount) if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count)) if (!(position >= 0 && position <= Count))
throw new Exception("Неверная позиция для вставки"); throw new Exception("Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(cruiser, equal))
throw new ArgumentException(nameof(cruiser));
}
_places.Insert(position, cruiser); _places.Insert(position, cruiser);
} }
public void Remove(int position) public void Remove(int position)