Готовая 8 лабораторная

This commit is contained in:
Илья 2023-12-19 00:50:02 +04:00
parent 14ce4e5d49
commit f52c6557ce
9 changed files with 267 additions and 26 deletions

View File

@ -0,0 +1,57 @@
using ProjectMonorail.DrawingObjects;
using ProjectMonorail.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectMonorail.Generics
{
internal class DrawingMonorailEqutables : IEqualityComparer<DrawingMonorail?>
{
public bool Equals(DrawingMonorail? x, DrawingMonorail? y)
{
if (x == null || x.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityMonorail.Speed != y.EntityMonorail.Speed)
{
return false;
}
if (x.EntityMonorail.Weight != y.EntityMonorail.Weight)
{
return false;
}
if (x.EntityMonorail.MainColor != y.EntityMonorail.MainColor)
{
return false;
}
if (x is DrawingExtendedMonorail && y is DrawingExtendedMonorail)
{
EntityExtendedMonorail xExtendedMonorail = (EntityExtendedMonorail)x.EntityMonorail;
EntityExtendedMonorail yExtendedMonorail = (EntityExtendedMonorail)y.EntityMonorail;
if (xExtendedMonorail.AdditionalColor != yExtendedMonorail.AdditionalColor)
return false;
if (xExtendedMonorail.MagneticRail != yExtendedMonorail.MagneticRail)
return false;
if (xExtendedMonorail.ExtraCabin != yExtendedMonorail.ExtraCabin)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawingMonorail obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
panelTools = new Panel();
buttonSortByColor = new Button();
buttonSortByType = new Button();
panelSets = new Panel();
textBoxNameSet = new TextBox();
buttonAddSet = new Button();
@ -57,6 +59,8 @@
//
panelTools.Anchor = AnchorStyles.Top | AnchorStyles.Right;
panelTools.BorderStyle = BorderStyle.FixedSingle;
panelTools.Controls.Add(buttonSortByColor);
panelTools.Controls.Add(buttonSortByType);
panelTools.Controls.Add(panelSets);
panelTools.Controls.Add(buttonRefreshCollection);
panelTools.Controls.Add(labelToolsName);
@ -65,9 +69,37 @@
panelTools.Controls.Add(buttonAddMonorail);
panelTools.Location = new Point(784, 38);
panelTools.Name = "panelTools";
panelTools.Size = new Size(187, 580);
panelTools.Size = new Size(187, 661);
panelTools.TabIndex = 0;
//
// buttonSortByColor
//
buttonSortByColor.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonSortByColor.BackColor = SystemColors.Window;
buttonSortByColor.FlatAppearance.BorderColor = Color.Black;
buttonSortByColor.FlatStyle = FlatStyle.Flat;
buttonSortByColor.Location = new Point(5, 392);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(175, 28);
buttonSortByColor.TabIndex = 6;
buttonSortByColor.Text = "Sort by color";
buttonSortByColor.UseVisualStyleBackColor = false;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonSortByType.BackColor = SystemColors.Window;
buttonSortByType.FlatAppearance.BorderColor = Color.Black;
buttonSortByType.FlatStyle = FlatStyle.Flat;
buttonSortByType.Location = new Point(5, 354);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(175, 28);
buttonSortByType.TabIndex = 5;
buttonSortByType.Text = "Sort by type";
buttonSortByType.UseVisualStyleBackColor = false;
buttonSortByType.Click += buttonSortByType_Click;
//
// panelSets
//
panelSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
@ -145,7 +177,7 @@
buttonRefreshCollection.BackColor = SystemColors.Window;
buttonRefreshCollection.FlatAppearance.BorderColor = Color.Black;
buttonRefreshCollection.FlatStyle = FlatStyle.Flat;
buttonRefreshCollection.Location = new Point(5, 534);
buttonRefreshCollection.Location = new Point(5, 615);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(175, 34);
buttonRefreshCollection.TabIndex = 3;
@ -168,7 +200,7 @@
//
maskedTextBoxNumber.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
maskedTextBoxNumber.BorderStyle = BorderStyle.FixedSingle;
maskedTextBoxNumber.Location = new Point(4, 411);
maskedTextBoxNumber.Location = new Point(4, 492);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(175, 23);
maskedTextBoxNumber.TabIndex = 2;
@ -179,7 +211,7 @@
buttonRemoveMonorail.BackColor = SystemColors.Window;
buttonRemoveMonorail.FlatAppearance.BorderColor = Color.Black;
buttonRemoveMonorail.FlatStyle = FlatStyle.Flat;
buttonRemoveMonorail.Location = new Point(4, 451);
buttonRemoveMonorail.Location = new Point(4, 532);
buttonRemoveMonorail.Name = "buttonRemoveMonorail";
buttonRemoveMonorail.Size = new Size(175, 34);
buttonRemoveMonorail.TabIndex = 1;
@ -193,7 +225,7 @@
buttonAddMonorail.BackColor = SystemColors.Window;
buttonAddMonorail.FlatAppearance.BorderColor = Color.Black;
buttonAddMonorail.FlatStyle = FlatStyle.Flat;
buttonAddMonorail.Location = new Point(4, 357);
buttonAddMonorail.Location = new Point(4, 438);
buttonAddMonorail.Name = "buttonAddMonorail";
buttonAddMonorail.Size = new Size(175, 34);
buttonAddMonorail.TabIndex = 0;
@ -205,7 +237,7 @@
//
pictureBoxCollection.Location = new Point(8, 38);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(774, 580);
pictureBoxCollection.Size = new Size(774, 661);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
@ -254,7 +286,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(974, 626);
ClientSize = new Size(974, 701);
Controls.Add(pictureBoxCollection);
Controls.Add(panelTools);
Controls.Add(menuStripFile);
@ -294,5 +326,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -39,7 +39,7 @@ namespace ProjectMonorail
listBoxSets.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxSets.Items.Add(_storage.Keys[i]);
listBoxSets.Items.Add(_storage.Keys[i].Name);
}
if (listBoxSets.Items.Count > 0 && (index == -1 || index >= listBoxSets.Items.Count))
{
@ -136,6 +136,11 @@ namespace ProjectMonorail
MessageBox.Show(ex.Message);
_logger.LogWarning($"Failed attempt to add a object: {ex.Message}");
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Failed attempt to add a object: {ex.Message}");
}
}
/// <summary>
@ -198,7 +203,7 @@ namespace ProjectMonorail
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Failed attempt to delete a object: {ex.Message}");
}
}
}
/// <summary>
@ -272,5 +277,38 @@ namespace ProjectMonorail
}
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSortByType_Click(object sender, EventArgs e) => CompareMonorails(new MonorailCompareByType());
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareMonorails(new MonorailCompareByColor());
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareMonorails(IComparer<DrawingMonorail?> comparer)
{
if (listBoxSets.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowMonorails();
}
}
}

View File

@ -0,0 +1,41 @@
using ProjectMonorail.DrawingObjects;
using ProjectMonorail.Entities;
namespace ProjectMonorail.Generics
{
internal class MonorailCompareByColor :IComparer<DrawingMonorail?>
{
public int Compare(DrawingMonorail? x, DrawingMonorail? y)
{
if (x == null || x.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(y));
}
var mainColorCompare = x.EntityMonorail.MainColor.Name.CompareTo(y.EntityMonorail.MainColor.Name);
if (mainColorCompare != 0)
{
return mainColorCompare;
}
if (x is DrawingExtendedMonorail && y is DrawingExtendedMonorail)
{
EntityExtendedMonorail xExtendedMonorail = (EntityExtendedMonorail)x.EntityMonorail;
EntityExtendedMonorail yExtendedMonorail = (EntityExtendedMonorail)y.EntityMonorail;
var additionalColorCompare = xExtendedMonorail.AdditionalColor.Name.CompareTo(yExtendedMonorail.AdditionalColor.Name);
if (additionalColorCompare != 0)
{
return additionalColorCompare;
}
}
var speedCompare = x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight);
}
}
}

View File

@ -0,0 +1,29 @@
using ProjectMonorail.DrawingObjects;
namespace ProjectMonorail.Generics
{
internal class MonorailCompareByType : IComparer<DrawingMonorail?>
{
public int Compare(DrawingMonorail? x, DrawingMonorail? y)
{
if (x == null || x.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityMonorail == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight);
}
}
}

View File

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

View File

@ -42,6 +42,12 @@ namespace ProjectMonorail.Generics
/// </summary>
public IEnumerable<T?> GetMonorails => _collection.GetMonorails();
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Конструктор
/// </summary>
@ -68,7 +74,7 @@ namespace ProjectMonorail.Generics
{
return -1;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawingMonorailEqutables());
}
/// <summary>

View File

@ -27,12 +27,12 @@ namespace ProjectMonorail.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>> _monorailsStorages;
readonly Dictionary<MonorailsCollectionInfo, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>> _monorailsStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _monorailsStorages.Keys.ToList();
public List<MonorailsCollectionInfo> Keys => _monorailsStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
@ -51,7 +51,7 @@ namespace ProjectMonorail.Generics
/// <param name="pictureHeight"></param>
public MonorailsGenericStorage(int pictureWidth, int pictureHeight)
{
_monorailsStorages = new Dictionary<string, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>>();
_monorailsStorages = new Dictionary<MonorailsCollectionInfo, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -62,8 +62,8 @@ namespace ProjectMonorail.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_monorailsStorages.ContainsKey(name))
_monorailsStorages.Add(name, new MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>(_pictureWidth, _pictureHeight));
if (!_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(name, "")))
_monorailsStorages.Add(new MonorailsCollectionInfo(name, ""), new MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>(_pictureWidth, _pictureHeight));
}
/// <summary>
@ -72,8 +72,8 @@ namespace ProjectMonorail.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_monorailsStorages.ContainsKey(name))
_monorailsStorages.Remove(name);
if (_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(name, "")))
_monorailsStorages.Remove(new MonorailsCollectionInfo(name, ""));
}
/// <summary>
@ -85,8 +85,8 @@ namespace ProjectMonorail.Generics
{
get
{
if (_monorailsStorages.ContainsKey(ind))
return _monorailsStorages[ind];
if (_monorailsStorages.ContainsKey(new MonorailsCollectionInfo(ind, "")))
return _monorailsStorages[new MonorailsCollectionInfo(ind, "")];
return null;
}
}
@ -102,14 +102,14 @@ namespace ProjectMonorail.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>> record in _monorailsStorages)
foreach (KeyValuePair<MonorailsCollectionInfo, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>> record in _monorailsStorages)
{
StringBuilder records = new();
foreach (DrawingMonorail? elem in record.Value.GetMonorails)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
@ -162,7 +162,7 @@ namespace ProjectMonorail.Generics
}
}
}
_monorailsStorages.Add(record[0], collection);
_monorailsStorages.Add(new MonorailsCollectionInfo(record[0], ""), collection);
}
}
}

View File

@ -24,6 +24,12 @@ namespace ProjectMonorail.Generics
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Конструктор
/// </summary>
@ -39,9 +45,9 @@ namespace ProjectMonorail.Generics
/// </summary>
/// <param name="monorail">Добавляемый монорельс</param>
/// <returns></returns>
public int Insert(T monorail)
public int Insert(T monorail, IEqualityComparer<T?>? equal = null)
{
return Insert(monorail, 0);
return Insert(monorail, 0, equal);
}
/// <summary>
@ -50,13 +56,14 @@ namespace ProjectMonorail.Generics
/// <param name="monorail">Добавляемый монорельс</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T monorail, int position)
public int Insert(T monorail, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position > Count)
throw new MonorailNotFoundException("Impossible to insert");
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null && _places.Contains(monorail, equal))
throw new ArgumentException("This object is already in the collection");
_places.Insert(position, monorail);
return position;
}