Начала и закончила 8 лабу

This commit is contained in:
ALINA_KURBANOVA 2023-12-29 22:03:17 +04:00
parent 34fb706739
commit f03d4d88a4
10 changed files with 274 additions and 105 deletions

View File

@ -0,0 +1,53 @@
using System.Diagnostics.CodeAnalysis;
using WarmlyLocomotive.DrawningObjects;
using WarmlyLocomotive.Entities;
namespace WarmlyLocomotive
{
internal class DrawingWarmlyLocomotiveEqutables : IEqualityComparer<DrawningWarmlyLocomotive?>
{
public bool Equals(DrawningWarmlyLocomotive? x, DrawningWarmlyLocomotive? y)
{
if (x == null || x.EntityWarmlyLocomotive == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityWarmlyLocomotive == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityWarmlyLocomotive.Speed != y.EntityWarmlyLocomotive.Speed)
{
return false;
}
if (x.EntityWarmlyLocomotive.Weight != y.EntityWarmlyLocomotive.Weight)
{
return false;
}
if (x.EntityWarmlyLocomotive.BodyColor != y.EntityWarmlyLocomotive.BodyColor)
{
return false;
}
if (x is DrawningWarmlyLocomotiveWithTrumpet && y is DrawningWarmlyLocomotiveWithTrumpet)
{
EntityWarmlyLocomotiveWithTrumpet EntityX = (EntityWarmlyLocomotiveWithTrumpet)x.EntityWarmlyLocomotive;
EntityWarmlyLocomotiveWithTrumpet EntityY = (EntityWarmlyLocomotiveWithTrumpet)y.EntityWarmlyLocomotive;
if (EntityX.Trumpet != EntityY.Trumpet)
return false;
if (EntityX.Luggage != EntityY.Luggage)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningWarmlyLocomotive? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarmlyLocomotive.Generics;
namespace WarmlyLocomotive
{
internal class DrawningEqutables : IEqualityComparer<WarmlyLocomotiveGenericStorage?>
{
public bool Equals(WarmlyLocomotiveGenericStorage? x, WarmlyLocomotiveGenericStorage? y)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.Keys.Count != y.Keys.Count)
{
return false;
}
return true;
}
public int GetHashCode([DisallowNull] WarmlyLocomotiveGenericStorage? obj)
{
throw new NotImplementedException();
}
}
}

View File

@ -47,6 +47,8 @@
file = new ToolStripMenuItem(); file = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem(); SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem(); LoadToolStripMenuItem = new ToolStripMenuItem();
buttonSortByType = new Button();
buttonSortByColor = new Button();
panelCollectionWarmlyLocomotive.SuspendLayout(); panelCollectionWarmlyLocomotive.SuspendLayout();
panel1.SuspendLayout(); panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollectionWarmlyLocomotive).BeginInit();
@ -90,6 +92,8 @@
// //
// panel1 // panel1
// //
panel1.Controls.Add(buttonSortByColor);
panel1.Controls.Add(buttonSortByType);
panel1.Controls.Add(buttonDelObject); panel1.Controls.Add(buttonDelObject);
panel1.Controls.Add(textBoxStorageName); panel1.Controls.Add(textBoxStorageName);
panel1.Controls.Add(listBoxStorages); panel1.Controls.Add(listBoxStorages);
@ -97,7 +101,7 @@
panel1.Location = new Point(6, 34); panel1.Location = new Point(6, 34);
panel1.Margin = new Padding(6); panel1.Margin = new Padding(6);
panel1.Name = "panel1"; panel1.Name = "panel1";
panel1.Size = new Size(266, 446); panel1.Size = new Size(293, 526);
panel1.TabIndex = 4; panel1.TabIndex = 4;
// //
// buttonDelObject // buttonDelObject
@ -172,7 +176,7 @@
// //
// buttonAdd // buttonAdd
// //
buttonAdd.Location = new Point(11, 527); buttonAdd.Location = new Point(7, 572);
buttonAdd.Margin = new Padding(6); buttonAdd.Margin = new Padding(6);
buttonAdd.Name = "buttonAdd"; buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(266, 47); buttonAdd.Size = new Size(266, 47);
@ -200,7 +204,7 @@
menuStrip.Items.AddRange(new ToolStripItem[] { file }); menuStrip.Items.AddRange(new ToolStripItem[] { file });
menuStrip.Location = new Point(0, 0); menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip"; menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(1528, 42); menuStrip.Size = new Size(1528, 40);
menuStrip.TabIndex = 3; menuStrip.TabIndex = 3;
menuStrip.Text = "menuStrip1"; menuStrip.Text = "menuStrip1";
// //
@ -208,23 +212,43 @@
// //
file.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); file.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
file.Name = "file"; file.Name = "file";
file.Size = new Size(90, 38); file.Size = new Size(90, 36);
file.Text = "Файл"; file.Text = "Файл";
// //
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(359, 44); SaveToolStripMenuItem.Size = new Size(263, 44);
SaveToolStripMenuItem.Text = "Сохранить"; SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(359, 44); LoadToolStripMenuItem.Size = new Size(263, 44);
LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
// //
// buttonSortByType
//
buttonSortByType.Location = new Point(5, 434);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(258, 41);
buttonSortByType.TabIndex = 4;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(3, 477);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(265, 46);
buttonSortByColor.TabIndex = 5;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// FormWarmlyLocomotiveCollection // FormWarmlyLocomotiveCollection
// //
AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleDimensions = new SizeF(13F, 32F);
@ -270,5 +294,7 @@
private ToolStripMenuItem file; private ToolStripMenuItem file;
private ToolStripMenuItem SaveToolStripMenuItem; private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@ -5,16 +5,17 @@ using System.Windows.Forms;
using WarmlyLocomotive.DrawningObjects; using WarmlyLocomotive.DrawningObjects;
using Serilog; using Serilog;
namespace WarmlyLocomotive namespace WarmlyLocomotive
{ {
public partial class FormWarmlyLocomotiveCollection : Form public partial class FormWarmlyLocomotiveCollection : Form
{ {
private readonly WarmlyLocomotivesGenericStorage _storage; private readonly WarmlyLocomotiveGenericStorage _storage;
public FormWarmlyLocomotiveCollection() public FormWarmlyLocomotiveCollection()
{ {
InitializeComponent(); InitializeComponent();
_storage = new WarmlyLocomotivesGenericStorage(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height); _storage = new WarmlyLocomotiveGenericStorage(pictureBoxCollectionWarmlyLocomotive.Width, pictureBoxCollectionWarmlyLocomotive.Height);
} }
private void ReloadObjects() private void ReloadObjects()
{ {
@ -22,7 +23,7 @@ namespace WarmlyLocomotive
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 if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count)) >= listBoxStorages.Items.Count))
@ -95,6 +96,11 @@ namespace WarmlyLocomotive
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("Добавляемый объект уже сущесвует в коллекции");
}
}); });
Action<Color>? ColorDelegate = new((ship) => Action<Color>? ColorDelegate = new((ship) =>
{ {
@ -127,6 +133,7 @@ namespace WarmlyLocomotive
Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}"); Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives(); pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
} }
catch (WarmlyLocomotiveNotFoundException ex) catch (WarmlyLocomotiveNotFoundException ex)
{ {
Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
@ -137,6 +144,7 @@ namespace WarmlyLocomotive
Log.Warning($"Было введено не число"); Log.Warning($"Было введено не число");
MessageBox.Show("Введите число"); MessageBox.Show("Введите число");
} }
} }
private void buttonreFreshCollection_Click(object sender, EventArgs e) private void buttonreFreshCollection_Click(object sender, EventArgs e)
{ {
@ -194,5 +202,22 @@ namespace WarmlyLocomotive
} }
} }
} }
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareWarmlyLocomotive(new WarmlyLocomotiveCompareByColor());
private void buttonSortByType_Click(object sender, EventArgs e) => CompareWarmlyLocomotive(new WarmlyLocomotiveCompareByType());
private void CompareWarmlyLocomotive(IComparer<DrawningWarmlyLocomotive?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollectionWarmlyLocomotive.Image = obj.ShowWarmlyLocomotives();
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles; using System.Windows.Forms.VisualStyles;
using WarmlyLocomotive.Exceptions; using WarmlyLocomotive.Exceptions;
using System; using System;
using System.Numerics;
namespace WarmlyLocomotive.Generics namespace WarmlyLocomotive.Generics
@ -23,28 +24,32 @@ namespace WarmlyLocomotive.Generics
_places = new List<T?>(count); _places = new List<T?>(count);
countMax = count; countMax = count;
} }
public bool Insert(T ship) public bool Insert(T ship, IEqualityComparer<T>? equal = null)
{ {
if (_places.Count == countMax) if (_places.Count == countMax)
throw new StorageOverflowException(countMax); throw new StorageOverflowException(countMax);
Insert(ship, 0); Insert(ship, 0, equal);
return true; return true;
} }
public bool Insert(T ship, int position) public void Insert(T ship, int position, IEqualityComparer<T>? equal = null)
{ {
if (_places.Count == countMax) if (_places.Count == countMax)
throw new StorageOverflowException(countMax); throw new StorageOverflowException(countMax);
if (!(position >= 0 && position <= Count && _places.Count < countMax)) if (!(position >= 0 && position <= Count))
return false; throw new Exception($"Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(ship, equal))
throw new ArgumentException(nameof(ship));
}
_places.Insert(position, ship); _places.Insert(position, ship);
return true;
} }
public bool Remove(int position) public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public void Remove(int position)
{ {
if (!(position >= 0 && position < Count)) if (!(position >= 0 && position < Count))
throw new WarmlyLocomotiveNotFoundException(position); throw new WarmlyLocomotiveNotFoundException(position);
_places.RemoveAt(position); _places.RemoveAt(position);
return true;
} }
public T? this[int position] public T? this[int position]
{ {

View File

@ -0,0 +1,23 @@
using WarmlyLocomotive.DrawningObjects;
namespace WarmlyLocomotive
{
internal class WarmlyLocomotiveCompareByColor : IComparer<DrawningWarmlyLocomotive?>
{
public int Compare(DrawningWarmlyLocomotive? x, DrawningWarmlyLocomotive? y)
{
if (x == null || x.EntityWarmlyLocomotive == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityWarmlyLocomotive == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityWarmlyLocomotive.BodyColor.Name != y.EntityWarmlyLocomotive.BodyColor.Name)
{
return x.EntityWarmlyLocomotive.BodyColor.Name.CompareTo(y.EntityWarmlyLocomotive.BodyColor.Name);
}
var speedCompare = x.EntityWarmlyLocomotive.Speed.CompareTo(y.EntityWarmlyLocomotive.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityWarmlyLocomotive.Weight.CompareTo(y.EntityWarmlyLocomotive.Weight);
}
}
}

View File

@ -0,0 +1,22 @@
using WarmlyLocomotive.DrawningObjects;
namespace WarmlyLocomotive
{
internal class WarmlyLocomotiveCompareByType : IComparer<DrawningWarmlyLocomotive?>
{
public int Compare(DrawningWarmlyLocomotive? x, DrawningWarmlyLocomotive? y)
{
if (x == null || x.EntityWarmlyLocomotive == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityWarmlyLocomotive == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityWarmlyLocomotive.Speed.CompareTo(y.EntityWarmlyLocomotive.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityWarmlyLocomotive.Weight.CompareTo(y.EntityWarmlyLocomotive.Weight);
}
}
}

View File

@ -0,0 +1,25 @@
using WarmlyLocomotive.Generics;
namespace WarmlyLocomotive
{
internal class WarmlyLocomotivesCollectionInfo : IEquatable<WarmlyLocomotivesCollectionInfo?>
{
public string Name { get; private set; }
public string Description { get; private set; }
public WarmlyLocomotivesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(WarmlyLocomotivesCollectionInfo? other)
{
if (other == null)
return false;
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -1,41 +1,29 @@
using WarmlyLocomotive.DrawningObjects; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarmlyLocomotive.Generics;
using WarmlyLocomotive.DrawningObjects;
using WarmlyLocomotive.Entities;
using WarmlyLocomotive.MovementStrategy; using WarmlyLocomotive.MovementStrategy;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WarmlyLocomotive;
namespace WarmlyLocomotive.Generics namespace WarmlyLocomotive.Generics
{ {
internal class WarmlyLocomotivesGenericCollection<T, U> internal class WarmlyLocomotiveGenericCollection<T, U>
where T : DrawningWarmlyLocomotive where T : DrawningWarmlyLocomotive
where U : IMoveableObject where U : IMoveableObject
{ {
/// <summary>
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetWarmlyLocomotives => _collection.GetWarmlyLocomotive();
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth; private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight; private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 270; private readonly int _placeSizeWidth = 270;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 100; private readonly int _placeSizeHeight = 100;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
/// <summary> public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// Конструктор public WarmlyLocomotiveGenericCollection(int picWidth, int picHeight)
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public WarmlyLocomotivesGenericCollection(int picWidth, int picHeight)
{ {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
@ -43,48 +31,23 @@ namespace WarmlyLocomotive.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
/// <summary> public static bool operator +(WarmlyLocomotiveGenericCollection<T, U> collect, T? obj)
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool operator +(WarmlyLocomotivesGenericCollection<T, U> collect, T? obj)
{ {
if (obj == null) if (obj == null || collect == null)
{
return false; return false;
} collect._collection.Insert(obj, new DrawingWarmlyLocomotiveEqutables());
return (bool)collect?._collection.Insert(obj); return true;
} }
/// <summary> public static T? operator -(WarmlyLocomotiveGenericCollection<T, U> collect, int pos)
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(WarmlyLocomotivesGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection[pos]; T? obj = collect._collection[pos];
if (obj != null) collect._collection.Remove(pos);
{ return obj;
collect._collection.Remove(pos);
}
return false;
} }
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection[pos]?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowWarmlyLocomotives() public Bitmap ShowWarmlyLocomotives()
{ {
Bitmap bmp = new(_pictureWidth, _pictureHeight); Bitmap bmp = new(_pictureWidth, _pictureHeight);
@ -93,10 +56,6 @@ namespace WarmlyLocomotive.Generics
DrawObjects(gr); DrawObjects(gr);
return bmp; return bmp;
} }
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g) private void DrawBackground(Graphics g)
{ {
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
@ -104,7 +63,7 @@ namespace WarmlyLocomotive.Generics
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j) 1; ++j)
{ {//линия разметки места
g.DrawLine(pen, i * _placeSizeWidth, j * g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight); _placeSizeHeight);
@ -113,10 +72,6 @@ namespace WarmlyLocomotive.Generics
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
} }
} }
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int i = 0; int i = 0;
@ -131,5 +86,6 @@ namespace WarmlyLocomotive.Generics
i++; i++;
} }
} }
public IEnumerable<T?> GetWarmlyLocomotive => _collection.GetWarmlyLocomotive();
} }
} }

View File

@ -5,43 +5,42 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WarmlyLocomotive.DrawningObjects; using WarmlyLocomotive.DrawningObjects;
using WarmlyLocomotive.MovementStrategy; using WarmlyLocomotive.MovementStrategy;
using System.Xml.Linq;
namespace WarmlyLocomotive.Generics namespace WarmlyLocomotive.Generics
{ {
internal class WarmlyLocomotivesGenericStorage internal class WarmlyLocomotiveGenericStorage
{ {
readonly Dictionary<string, WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, readonly Dictionary<WarmlyLocomotivesCollectionInfo, WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>> _warmlylocomotiveStorages;
DrawningObjectWarmlyLocomotive>> _warmlylocomotiveStorages; public List<WarmlyLocomotivesCollectionInfo> Keys => _warmlylocomotiveStorages.Keys.ToList();
public List<string> Keys => _warmlylocomotiveStorages.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 = '|';
private readonly char _separatorRecords = ';'; private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':'; private static readonly char _separatorForObject = ':';
public WarmlyLocomotivesGenericStorage(int pictureWidth, int pictureHeight) public WarmlyLocomotiveGenericStorage(int pictureWidth, int pictureHeight)
{ {
_warmlylocomotiveStorages = new Dictionary<string, WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>>(); _warmlylocomotiveStorages = new Dictionary<WarmlyLocomotivesCollectionInfo, WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
public void AddSet(string name) public void AddSet(string name)
{ {
_warmlylocomotiveStorages.Add(name, new WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>(_pictureWidth, _pictureHeight)); _warmlylocomotiveStorages.Add(new WarmlyLocomotivesCollectionInfo(name, string.Empty), new WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>(_pictureWidth, _pictureHeight));
} }
public void DelSet(string name) public void DelSet(string name)
{ {
if (!_warmlylocomotiveStorages.ContainsKey(name)) if (!_warmlylocomotiveStorages.ContainsKey(new WarmlyLocomotivesCollectionInfo(name, string.Empty)))
return; return;
_warmlylocomotiveStorages.Remove(name); _warmlylocomotiveStorages.Remove(new WarmlyLocomotivesCollectionInfo(name, string.Empty));
} }
public WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>? this[string ind] public WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>? this[string ind]
{ {
get get
{ {
if (_warmlylocomotiveStorages.ContainsKey(ind)) WarmlyLocomotivesCollectionInfo index = new WarmlyLocomotivesCollectionInfo(ind, string.Empty);
{ if (_warmlylocomotiveStorages.ContainsKey(index))
return _warmlylocomotiveStorages[ind]; return _warmlylocomotiveStorages[index];
}
return null; return null;
} }
} }
@ -52,14 +51,14 @@ namespace WarmlyLocomotive.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string,WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>> record in _warmlylocomotiveStorages) foreach (KeyValuePair<WarmlyLocomotivesCollectionInfo,WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive>> record in _warmlylocomotiveStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawningWarmlyLocomotive? elem in record.Value.GetWarmlyLocomotives) foreach (DrawningWarmlyLocomotive? elem in record.Value.GetWarmlyLocomotive)
{ {
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)
@ -105,7 +104,7 @@ namespace WarmlyLocomotive.Generics
str = sr.ReadLine(); str = sr.ReadLine();
continue; continue;
} }
WarmlyLocomotivesGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive> collection = new(_pictureWidth, _pictureHeight); WarmlyLocomotiveGenericCollection<DrawningWarmlyLocomotive, DrawningObjectWarmlyLocomotive> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
@ -118,7 +117,7 @@ namespace WarmlyLocomotive.Generics
} }
} }
} }
_warmlylocomotiveStorages.Add(record[0], collection); _warmlylocomotiveStorages.Add(new WarmlyLocomotivesCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine(); str = sr.ReadLine();
} while (str != null); } while (str != null);
@ -126,4 +125,4 @@ namespace WarmlyLocomotive.Generics
return true; return true;
} }
} }
} }