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

This commit is contained in:
Даниил Путинцев 2023-12-19 13:34:04 +04:00
parent bcc99e559f
commit 3957e5ba4f
9 changed files with 267 additions and 24 deletions

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadTrain.DrawningObjects;
using RoadTrain.Entities;
using System.Diagnostics.CodeAnalysis;
namespace RoadTrain.Generics
{
internal class DrawiningTrainEqutables : IEqualityComparer<DrawningRoadTrain?>
{
public bool Equals(DrawningRoadTrain? x, DrawningRoadTrain? y)
{
if (x == null || x.EntityRoadTrain == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityRoadTrain == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityRoadTrain.Speed != y.EntityRoadTrain.Speed)
{
return false;
}
if (x.EntityRoadTrain.Weight != y.EntityRoadTrain.Weight)
{
return false;
}
if (x.EntityRoadTrain.BodyColor != y.EntityRoadTrain.BodyColor)
{
return false;
}
if (x is DrawningTrain && y is DrawningTrain)
{
EntityTrain EntityX = x.EntityRoadTrain as EntityTrain;
EntityTrain EntityY = y.EntityRoadTrain as EntityTrain;
if (EntityX.WaterContainer != EntityY.WaterContainer)
return false;
if (EntityX.SweepingBrush != EntityY.SweepingBrush)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningRoadTrain obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
panel1 = new Panel();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
label2 = new Label();
panel2 = new Panel();
textBoxStorageName = new TextBox();
@ -56,6 +58,8 @@
//
// panel1
//
panel1.Controls.Add(ButtonSortByColor);
panel1.Controls.Add(ButtonSortByType);
panel1.Controls.Add(label2);
panel1.Controls.Add(panel2);
panel1.Controls.Add(InputTextBox);
@ -69,6 +73,26 @@
panel1.Size = new Size(236, 572);
panel1.TabIndex = 0;
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(32, 305);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(169, 32);
ButtonSortByColor.TabIndex = 7;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(35, 257);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(163, 31);
ButtonSortByType.TabIndex = 6;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// label2
//
label2.AutoSize = true;
@ -86,19 +110,19 @@
panel2.Controls.Add(listBoxStorages);
panel2.Location = new Point(24, 29);
panel2.Name = "panel2";
panel2.Size = new Size(190, 278);
panel2.Size = new Size(190, 212);
panel2.TabIndex = 5;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(21, 30);
textBoxStorageName.Location = new Point(21, 21);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(150, 27);
textBoxStorageName.TabIndex = 3;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(21, 179);
ButtonDelObject.Location = new Point(21, 170);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(150, 29);
ButtonDelObject.TabIndex = 2;
@ -108,7 +132,7 @@
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(21, 77);
ButtonAddObject.Location = new Point(21, 66);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(150, 29);
ButtonAddObject.TabIndex = 1;
@ -120,7 +144,7 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(21, 121);
listBoxStorages.Location = new Point(21, 110);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(150, 44);
listBoxStorages.TabIndex = 0;
@ -128,7 +152,7 @@
//
// InputTextBox
//
InputTextBox.Location = new Point(59, 384);
InputTextBox.Location = new Point(58, 407);
InputTextBox.Margin = new Padding(3, 4, 3, 4);
InputTextBox.Name = "InputTextBox";
InputTextBox.Size = new Size(114, 27);
@ -136,7 +160,7 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(45, 485);
ButtonRefreshCollection.Location = new Point(43, 499);
ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(144, 59);
@ -147,7 +171,7 @@
//
// ButtonRemoveTrain
//
ButtonRemoveTrain.Location = new Point(45, 428);
ButtonRemoveTrain.Location = new Point(43, 442);
ButtonRemoveTrain.Margin = new Padding(3, 4, 3, 4);
ButtonRemoveTrain.Name = "ButtonRemoveTrain";
ButtonRemoveTrain.Size = new Size(144, 40);
@ -158,10 +182,10 @@
//
// ButtonAddTrain
//
ButtonAddTrain.Location = new Point(45, 322);
ButtonAddTrain.Location = new Point(44, 355);
ButtonAddTrain.Margin = new Padding(3, 4, 3, 4);
ButtonAddTrain.Name = "ButtonAddTrain";
ButtonAddTrain.Size = new Size(144, 44);
ButtonAddTrain.Size = new Size(142, 44);
ButtonAddTrain.TabIndex = 1;
ButtonAddTrain.Text = "Добавить поезд";
ButtonAddTrain.UseVisualStyleBackColor = true;
@ -279,5 +303,7 @@
private ToolStripMenuItem SaveToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -35,7 +35,7 @@ pictureBoxCollection.Height);
listBoxStorages.Items.Clear();
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))
@ -73,6 +73,11 @@ pictureBoxCollection.Height);
MessageBox.Show(ex.Message);
_logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
}
}
private void ButtonAddTrain_Click(object sender, EventArgs e)
{
@ -210,5 +215,32 @@ _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains()
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) =>
CompareTrains(new TrainCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) =>
CompareTrains(new TrainCompareByColor());
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer"></param>
private void CompareTrains(IComparer<DrawningRoadTrain?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowTrains();
}
}
}

View File

@ -47,6 +47,12 @@ namespace RoadTrain.Generics
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) =>
_collection.SortSet(comparer);
/// <summary>
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetTrains => _collection.GetTrains();
@ -63,7 +69,7 @@ namespace RoadTrain.Generics
{
return -1;
}
return collect._collection.Insert(obj);
return collect._collection.Insert(obj, new DrawiningTrainEqutables());
}
/// <summary>
/// Перегрузка оператора вычитания

View File

@ -18,12 +18,12 @@ namespace RoadTrain.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, RoadTrainGenericCollection<DrawningRoadTrain,
readonly Dictionary<TrainsCollectionInfo, RoadTrainGenericCollection<DrawningRoadTrain,
DrawningObjectTrain>> _trainStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _trainStorages.Keys.ToList();
public List<TrainsCollectionInfo> Keys => _trainStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -52,7 +52,7 @@ namespace RoadTrain.Generics
/// <param name="pictureHeight"></param>
public RoadTrainGenericStorage(int pictureWidth, int pictureHeight)
{
_trainStorages = new Dictionary<string,
_trainStorages = new Dictionary<TrainsCollectionInfo,
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -63,8 +63,8 @@ namespace RoadTrain.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_trainStorages.ContainsKey(name)) return;
_trainStorages[name] = new RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight);
if (_trainStorages.ContainsKey(new TrainsCollectionInfo(name, string.Empty))) return;
_trainStorages.Add(new TrainsCollectionInfo(name, string.Empty), new RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
@ -72,7 +72,7 @@ namespace RoadTrain.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name);
if (_trainStorages.ContainsKey(new TrainsCollectionInfo(name, string.Empty))) _trainStorages.Remove(new TrainsCollectionInfo(name, string.Empty));
return;
}
/// <summary>
@ -85,7 +85,9 @@ namespace RoadTrain.Generics
{
get
{
if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind];
TrainsCollectionInfo indObj = new TrainsCollectionInfo(ind, string.Empty);
if (_trainStorages.ContainsKey(indObj))
return _trainStorages[indObj];
return null;
}
}
@ -101,7 +103,7 @@ namespace RoadTrain.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<TrainsCollectionInfo,
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>> record in _trainStorages)
{
StringBuilder records = new();
@ -182,7 +184,7 @@ namespace RoadTrain.Generics
}
}
_trainStorages.Add(name, collection);
_trainStorages.Add(new TrainsCollectionInfo(name, string.Empty), collection);
}
}
}

View File

@ -28,13 +28,19 @@ namespace RoadTrain.Generics
_maxCount = count;
_places = new List<T?> (count);
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) =>
_places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <returns></returns>
public int Insert(T train)
public int Insert(T train, IEqualityComparer<T?>? equal = null)
{
return Insert(train, 0);
return Insert(train, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -42,13 +48,16 @@ namespace RoadTrain.Generics
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T train, int position)
public int Insert(T train, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new StorageOverflowException("Невалидная операция");
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null && _places.Contains(train, equal))
throw new ArgumentException("Обьект уже есть в коллекции");
_places.Insert(position, train);
return position;
}

View File

@ -0,0 +1,45 @@
using RoadTrain.DrawningObjects;
using RoadTrain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RoadTrain
{
internal class TrainCompareByColor : IComparer<DrawningRoadTrain?>
{
public int Compare(DrawningRoadTrain? x, DrawningRoadTrain? y)
{
if (x == null || x.EntityRoadTrain == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityRoadTrain == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityRoadTrain.BodyColor.Name != y.EntityRoadTrain.BodyColor.Name)
{
return x.EntityRoadTrain.BodyColor.Name.CompareTo(y.EntityRoadTrain.BodyColor.Name);
}
if (x.GetType().Name == y.GetType().Name && x is DrawningTrain)
{
EntityTrain EntityX = x.EntityRoadTrain as EntityTrain;
EntityTrain EntityY = y.EntityRoadTrain as EntityTrain;
if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name)
{
return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name);
}
}
var speedCompare =
x.EntityRoadTrain.Speed.CompareTo(y.EntityRoadTrain.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityRoadTrain.Weight.CompareTo(y.EntityRoadTrain.Weight);
}
}
}

View File

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

View File

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