Это супер пупер сохранение только для тебя)

This commit is contained in:
Extrimal 2023-12-12 23:42:44 +03:00
parent 57e628eca5
commit 168c971da1
9 changed files with 244 additions and 27 deletions

View File

@ -0,0 +1,34 @@
using AircraftCarrier.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.Drawnings;
namespace AircraftCarrier.Generics
{
internal class AircraftCompareByColor : IComparer<DrawningAircraft?>
{
public int Compare(DrawningAircraft? x, DrawningAircraft? y)
{
if (x == null || x.EntityAircraft == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityAircraft == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityAircraft.BodyColor.Name != y.EntityAircraft.BodyColor.Name)
{
return x.EntityAircraft.BodyColor.Name.CompareTo(y.EntityAircraft.BodyColor.Name);
}
var speedCompare = x.EntityAircraft.Speed.CompareTo(y.EntityAircraft.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityAircraft.Weight.CompareTo(y.EntityAircraft.Weight);
}
}
}

View File

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

View File

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

View File

@ -12,6 +12,8 @@ namespace AircraftCarrier.Generics
where T : DrawningAircraft
where U : IMoveableObject
{
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// Получение объектов коллекции
public IEnumerable<T?> GetCars => _collection.GetAircrafts();
/// Ширина окна прорисовки
@ -41,7 +43,7 @@ namespace AircraftCarrier.Generics
{
return -1;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawiningAircraftEqutables());
}
/// Перегрузка оператора вычитания
public static bool operator -(AircraftsGenericCollection<T, U> collect, int pos)

View File

@ -12,10 +12,10 @@ namespace AircraftCarrier.Generics
internal class AircraftsGenericStorage
{
/// Словарь (хранилище)
readonly Dictionary<string, AircraftsGenericCollection<DrawningAircraft,
readonly Dictionary<AircraftsCollectionInfo, AircraftsGenericCollection<DrawningAircraft,
DrawningObjectAircraft>> _AircraftStorages;
/// Возвращение списка названий наборов
public List<string> Keys => _AircraftStorages.Keys.ToList();
public List<AircraftsCollectionInfo> Keys => _AircraftStorages.Keys.ToList();
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
@ -29,7 +29,7 @@ namespace AircraftCarrier.Generics
/// Конструктор
public AircraftsGenericStorage(int pictureWidth, int pictureHeight)
{
_AircraftStorages = new Dictionary<string,
_AircraftStorages = new Dictionary<AircraftsCollectionInfo,
AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -37,17 +37,17 @@ namespace AircraftCarrier.Generics
/// Добавление набора
public void AddSet(string name)
{
if (!_AircraftStorages.ContainsKey(name))
if (!_AircraftStorages.ContainsKey(new AircraftsCollectionInfo(name, string.Empty)))
{
_AircraftStorages.Add(name, new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(_pictureWidth, _pictureHeight));
_AircraftStorages.Add(new AircraftsCollectionInfo(name, string.Empty), new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(_pictureWidth, _pictureHeight));
}
}
/// Удаление набора
public void DelSet(string name)
{
if (_AircraftStorages.ContainsKey(name))
if (_AircraftStorages.ContainsKey(new AircraftsCollectionInfo(name, string.Empty)))
{
_AircraftStorages.Remove(name);
_AircraftStorages.Remove(new AircraftsCollectionInfo(name, string.Empty));
}
}
/// Доступ к набору
@ -56,9 +56,10 @@ namespace AircraftCarrier.Generics
{
get
{
if (_AircraftStorages.ContainsKey(ind))
AircraftsCollectionInfo indObj = new AircraftsCollectionInfo(ind, string.Empty);
if (_AircraftStorages.ContainsKey(indObj))
{
return _AircraftStorages[ind];
return _AircraftStorages[indObj];
}
return null;
}
@ -71,7 +72,7 @@ namespace AircraftCarrier.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<AircraftsCollectionInfo,
AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>> record in _AircraftStorages)
{
StringBuilder records = new();
@ -79,7 +80,7 @@ namespace AircraftCarrier.Generics
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
@ -131,7 +132,7 @@ namespace AircraftCarrier.Generics
}
}
}
_AircraftStorages.Add(record[0], collection);
_AircraftStorages.Add(new AircraftsCollectionInfo(record[0], string.Empty), collection);
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.Drawnings;
using AircraftCarrier.Entities;
using System.Diagnostics.CodeAnalysis;
using AircraftCarrier.DrawningObjects;
namespace AircraftCarrier.Generics
{
internal class DrawiningAircraftEqutables : IEqualityComparer<DrawningAircraft?>
{
public bool Equals(DrawningAircraft? x, DrawningAircraft? y)
{
if (x == null || x.EntityAircraft == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAircraft == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityAircraft.Speed != y.EntityAircraft.Speed)
{
return false;
}
if (x.EntityAircraft.Weight != y.EntityAircraft.Weight)
{
return false;
}
if (x.EntityAircraft.BodyColor != y.EntityAircraft.BodyColor)
{
return false;
}
if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier)
{
EntityAircraftCarrier EntityX = (EntityAircraftCarrier)x.EntityAircraft;
EntityAircraftCarrier EntityY = (EntityAircraftCarrier)y.EntityAircraft;
if (EntityX.Runway != EntityY.Runway)
return false;
if (EntityX.Cabin != EntityY.Cabin)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAircraft obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -47,6 +47,8 @@
loadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
ButtonSortByType = new Button();
ButtonSortByColor = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
PanelTools.SuspendLayout();
PanelSets.SuspendLayout();
@ -64,6 +66,8 @@
// PanelTools
//
PanelTools.BorderStyle = BorderStyle.FixedSingle;
PanelTools.Controls.Add(ButtonSortByColor);
PanelTools.Controls.Add(ButtonSortByType);
PanelTools.Controls.Add(labelTools);
PanelTools.Controls.Add(PanelSets);
PanelTools.Controls.Add(ButtonRefreshCollection);
@ -144,7 +148,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(36, 572);
ButtonRefreshCollection.Location = new Point(36, 633);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(168, 41);
ButtonRefreshCollection.TabIndex = 3;
@ -154,7 +158,7 @@
//
// ButtonRemoveAircraft
//
ButtonRemoveAircraft.Location = new Point(36, 497);
ButtonRemoveAircraft.Location = new Point(36, 586);
ButtonRemoveAircraft.Name = "ButtonRemoveAircraft";
ButtonRemoveAircraft.Size = new Size(168, 41);
ButtonRemoveAircraft.TabIndex = 2;
@ -164,7 +168,7 @@
//
// MaskedTextBoxNumber
//
MaskedTextBoxNumber.Location = new Point(57, 438);
MaskedTextBoxNumber.Location = new Point(57, 553);
MaskedTextBoxNumber.Name = "MaskedTextBoxNumber";
MaskedTextBoxNumber.Size = new Size(125, 27);
MaskedTextBoxNumber.TabIndex = 1;
@ -172,7 +176,7 @@
//
// ButtonAddAircraft
//
ButtonAddAircraft.Location = new Point(36, 391);
ButtonAddAircraft.Location = new Point(36, 506);
ButtonAddAircraft.Name = "ButtonAddAircraft";
ButtonAddAircraft.Size = new Size(168, 41);
ButtonAddAircraft.TabIndex = 0;
@ -200,14 +204,14 @@
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.Size = new Size(224, 26);
saveToolStripMenuItem.Size = new Size(166, 26);
saveToolStripMenuItem.Text = "Сохранить";
saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.Size = new Size(224, 26);
loadToolStripMenuItem.Size = new Size(166, 26);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -220,6 +224,26 @@
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(36, 393);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(168, 41);
ButtonSortByType.TabIndex = 6;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(36, 450);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(168, 41);
ButtonSortByColor.TabIndex = 7;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormAircraftCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -262,5 +286,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -33,7 +33,7 @@ namespace AircraftCarrier
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
listBoxStorage.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
@ -114,11 +114,16 @@ namespace AircraftCarrier
MessageBox.Show("Неудачная попытка добавить объект");
}
}
catch(ApplicationException ex)
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Неудачная попытка добавить объект: {ex.Message}");
}
catch (ArgumentException ex)
{
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
_logger.LogWarning($"Добавляемый объект уже существует в коллекции {ex.Message}");
}
}
private void ButtonRemoveAircraft_Click(object sender, EventArgs e)
{
@ -149,7 +154,7 @@ namespace AircraftCarrier
}
else
{
MessageBox.Show("Неудачная попытка удалить объект");
MessageBox.Show("Неудачная попытка удалить объект");
}
}
catch (AircraftNotFoundException ex)
@ -211,5 +216,24 @@ namespace AircraftCarrier
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAircrafts(new AircraftCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAircrafts(new AircraftCompareByColor());
private void CompareAircrafts(IComparer<DrawningAircraft?> comparer)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowAircrafts();
}
}
}

View File

@ -21,20 +21,27 @@ namespace AircraftCarrier.Generics
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// Добавление объекта в набор
public int Insert(T Aircraft)
public int Insert(T aircraft, IEqualityComparer<T?>? equal = null)
{
return Insert(Aircraft, 0);
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
return Insert(aircraft, 0, equal);
}
/// Добавление объекта в набор на конкретную позицию
public int Insert(T Aircraft, int position)
public int Insert(T aircraft, int position, IEqualityComparer<T?>? equal= null)
{
if (position < 0 || position > Count)
throw new AircraftNotFoundException("Impossible to insert");
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
_places.Insert(position, Aircraft);
if (equal != null)
{
if (_places.Contains(aircraft, equal))
throw new ArgumentException(nameof(aircraft));
}
_places.Insert(position, aircraft);
return position;
}
/// Удаление объекта из набора с конкретной позиции