PIbd-22_Kamcharova_K.A._lab8

This commit is contained in:
MayDayR 2023-12-29 22:19:11 +03:00
parent 0efbdf50ab
commit d681cb8414
15 changed files with 254 additions and 59 deletions

View File

@ -90,35 +90,23 @@ namespace DoubleDeckerbus.Drawing
} }
public virtual void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush additionalBrush = new Brush additionalBrush = new
SolidBrush(EntityBus.BodyColor); SolidBrush(EntityBus.BodyColor);
g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 52, 21, 20); g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 52, 21, 20);
g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 71, 30, 27); g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 71, 30, 27);
g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 52, 137, 46); g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 52, 137, 46);
Brush brBlue = new SolidBrush(Color.LightBlue); Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 55, 15, 15);
g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 55, 15, 15);
g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 55, 15, 15);
g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 55, 15, 15);
g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 55, 15, 15);
g.DrawLine(pen, _startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); g.DrawLine(pen, _startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98);
g.DrawLine(pen, _startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); g.DrawLine(pen, _startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98);
Brush gr = new SolidBrush(Color.Gray); Brush gr = new SolidBrush(Color.Gray);
g.FillEllipse(additionalBrush, _startPosX + 23, _startPosY + 88, 25, 25); g.FillEllipse(additionalBrush, _startPosX + 23, _startPosY + 88, 25, 25);
g.FillEllipse(additionalBrush, _startPosX + 123, _startPosY + 88, 25, 25); g.FillEllipse(additionalBrush, _startPosX + 123, _startPosY + 88, 25, 25);
g.FillEllipse(gr, _startPosX + 25, _startPosY + 90, 21, 21); g.FillEllipse(gr, _startPosX + 25, _startPosY + 90, 21, 21);
g.FillEllipse(gr, _startPosX + 125, _startPosY + 90, 21, 21); g.FillEllipse(gr, _startPosX + 125, _startPosY + 90, 21, 21);
} }
@ -137,7 +125,6 @@ namespace DoubleDeckerbus.Drawing
_ => false, _ => false,
}; };
} }
public void ChangeBordersPicture(int width, int height) public void ChangeBordersPicture(int width, int height)
{ {
_pictureWidth = width; _pictureWidth = width;

View File

@ -27,16 +27,12 @@ namespace DoubleDeckerbus.Drawing
Pen additionalPen = new(doubleDeckerBus.AddColor); Pen additionalPen = new(doubleDeckerBus.AddColor);
Brush additionalBrush = new SolidBrush(doubleDeckerBus.AddColor); Brush additionalBrush = new SolidBrush(doubleDeckerBus.AddColor);
base.DrawTransport(g); base.DrawTransport(g);
if (doubleDeckerBus.IsSecondFloor) if (doubleDeckerBus.IsSecondFloor)
{ {
Brush additionalBrush2 = new SolidBrush(doubleDeckerBus.AddColor); Brush additionalBrush2 = new SolidBrush(doubleDeckerBus.AddColor);
Brush brBlue = new SolidBrush(Color.LightBlue); Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(additionalBrush2, _startPosX + 7, _startPosY + 12, 172, 40); g.FillRectangle(additionalBrush2, _startPosX + 7, _startPosY + 12, 172, 40);
g.DrawLine(pen, _startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); g.DrawLine(pen, _startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36);
g.FillRectangle(brBlue, _startPosX + 15, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 15, _startPosY + 15, 15, 15);
g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 15, 15, 15);
g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 15, 15, 15);

View File

@ -45,4 +45,3 @@ namespace DoubleDeckerbus.Drawing
} }
} }
} }

View File

@ -12,7 +12,6 @@ namespace DoubleDeckerbus.Entities
public Color AddColor { get; private set; } public Color AddColor { get; private set; }
public bool IsSecondFloor { get; private set; } public bool IsSecondFloor { get; private set; }
public bool IsStairs { get; private set; } public bool IsStairs { get; private set; }
public EntityDoubleDeckerbus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondfloor, bool stairs) : base(speed, weight, bodyColor) public EntityDoubleDeckerbus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondfloor, bool stairs) : base(speed, weight, bodyColor)
{ {
AddColor = additionalColor; AddColor = additionalColor;

View File

@ -48,11 +48,13 @@ namespace DoubleDeckerbus
EventAddBus += ev; EventAddBus += ev;
} }
} }
private void LabelObject_MouseDown(object sender, MouseEventArgs e) private void LabelObject_MouseDown(object sender, MouseEventArgs e)
{ {
(sender as Label)?.DoDragDrop((sender as Label)?.Name, (sender as Label)?.DoDragDrop((sender as Label)?.Name,
DragDropEffects.Move | DragDropEffects.Copy); DragDropEffects.Move | DragDropEffects.Copy);
} }
private void PanelObject_DragEnter(object sender, DragEventArgs e) private void PanelObject_DragEnter(object sender, DragEventArgs e)
{ {
if (e.Data?.GetDataPresent(DataFormats.Text) ?? false) if (e.Data?.GetDataPresent(DataFormats.Text) ?? false)
@ -92,7 +94,6 @@ namespace DoubleDeckerbus
(sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, (sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor,
DragDropEffects.Move | DragDropEffects.Copy); DragDropEffects.Move | DragDropEffects.Copy);
} }
private void LabelMainColor_DragEnter(object sender, DragEventArgs e) private void LabelMainColor_DragEnter(object sender, DragEventArgs e)
{ {
if (e.Data.GetDataPresent(typeof(Color)) && _bus != null) if (e.Data.GetDataPresent(typeof(Color)) && _bus != null)

View File

@ -46,6 +46,8 @@
this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.Sort_color_button = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.menuStrip.SuspendLayout(); this.menuStrip.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -129,7 +131,7 @@
this.listBoxStorages.Location = new System.Drawing.Point(850, 160); this.listBoxStorages.Location = new System.Drawing.Point(850, 160);
this.listBoxStorages.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.listBoxStorages.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.listBoxStorages.Name = "listBoxStorages"; this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(132, 109); this.listBoxStorages.Size = new System.Drawing.Size(132, 34);
this.listBoxStorages.TabIndex = 8; this.listBoxStorages.TabIndex = 8;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged); this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged);
// //
@ -187,14 +189,14 @@
// saveToolStripMenuItem // saveToolStripMenuItem
// //
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.saveToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
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(180, 22); this.loadToolStripMenuItem.Size = new System.Drawing.Size(133, 22);
this.loadToolStripMenuItem.Text = "Загрузить"; this.loadToolStripMenuItem.Text = "Загрузить";
this.loadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.loadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
@ -207,11 +209,33 @@
this.openFileDialog.FileName = "openFileDialog1"; this.openFileDialog.FileName = "openFileDialog1";
this.openFileDialog.Filter = "txt file | *.txt"; this.openFileDialog.Filter = "txt file | *.txt";
// //
// button1
//
this.button1.Location = new System.Drawing.Point(850, 199);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(131, 23);
this.button1.TabIndex = 13;
this.button1.Text = "Сортировка по типу";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// Sort_color_button
//
this.Sort_color_button.Location = new System.Drawing.Point(845, 228);
this.Sort_color_button.Name = "Sort_color_button";
this.Sort_color_button.Size = new System.Drawing.Size(144, 23);
this.Sort_color_button.TabIndex = 14;
this.Sort_color_button.Text = "Сортировка по цвету";
this.Sort_color_button.UseVisualStyleBackColor = true;
this.Sort_color_button.Click += new System.EventHandler(this.Sort_Color_button_Click);
//
// FormDoubleDeckerbusCollection // FormDoubleDeckerbusCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1003, 504); this.ClientSize = new System.Drawing.Size(1003, 504);
this.Controls.Add(this.Sort_color_button);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBoxStorageName); this.Controls.Add(this.textBoxStorageName);
this.Controls.Add(this.buttonDeleteStorage); this.Controls.Add(this.buttonDeleteStorage);
this.Controls.Add(this.buttonAddStorage); this.Controls.Add(this.buttonAddStorage);
@ -255,5 +279,7 @@
private ToolStripMenuItem loadToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private Button button1;
private Button Sort_color_button;
} }
} }

View File

@ -12,8 +12,7 @@ using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Move_Strategy; using DoubleDeckerbus.Move_Strategy;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using DoubleDeckerbus.Exceptions; using DoubleDeckerbus.Exceptions;
using DoubleDeckerbus.Generics;
namespace DoubleDeckerbus namespace DoubleDeckerbus
{ {
@ -27,13 +26,29 @@ namespace DoubleDeckerbus
_storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
_logger = logger; _logger = logger;
} }
private void buttonSortByType_Click(object sender, EventArgs e) => CompareBus(new BusCompareByType());
private void Sort_Color_button_Click(object sender, EventArgs e) => CompareBus(new BusCompareByColor());
private void CompareBus(IComparer<DrawingBus?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowBus();
}
private void ReloadObjects() private void ReloadObjects()
{ {
int index = listBoxStorages.SelectedIndex; int index = listBoxStorages.SelectedIndex;
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))
{ {
@ -43,7 +58,6 @@ namespace DoubleDeckerbus
{ {
listBoxStorages.SelectedIndex = index; listBoxStorages.SelectedIndex = index;
} }
} }
private void buttonAddStorage_Click(object sender, EventArgs e) private void buttonAddStorage_Click(object sender, EventArgs e)
{ {
@ -74,8 +88,6 @@ namespace DoubleDeckerbus
_logger.LogInformation($"Удалён набор: {name}"); _logger.LogInformation($"Удалён набор: {name}");
} }
} }
private void buttonAddBus_Click(object sender, EventArgs e) private void buttonAddBus_Click(object sender, EventArgs e)
{ {
@ -83,7 +95,6 @@ namespace DoubleDeckerbus
formBusConfig.AddEvent(AddBus); formBusConfig.AddEvent(AddBus);
formBusConfig.Show(); formBusConfig.Show();
} }
private void AddBus(DrawingBus selectedBus) private void AddBus(DrawingBus selectedBus)
{ {
if (listBoxStorages.SelectedIndex == -1) if (listBoxStorages.SelectedIndex == -1)
@ -115,8 +126,12 @@ namespace DoubleDeckerbus
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message); _logger.LogWarning(ex.Message);
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning(ex.Message);
}
} }
private void buttonDeleteBus_Click(object sender, EventArgs e) private void buttonDeleteBus_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1) if (listBoxStorages.SelectedIndex == -1)
@ -171,7 +186,6 @@ namespace DoubleDeckerbus
} }
pictureBoxCollection.Image = obj.ShowBus(); pictureBoxCollection.Image = obj.ShowBus();
} }
private void SaveToolStripMenuItem_Click(object sender, EventArgs e) private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)

View File

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

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Entities;
namespace DoubleDeckerbus.Generics
{
internal class BusCompareByColor : IComparer<DrawingBus>
{
public int Compare(DrawingBus? x, DrawingBus? y)
{
if (x == null || x.EntityBus == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityBus == null)
throw new ArgumentNullException(nameof(y));
var xBus = x.EntityBus;
var yBus = y.EntityBus;
if (xBus.BodyColor != yBus.BodyColor)
return xBus.BodyColor.Name.CompareTo(yBus.BodyColor.Name);
var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight);
}
}
}

View File

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

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DoubleDeckerbus.Drawing; using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Generics;
using DoubleDeckerbus.Move_Strategy; using DoubleDeckerbus.Move_Strategy;
namespace DoubleDeckerbus.Generic namespace DoubleDeckerbus.Generic
@ -26,11 +27,12 @@ namespace DoubleDeckerbus.Generic
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public static int operator +(BusGenericCollection<T, U> collect, T? obj) public static int operator +(BusGenericCollection<T, U> collect, T? obj)
{ {
if (obj != null) if (obj != null)
{ {
return collect._collection.Insert(obj); return collect._collection.Insert(obj, new DrawingBusEqutables());
} }
return -1; return -1;
} }
@ -71,6 +73,7 @@ namespace DoubleDeckerbus.Generic
{ {
int x = _pictureWidth / _placeSizeWidth - 1; int x = _pictureWidth / _placeSizeWidth - 1;
int y = 0; int y = 0;
foreach (var bus in _collection.GetBus()) foreach (var bus in _collection.GetBus())
{ {
if (bus != null) if (bus != null)

View File

@ -3,55 +3,74 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DoubleDeckerbus.Generics;
using DoubleDeckerbus.Drawing; using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Generics;
using DoubleDeckerbus.Move_Strategy; using DoubleDeckerbus.Move_Strategy;
namespace DoubleDeckerbus.Generic namespace DoubleDeckerbus.Generic
{ {
internal class BusesGenericStorage internal class BusesGenericStorage
{ {
readonly Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages; //Словарь (хранилище)
public List<string> Keys => _busStorages.Keys.ToList(); readonly Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
//Возвращение списка названий наборов
public List<BusCollectionInfo> Keys => _busStorages.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 BusesGenericStorage(int pictureWidth, int pictureHeight) public BusesGenericStorage(int pictureWidth, int pictureHeight)
{ {
_busStorages = new Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>>(); _busStorages = new Dictionary<BusCollectionInfo, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
// Добавление набора
public void AddSet(string name) public void AddSet(string name)
{ {
foreach (string nameStorage in Keys) BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
{
if (nameStorage == name) if (_busStorages.ContainsKey(set))
{ return;
return;
} _busStorages.Add(set, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
}
_busStorages.Add(name, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
} }
// Удаление набора
public void DelSet(string name) public void DelSet(string name)
{ {
if (_busStorages.ContainsKey(name)) BusCollectionInfo set = new BusCollectionInfo(name, string.Empty);
{
_busStorages.Remove(name); // проверка, что нет набора с таким именем
} if (!_busStorages.ContainsKey(set))
return;
_busStorages.Remove(set);
} }
// Доступ к набору
public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind] public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
{ {
get get
{ {
if (_busStorages.ContainsKey(ind)) BusCollectionInfo set = new BusCollectionInfo(ind, string.Empty);
if (!_busStorages.ContainsKey(set))
{ {
return _busStorages[ind]; return null;
} }
return null; return _busStorages[set];
} }
} }
public void SaveData(string filename) public void SaveData(string filename)
@ -61,7 +80,7 @@ namespace DoubleDeckerbus.Generic
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<BusCollectionInfo,
BusGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages) BusGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
@ -132,7 +151,7 @@ namespace DoubleDeckerbus.Generic
} }
} }
} }
_busStorages.Add(record[0], collection); _busStorages.Add(new BusCollectionInfo(record[0], string.Empty), collection);
} }
} }
} }

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using DoubleDeckerbus.Drawing;
using DoubleDeckerbus.Entities;
namespace DoubleDeckerbus.Generics
{
internal class DrawingBusEqutables : IEqualityComparer<DrawingBus?>
{
public bool Equals(DrawingBus? x, DrawingBus? y)
{
if (x == null && x.EntityBus == null)
throw new ArgumentNullException(nameof(x));
if (y == null && y.EntityBus == null)
throw new ArgumentNullException(nameof(y));
if ((x.GetType().Name != y.GetType().Name))
return false;
if (x.EntityBus.Speed != y.EntityBus.Speed)
return false;
if (x.EntityBus.Weight != y.EntityBus.Weight)
return false;
if (x.EntityBus.BodyColor != y.EntityBus.BodyColor)
return false;
if (x is DrawingDoubleDeckerbus && y is DrawingDoubleDeckerbus)
{
var xDoublebus = (EntityDoubleDeckerbus)x.EntityBus;
var yDoublebus = (EntityDoubleDeckerbus)y.EntityBus;
if (xDoublebus.AddColor != yDoublebus.AddColor)
return false;
if (xDoublebus.IsSecondFloor != yDoublebus.IsSecondFloor)
return false;
if (xDoublebus.IsStairs != yDoublebus.IsStairs)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawingBus? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DoubleDeckerbus.Generic namespace DoubleDeckerbus.Generic
{ {
internal class SetGeneric<T> internal class SetGeneric<T>
@ -18,11 +19,13 @@ namespace DoubleDeckerbus.Generic
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public int Insert(T bus) public int Insert(T bus, IEqualityComparer<T?>? equal = null)
{ {
return Insert(bus, 0); return Insert(bus, 0, equal);
} }
public int Insert(T bus, int position)
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public int Insert(T bus, int position, IEqualityComparer<T?>? equal = null)
{ {
if (Count >= _maxCount) if (Count >= _maxCount)
{ {
@ -32,6 +35,10 @@ namespace DoubleDeckerbus.Generic
{ {
throw new IndexOutOfRangeException("Индекс вне границ коллекции"); throw new IndexOutOfRangeException("Индекс вне границ коллекции");
} }
if (equal != null && _places.Contains(bus, equal))
{
throw new ArgumentException("Данный объект уже есть в коллекции");
}
_places.Insert(position, bus); _places.Insert(position, bus);
return 0; return 0;
} }

View File

@ -38,6 +38,7 @@ namespace DoubleDeckerbus.Move_Strategy
{ {
MoveRight(); MoveRight();
} }
} }
var diffY = objParams.ObjectMiddleVertical - FieldHeight; var diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep()) if (Math.Abs(diffY) > GetStep())