что-то вечно, но ошибочно полагать, что это вечное останется с тобой навечно

This commit is contained in:
antoc0der 2023-12-17 19:03:50 +03:00
parent 793ecd010e
commit 86aee47092
9 changed files with 208 additions and 25 deletions

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Generics
{
internal class AirplaneCollectionInfo
{
public string Name { get; private set; }
public string Description { get; private set; }
public AirplaneCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(AirplaneCollectionInfo? other)
{
if (other != null)
{
return Name == other.Name;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,49 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Generics
{
internal class AirplaneCompareByColor : IComparer<DrawningAirplane?>
{
public int Compare(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityAirplane == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityAirplane.BodyColor.Name != y.EntityAirplane.BodyColor.Name)
{
return x.EntityAirplane.BodyColor.Name.CompareTo(y.EntityAirplane.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is DrawningAirplane)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningAirplaneWithRadar)
{
EntityAirplaneWithRadar EntityX = (EntityAirplaneWithRadar)x.EntityAirplane;
EntityAirplaneWithRadar EntityY = (EntityAirplaneWithRadar)y.EntityAirplane;
if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name)
{
return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name);
}
}
var speedCompare = x.EntityAirplane.Speed.CompareTo(y.EntityAirplane.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityAirplane.Weight.CompareTo(y.EntityAirplane.Weight);
}
}
}

View File

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

View File

@ -13,6 +13,7 @@ namespace ProjectAirplaneWithRadar.Generics
private readonly int _placeSizeWidth = 215; private readonly int _placeSizeWidth = 215;
private readonly int _placeSizeHeight = 90; private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public AirplanesGenericCollection(int picWidth, int picHeight) public AirplanesGenericCollection(int picWidth, int picHeight)
{ {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
@ -28,7 +29,7 @@ namespace ProjectAirplaneWithRadar.Generics
{ {
return false; return false;
} }
return collect?._collection.Insert(obj)??false; return collect?._collection.Insert(obj, new DrawningAirplanesEqutables()) ?? false;
} }
public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos) public static bool operator -(AirplanesGenericCollection<T, U> collect, int pos)
{ {

View File

@ -11,8 +11,8 @@ namespace ProjectAirplaneWithRadar.Generics
{ {
internal class AirplanesGenericStorage internal class AirplanesGenericStorage
{ {
readonly Dictionary<string, AirplanesGenericCollection<DrawningAirplane,DrawningObjectAirplane>> _airplanesStorages; readonly Dictionary<AirplaneCollectionInfo, AirplanesGenericCollection<DrawningAirplane,DrawningObjectAirplane>> _airplanesStorages;
public List<string> Keys => _airplanesStorages.Keys.ToList(); public List<AirplaneCollectionInfo> Keys => _airplanesStorages.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,7 +20,7 @@ namespace ProjectAirplaneWithRadar.Generics
private static readonly char _separatorForObject = ':'; private static readonly char _separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight) public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{ {
_airplanesStorages = new Dictionary<string,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>(); _airplanesStorages = new Dictionary<AirplaneCollectionInfo,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
@ -31,7 +31,7 @@ namespace ProjectAirplaneWithRadar.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<AirplaneCollectionInfo,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplanesStorages) AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplanesStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
@ -39,7 +39,7 @@ namespace ProjectAirplaneWithRadar.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)
{ {
@ -98,7 +98,7 @@ namespace ProjectAirplaneWithRadar.Generics
} }
} }
} }
_airplanesStorages.Add(record[0], collection); _airplanesStorages.Add(new AirplaneCollectionInfo (record[0], string.Empty), collection);
str = sr.ReadLine(); str = sr.ReadLine();
} while (str != null); } while (str != null);
} }
@ -106,21 +106,22 @@ namespace ProjectAirplaneWithRadar.Generics
} }
public void AddSet(string name) public void AddSet(string name)
{ {
_airplanesStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight)); _airplanesStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
} }
public void DelSet(string name) public void DelSet(string name)
{ {
if (!_airplanesStorages.ContainsKey(name)) if (!_airplanesStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty)))
return; return;
_airplanesStorages.Remove(name); _airplanesStorages.Remove(new AirplaneCollectionInfo(name, string.Empty));
} }
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind] public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind]
{ {
get get
{ {
if (_airplanesStorages.ContainsKey(ind)) AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty);
return _airplanesStorages[ind]; if (_airplanesStorages.ContainsKey(indObj))
return null; return _airplanesStorages[indObj];
return null;
} }
} }
} }

View File

@ -0,0 +1,59 @@
using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar.Generics
{
internal class DrawningAirplanesEqutables : IEqualityComparer<DrawningAirplane?>
{
public bool Equals(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityAirplane.Speed != y.EntityAirplane.Speed)
{
return false;
}
if (x.EntityAirplane.Weight != y.EntityAirplane.Weight)
{
return false;
}
if (x.EntityAirplane.BodyColor != y.EntityAirplane.BodyColor)
{
return false;
}
if (x is DrawningAirplaneWithRadar && y is DrawningAirplaneWithRadar)
{
EntityAirplaneWithRadar EntityX = (EntityAirplaneWithRadar)x.EntityAirplane;
EntityAirplaneWithRadar EntityY = (EntityAirplaneWithRadar)y.EntityAirplane;
if (EntityX.Radar != EntityY.Radar)
return false;
if (EntityX.DopBak != EntityY.DopBak)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirplane obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -71,7 +71,7 @@
groupBoxAirplaneWithRadar.Dock = DockStyle.Right; groupBoxAirplaneWithRadar.Dock = DockStyle.Right;
groupBoxAirplaneWithRadar.Location = new Point(650, 28); groupBoxAirplaneWithRadar.Location = new Point(650, 28);
groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar"; groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar";
groupBoxAirplaneWithRadar.Size = new Size(251, 464); groupBoxAirplaneWithRadar.Size = new Size(251, 522);
groupBoxAirplaneWithRadar.TabIndex = 1; groupBoxAirplaneWithRadar.TabIndex = 1;
groupBoxAirplaneWithRadar.TabStop = false; groupBoxAirplaneWithRadar.TabStop = false;
groupBoxAirplaneWithRadar.Text = "Инструменты"; groupBoxAirplaneWithRadar.Text = "Инструменты";
@ -183,14 +183,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26); SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1;
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26); LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
// //
@ -202,7 +202,7 @@
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(901, 492); ClientSize = new Size(901, 550);
Controls.Add(groupBoxAirplaneWithRadar); Controls.Add(groupBoxAirplaneWithRadar);
Controls.Add(pictureBoxAirplanesCollection); Controls.Add(pictureBoxAirplanesCollection);
Controls.Add(menuStrip1); Controls.Add(menuStrip1);

View File

@ -64,7 +64,7 @@ namespace ProjectAirplaneWithRadar
Log.Information($"Äîáàâëåí îáúåêò â êîëëåêöèþ {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); Log.Information($"Äîáàâëåí îáúåêò â êîëëåêöèþ {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); pictureBoxAirplanesCollection.Image = obj.ShowAirplanes();
} }
catch(StorageOverflowException ex) catch (StorageOverflowException ex)
{ {
Log.Warning($"Êîëëåêöèÿ {listBoxStorages.SelectedItem.ToString() ?? string.Empty} ïåðåïîëíåíà"); Log.Warning($"Êîëëåêöèÿ {listBoxStorages.SelectedItem.ToString() ?? string.Empty} ïåðåïîëíåíà");
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
@ -89,8 +89,8 @@ namespace ProjectAirplaneWithRadar
{ {
return; return;
} }
try try
{ {
int pos = Convert.ToInt32(maskedTextBoxNumber.Text); int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
@ -175,7 +175,7 @@ namespace ProjectAirplaneWithRadar
catch (Exception ex) catch (Exception ex)
{ {
Log.Warning("Íå óäàëîñü ñîõðàíèòü"); Log.Warning("Íå óäàëîñü ñîõðàíèòü");
MessageBox.Show($"Íå ñîõðàíèëîñü: {ex.Message}","Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Íå ñîõðàíèëîñü: {ex.Message}", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
@ -198,7 +198,7 @@ namespace ProjectAirplaneWithRadar
catch (Exception ex) catch (Exception ex)
{ {
Log.Warning("Íå óäàëîñü çàãðóçèòü"); Log.Warning("Íå óäàëîñü çàãðóçèòü");
MessageBox.Show($"Íå çàãðóçèëîñü: {ex.Message}","Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Íå çàãðóçèëîñü: {ex.Message}", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }

View File

@ -19,19 +19,25 @@ namespace ProjectAirplaneWithRadar.Generics
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public bool Insert(T airplane) public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public bool Insert(T airplane, IEqualityComparer<T>? equal = null)
{ {
if (_places.Count == _maxCount) if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);
Insert(airplane, 0); Insert(airplane, 0, equal);
return true; return true;
} }
public bool Insert(T airplane, int position) public bool Insert(T airplane, int position, IEqualityComparer<T>? equal = null)
{ {
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))
return false; return false;
if (equal != null)
{
if (_places.Contains(airplane, equal))
throw new ArgumentException(nameof(airplane));
}
_places.Insert(position, airplane); _places.Insert(position, airplane);
return true; return true;
} }