Лабораторная №8
This commit is contained in:
parent
2fbd29e672
commit
a3ddbf0952
@ -34,6 +34,8 @@
|
||||
maskedTextBoxNumber = new MaskedTextBox();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
panel = new Panel();
|
||||
ButtonSortByColor = new Button();
|
||||
ButtonSortByType = new Button();
|
||||
textBoxStorageName = new TextBox();
|
||||
listBoxStorages = new ListBox();
|
||||
ButtonDelObject = new Button();
|
||||
@ -41,10 +43,9 @@
|
||||
menuStrip = new MenuStrip();
|
||||
ToolStripMenuItem = new ToolStripMenuItem();
|
||||
LoadToolStripMenuItem = new ToolStripMenuItem();
|
||||
saveToolStripMenuItem = new ToolStripMenuItem();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
saveFileDialog = new SaveFileDialog();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
panel.SuspendLayout();
|
||||
menuStrip.SuspendLayout();
|
||||
@ -53,7 +54,7 @@
|
||||
// buttonAddBus
|
||||
//
|
||||
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonAddBus.Location = new Point(19, 277);
|
||||
buttonAddBus.Location = new Point(19, 287);
|
||||
buttonAddBus.Name = "buttonAddBus";
|
||||
buttonAddBus.Size = new Size(138, 31);
|
||||
buttonAddBus.TabIndex = 0;
|
||||
@ -64,7 +65,7 @@
|
||||
// buttonRemoveBus
|
||||
//
|
||||
buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonRemoveBus.Location = new Point(19, 343);
|
||||
buttonRemoveBus.Location = new Point(19, 353);
|
||||
buttonRemoveBus.Name = "buttonRemoveBus";
|
||||
buttonRemoveBus.Size = new Size(138, 31);
|
||||
buttonRemoveBus.TabIndex = 1;
|
||||
@ -86,7 +87,7 @@
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
maskedTextBoxNumber.Location = new Point(36, 314);
|
||||
maskedTextBoxNumber.Location = new Point(36, 324);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(100, 23);
|
||||
maskedTextBoxNumber.TabIndex = 3;
|
||||
@ -103,6 +104,8 @@
|
||||
// panel
|
||||
//
|
||||
panel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
panel.Controls.Add(ButtonSortByColor);
|
||||
panel.Controls.Add(ButtonSortByType);
|
||||
panel.Controls.Add(textBoxStorageName);
|
||||
panel.Controls.Add(listBoxStorages);
|
||||
panel.Controls.Add(ButtonDelObject);
|
||||
@ -118,6 +121,26 @@
|
||||
panel.TabIndex = 5;
|
||||
panel.Tag = "";
|
||||
//
|
||||
// ButtonSortByColor
|
||||
//
|
||||
ButtonSortByColor.Location = new Point(13, 247);
|
||||
ButtonSortByColor.Name = "ButtonSortByColor";
|
||||
ButtonSortByColor.Size = new Size(138, 25);
|
||||
ButtonSortByColor.TabIndex = 12;
|
||||
ButtonSortByColor.Text = "Сортировка по цвету";
|
||||
ButtonSortByColor.UseVisualStyleBackColor = true;
|
||||
ButtonSortByColor.Click += ButtonSortByColor_Click;
|
||||
//
|
||||
// ButtonSortByType
|
||||
//
|
||||
ButtonSortByType.Location = new Point(13, 214);
|
||||
ButtonSortByType.Name = "ButtonSortByType";
|
||||
ButtonSortByType.Size = new Size(138, 27);
|
||||
ButtonSortByType.TabIndex = 11;
|
||||
ButtonSortByType.Text = "Сортировка по типу";
|
||||
ButtonSortByType.UseVisualStyleBackColor = true;
|
||||
ButtonSortByType.Click += ButtonSortByType_Click;
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(13, 36);
|
||||
@ -129,9 +152,9 @@
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 15;
|
||||
listBoxStorages.Location = new Point(13, 100);
|
||||
listBoxStorages.Location = new Point(3, 100);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(138, 64);
|
||||
listBoxStorages.Size = new Size(165, 64);
|
||||
listBoxStorages.TabIndex = 8;
|
||||
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
|
||||
//
|
||||
@ -174,16 +197,16 @@
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
LoadToolStripMenuItem.Size = new Size(180, 22);
|
||||
LoadToolStripMenuItem.Size = new Size(133, 22);
|
||||
LoadToolStripMenuItem.Text = "Загрузить";
|
||||
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
|
||||
//
|
||||
// saveToolStripMenuItem
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
|
||||
saveToolStripMenuItem.Size = new Size(180, 22);
|
||||
saveToolStripMenuItem.Text = "Сохранить";
|
||||
saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
|
||||
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
SaveToolStripMenuItem.Size = new Size(133, 22);
|
||||
SaveToolStripMenuItem.Text = "Сохранить";
|
||||
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
@ -194,13 +217,6 @@
|
||||
//
|
||||
saveFileDialog.Filter = "«txt file | *.txt»";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
SaveToolStripMenuItem.Size = new Size(180, 22);
|
||||
SaveToolStripMenuItem.Text = "Сохранить";
|
||||
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
|
||||
//
|
||||
// FormBusCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
@ -237,5 +253,7 @@
|
||||
private ToolStripMenuItem ToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private Button ButtonSortByColor;
|
||||
private Button ButtonSortByType;
|
||||
}
|
||||
}
|
@ -16,8 +16,6 @@ using DoubleDeckerBus.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Xml.Linq;
|
||||
|
||||
|
||||
|
||||
namespace DoubleDeckerBus
|
||||
{
|
||||
public partial class FormBusCollection : Form
|
||||
@ -33,13 +31,13 @@ namespace DoubleDeckerBus
|
||||
private void ReloadObjects()
|
||||
{
|
||||
int index = listBoxStorages.SelectedIndex;
|
||||
|
||||
listBoxStorages.Items.Clear();
|
||||
for (int i = 0; i < _storage.Keys.Count; i++)
|
||||
{
|
||||
listBoxStorages.Items.Add(_storage.Keys[i]);
|
||||
}
|
||||
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
|
||||
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
|
||||
>= listBoxStorages.Items.Count))
|
||||
{
|
||||
listBoxStorages.SelectedIndex = 0;
|
||||
}
|
||||
@ -60,7 +58,7 @@ namespace DoubleDeckerBus
|
||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
_storage.AddSet(textBoxStorageName.Text);
|
||||
_storage.AddSet(textBoxStorageName.Text.ToString());
|
||||
ReloadObjects();
|
||||
_logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}");
|
||||
}
|
||||
@ -85,30 +83,41 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormBusConfig form = new();
|
||||
form.Show();
|
||||
Action<DrawningBus>? busDelegate = new((m) =>
|
||||
FormBusConfig formBusConfig = new FormBusConfig();
|
||||
formBusConfig.AddEvent(AddBus);
|
||||
formBusConfig.Show();
|
||||
}
|
||||
|
||||
private void AddBus(DrawningBus bus)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool q = obj + m;
|
||||
MessageBox.Show("Объект добавлен");
|
||||
_logger.LogInformation($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
{
|
||||
_logger.LogWarning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
});
|
||||
form.AddEvent(busDelegate);
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
_logger.LogWarning("Добавление пустого объекта");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
_ = obj + bus;
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
_logger.LogInformation($"bus added in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"bus not added in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
|
||||
}
|
||||
private void buttonRemoveBus_Click(object sender, EventArgs e)
|
||||
{
|
||||
@ -116,7 +125,7 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
@ -127,23 +136,23 @@ namespace DoubleDeckerBus
|
||||
}
|
||||
try
|
||||
{
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
_logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogInformation($"Автобус удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Объект не удален");
|
||||
_logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogWarning($"Автобус не удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
}
|
||||
catch (BusNotFoundException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogWarning($"BusNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -170,13 +179,13 @@ namespace DoubleDeckerBus
|
||||
try
|
||||
{
|
||||
_storage.SaveData(saveFileDialog.FileName);
|
||||
MessageBox.Show("Сохранение прошло успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_logger.LogInformation($"Файл {saveFileDialog.FileName} успешно сохранен");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning("Не удалось сохранить");
|
||||
MessageBox.Show($"Не сохранилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
||||
}
|
||||
}
|
||||
@ -189,7 +198,7 @@ namespace DoubleDeckerBus
|
||||
try
|
||||
{
|
||||
_storage.LoadData(openFileDialog.FileName);
|
||||
MessageBox.Show("Загрузка прошла успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_logger.LogInformation($"Файл {openFileDialog.FileName} успешно загружен");
|
||||
foreach (var collection in _storage.Keys)
|
||||
{
|
||||
@ -200,11 +209,27 @@ namespace DoubleDeckerBus
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning("Не удалось загрузить");
|
||||
MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByType());
|
||||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByColor());
|
||||
private void CompareBuses(IComparer<DrawningBus?> comparer)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
obj.Sort(comparer);
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,9 +120,6 @@
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>11, 17</value>
|
||||
</metadata>
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>11, 17</value>
|
||||
</metadata>
|
||||
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>132, 17</value>
|
||||
</metadata>
|
||||
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DoubleDeckerBus.DrawningObjects;
|
||||
using DoubleDeckerBus.Entities;
|
||||
|
||||
namespace DoubleDeckerBus.Generics
|
||||
{
|
||||
internal class BusCompareByColor : IComparer<DrawningBus?>
|
||||
{
|
||||
public int Compare(DrawningBus? x, DrawningBus? y)
|
||||
{
|
||||
if (x == null || x.EntityBus == null)
|
||||
throw new ArgumentNullException(nameof(x));
|
||||
|
||||
if (y == null || y.EntityBus == null)
|
||||
throw new ArgumentNullException(nameof(y));
|
||||
|
||||
var xCruiser = x.EntityBus;
|
||||
var yCruiser = y.EntityBus;
|
||||
|
||||
if (xCruiser.BodyColor != yCruiser.BodyColor)
|
||||
return xCruiser.BodyColor.Name.CompareTo(yCruiser.BodyColor.Name);
|
||||
|
||||
var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed);
|
||||
if (speedCompare != 0)
|
||||
return speedCompare;
|
||||
return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight);
|
||||
}
|
||||
}
|
||||
}
|
35
DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs
Normal file
35
DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using DoubleDeckerBus.DrawningObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DoubleDeckerBus.Generics
|
||||
{
|
||||
internal class BusCompareByType : IComparer<DrawningBus?>
|
||||
{
|
||||
public int Compare(DrawningBus? x, DrawningBus? 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DoubleDeckerBus.Generics
|
||||
{
|
||||
internal class BusesCollectionInfo : IEquatable<BusesCollectionInfo>
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public BusesCollectionInfo(string name, string description)
|
||||
{
|
||||
Name = name;
|
||||
Description = description;
|
||||
}
|
||||
public bool Equals(BusesCollectionInfo? other)
|
||||
{
|
||||
if (Name != other?.Name)
|
||||
throw new NotImplementedException(nameof(Name));
|
||||
return true;
|
||||
}
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Name.GetHashCode();
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace DoubleDeckerBus.Generics
|
||||
where T : DrawningBus
|
||||
where U : IMoveableObject
|
||||
{
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
public IEnumerable<T?> GetTheBuses => _collection.GetTheBuses();
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
@ -33,7 +34,7 @@ namespace DoubleDeckerBus.Generics
|
||||
return false;
|
||||
}
|
||||
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
return collect._collection.Insert(obj, new DrawiningBusEqutables());
|
||||
}
|
||||
public static T? operator -(BusesGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
|
@ -10,8 +10,8 @@ namespace DoubleDeckerBus.Generics
|
||||
{
|
||||
internal class BusesGenericStorage
|
||||
{
|
||||
readonly Dictionary<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages;
|
||||
public List<string> Keys => _busStorages.Keys.ToList();
|
||||
readonly Dictionary<BusesCollectionInfo, BusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages;
|
||||
public List<BusesCollectionInfo> Keys => _busStorages.Keys.ToList();
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
@ -19,32 +19,29 @@ namespace DoubleDeckerBus.Generics
|
||||
private static readonly char _separatorForObject = ':';
|
||||
public BusesGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_busStorages = new Dictionary<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>>();
|
||||
_busStorages = new Dictionary<BusesCollectionInfo, BusesGenericCollection<DrawningBus, DrawningObjectBus>> ();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
public void AddSet(string name)
|
||||
{
|
||||
_busStorages.Add(name, new BusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight));
|
||||
_busStorages.Add(new BusesCollectionInfo(name, string.Empty),
|
||||
new BusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
public void DelSet(string name)
|
||||
{
|
||||
if (!_busStorages.ContainsKey(name))
|
||||
{
|
||||
if (!_busStorages.ContainsKey(new BusesCollectionInfo(name, string.Empty)))
|
||||
return;
|
||||
}
|
||||
|
||||
_busStorages.Remove(name);
|
||||
_busStorages.Remove(new BusesCollectionInfo(name, string.Empty));
|
||||
}
|
||||
public BusesGenericCollection<DrawningBus, DrawningObjectBus>?this[string ind]
|
||||
|
||||
public BusesGenericCollection<DrawningBus, DrawningObjectBus>? this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_busStorages.ContainsKey(ind))
|
||||
{
|
||||
return _busStorages[ind];
|
||||
}
|
||||
|
||||
BusesCollectionInfo indObj = new BusesCollectionInfo(ind, string.Empty);
|
||||
if (_busStorages.ContainsKey(indObj))
|
||||
return _busStorages[indObj];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -55,19 +52,18 @@ namespace DoubleDeckerBus.Generics
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,
|
||||
BusesGenericCollection<DrawningBus, DrawningObjectBus>> record in _busStorages)
|
||||
foreach (KeyValuePair<BusesCollectionInfo, BusesGenericCollection<DrawningBus, DrawningObjectBus>> record in _busStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningBus? elem in record.Value.GetTheBuses)
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
throw new Exception("Невалидная операция, нет данных для сохранения");
|
||||
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
||||
}
|
||||
using FileStream fs = new(filename, FileMode.Create);
|
||||
byte[] info = new
|
||||
@ -75,6 +71,7 @@ namespace DoubleDeckerBus.Generics
|
||||
fs.Write(info, 0, info.Length);
|
||||
return;
|
||||
}
|
||||
|
||||
public void LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
@ -91,8 +88,7 @@ namespace DoubleDeckerBus.Generics
|
||||
bufferTextFromFile += temp.GetString(b);
|
||||
}
|
||||
}
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (strs == null || strs.Length == 0)
|
||||
{
|
||||
throw new Exception("Нет данных для загрузки");
|
||||
@ -105,8 +101,7 @@ namespace DoubleDeckerBus.Generics
|
||||
_busStorages.Clear();
|
||||
foreach (string data in strs)
|
||||
{
|
||||
string[] record = data.Split(_separatorForKeyValue,
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
@ -117,17 +112,16 @@ namespace DoubleDeckerBus.Generics
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawningBus? car =
|
||||
elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (car != null)
|
||||
DrawningBus? bus = elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (bus != null)
|
||||
{
|
||||
if (!(collection + car))
|
||||
if (!(collection + bus))
|
||||
{
|
||||
throw new Exception("Ошибка добавления в коллекцию");
|
||||
}
|
||||
}
|
||||
}
|
||||
_busStorages.Add(record[0], collection);
|
||||
_busStorages.Add(new BusesCollectionInfo(record[0], string.Empty), collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DoubleDeckerBus.DrawningObjects;
|
||||
using DoubleDeckerBus.Entities;
|
||||
|
||||
|
||||
namespace DoubleDeckerBus.Generics
|
||||
{
|
||||
internal class DrawiningBusEqutables : IEqualityComparer<DrawningBus?>
|
||||
{
|
||||
public bool Equals(DrawningBus? x, DrawningBus? 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 DrawningDoubleDeckerBus && y is DrawningDoubleDeckerBus)
|
||||
{
|
||||
if ((x.EntityBus as EntityDoubleDeckerBus).AdditionalColor != (y.EntityBus as EntityDoubleDeckerBus).AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x.EntityBus as EntityDoubleDeckerBus).SecondFloor != (y.EntityBus as EntityDoubleDeckerBus).SecondFloor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((x.EntityBus as EntityDoubleDeckerBus).Ladder != (y.EntityBus as EntityDoubleDeckerBus).Ladder)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public int GetHashCode([DisallowNull] DrawningBus obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -14,24 +14,30 @@ namespace DoubleDeckerBus.Generics
|
||||
private readonly List<T?> _places;
|
||||
public int Count => _places.Count;
|
||||
private readonly int _maxCount;
|
||||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
public bool Insert(T bus)
|
||||
public bool Insert(T bus, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
Insert(bus, 0);
|
||||
Insert(bus, 0, equal);
|
||||
return true;
|
||||
}
|
||||
public bool Insert(T bus, int position)
|
||||
public bool Insert(T bus, int position, IEqualityComparer<T?>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
if (!(position >= 0 && position <= Count))
|
||||
return false;
|
||||
if (equal != null)
|
||||
{
|
||||
if (_places.Contains(bus, equal))
|
||||
throw new ArgumentException(("Такой объект уже существует"));
|
||||
}
|
||||
_places.Insert(position, bus);
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user