PIbd-23. Ivanov V.N. Lab Work 08 #8

Closed
Vyacheslav wants to merge 3 commits from LabWork8 into LabWork7
9 changed files with 263 additions and 32 deletions

View File

@ -45,8 +45,10 @@
FileToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new System.Windows.Forms.OpenFileDialog();
saveFileDialog = new System.Windows.Forms.SaveFileDialog();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
ButtonSortByType = new Button();
ButtonSortByColor = new Button();
toolsPanel.SuspendLayout();
panelSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
@ -55,6 +57,8 @@
//
// toolsPanel
//
toolsPanel.Controls.Add(ButtonSortByColor);
toolsPanel.Controls.Add(ButtonSortByType);
toolsPanel.Controls.Add(panelSets);
toolsPanel.Controls.Add(ButtonRefreshCollection);
toolsPanel.Controls.Add(ButtonDeleteBus);
@ -136,7 +140,7 @@
//
// ButtonDeleteBus
//
ButtonDeleteBus.Location = new Point(8, 379);
ButtonDeleteBus.Location = new Point(13, 406);
ButtonDeleteBus.Name = "ButtonDeleteBus";
ButtonDeleteBus.Size = new Size(197, 41);
ButtonDeleteBus.TabIndex = 3;
@ -146,14 +150,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(48, 346);
maskedTextBoxNumber.Location = new Point(53, 373);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.TabIndex = 2;
//
// ButtonAddBus
//
ButtonAddBus.Location = new Point(8, 284);
ButtonAddBus.Location = new Point(13, 311);
ButtonAddBus.Name = "ButtonAddBus";
ButtonAddBus.Size = new Size(197, 41);
ButtonAddBus.TabIndex = 1;
@ -178,7 +182,7 @@
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// menuStrip1
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem });
@ -198,14 +202,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Size = new Size(177, 26);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Size = new Size(177, 26);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -218,6 +222,26 @@
//
saveFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(18, 234);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(187, 32);
ButtonSortByType.TabIndex = 7;
ButtonSortByType.Text = "Сортировать по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(18, 272);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(187, 32);
ButtonSortByColor.TabIndex = 8;
ButtonSortByColor.Text = "Сортировать по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -266,5 +290,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -46,7 +46,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
listBoxObjects.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxObjects.Items.Add(_storage.Keys[i]);
listBoxObjects.Items.Add(_storage.Keys[i].Name);
}
if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count))
@ -137,10 +137,10 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
pictureBoxCollection.Image = obj.ShowTheBuses();
Log.Information($"Добавлен объект в коллекцию {listBoxObjects.SelectedItem.ToString() ?? string.Empty}");
}
catch (StorageOverflowException ex)
catch (ArgumentException)
{
Log.Warning($"Коллекция {listBoxObjects.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(busDelegate);
@ -258,5 +258,22 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base
}
}
}
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 (listBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowTheBuses();
}
}
}

View File

@ -0,0 +1,38 @@
using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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));
}
if (x.EntityBus.BodyColor.Name != y.EntityBus.BodyColor.Name)
{
return x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.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,34 @@
using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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)
return true;
return false;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,69 @@
using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects;
using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.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)
{
EntityDoubleDeckerBus EntityX = (EntityDoubleDeckerBus)x.EntityBus;
EntityDoubleDeckerBus EntityY = (EntityDoubleDeckerBus)y.EntityBus;
if (EntityX.Ladder != EntityY.Ladder)
{
return false;
}
if (EntityX.LineBetweenFloor != EntityY.LineBetweenFloor)
{
return false;
}
if (EntityX.SecondFloor != EntityY.SecondFloor)
{
return false;
}
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningBus? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -39,18 +39,24 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
_places = new List<T?>(count);
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="bus">Добавляемый автобус</param>
/// <returns></returns>
public void Insert(T bus)
public void Insert(T bus, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
Insert(bus, 0);
Insert(bus, 0, equal);
}
/// <summary>
@ -59,7 +65,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="bus">Добавляемый автобус</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public void Insert(T bus, int position)
public void Insert(T bus, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
{
@ -69,6 +75,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
{
throw new Exception("Неверная позиция для вставки");
}
if (equal != null)
{
if (_places.Contains(bus, equal))
{
throw new ArgumentException(nameof(bus));
}
}
_places.Insert(position, bus);
}
@ -79,7 +92,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <returns></returns>
public void Remove(int position)
{
if (position < 0 || position >= Count)
if (!(position >= 0 && position < Count))
{
throw new BusNotFoundException(position);
}
@ -96,7 +109,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
{
get
{
if (position < 0 || position >= _maxCount)
if (!(position >= 0 && position < Count))
{
return null;
}
@ -109,9 +122,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
{
return;
}
_places.Insert(position, value);
return;
}
}

View File

@ -23,6 +23,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// </summary>
public IEnumerable<T?> GetTheBuses => _collection.GetTheBuses();
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Ширина окна прорисовки
/// </summary>
@ -75,7 +81,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
return false;
}
collect?._collection.Insert(obj);
collect?._collection.Insert(obj, new DrawiningBusEqutables());
return true;
}

View File

@ -16,13 +16,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, TheBusesGenericCollection<DrawningBus,
readonly Dictionary<BusesCollectionInfo, TheBusesGenericCollection<DrawningBus,
DrawningObjectBus>> _busStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _busStorages.Keys.ToList();
public List<BusesCollectionInfo> Keys => _busStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
@ -54,7 +54,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="pictureHeight"></param>
public TheBusesGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>>();
_busStorages = new Dictionary<BusesCollectionInfo, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -71,14 +71,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> record in _busStorages)
foreach (KeyValuePair<BusesCollectionInfo, TheBusesGenericCollection<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)
@ -143,7 +143,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
}
}
}
_busStorages.Add(record[0], collection);
_busStorages.Add(new BusesCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
@ -156,7 +156,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
_busStorages.Add(name, new TheBusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight));
_busStorages.Add(new BusesCollectionInfo(name, string.Empty), new TheBusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight));
}
/// <summary>
@ -165,12 +165,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
/// <param name="name">Название набора</param>
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));
}
/// <summary>
@ -182,13 +182,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics
{
get
{
if (_busStorages.ContainsKey(ind))
BusesCollectionInfo indObj = new BusesCollectionInfo(ind, string.Empty);
if (_busStorages.ContainsKey(indObj))
{
return _busStorages[ind];
return _busStorages[indObj];
}
return null;
}
}
}
}
}