PIbd-23. Yunusov N.N. Lab work 08 #8
34
Trolleybus/Trolleybus/BusCompareByColor.cs
Normal file
34
Trolleybus/Trolleybus/BusCompareByColor.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectTrolleybus.DrawingObjects;
|
||||
|
||||
namespace ProjectTrolleybus.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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
36
Trolleybus/Trolleybus/BusCompareByType.cs
Normal file
36
Trolleybus/Trolleybus/BusCompareByType.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using ProjectTrolleybus.DrawingObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectTrolleybus.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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
29
Trolleybus/Trolleybus/BusesCollectionInfo.cs
Normal file
29
Trolleybus/Trolleybus/BusesCollectionInfo.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectTrolleybus.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();
|
||||
}
|
||||
}
|
||||
}
|
@ -55,7 +55,7 @@ namespace ProjectTrolleybus.Generics
|
||||
{
|
||||
if (obj == null || collect == null)
|
||||
return false;
|
||||
collect?._collection.Insert(obj);
|
||||
collect?._collection.Insert(obj, new DrawingBusEqutables());
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
@ -130,5 +130,7 @@ namespace ProjectTrolleybus.Generics
|
||||
}
|
||||
}
|
||||
public IEnumerable<T?> GetBuses => _collection.GetBuses();
|
||||
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
|
||||
|
||||
}
|
||||
}
|
@ -13,12 +13,12 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, BusesGenericCollection<DrawingBus,
|
||||
readonly Dictionary<BusesCollectionInfo, BusesGenericCollection<DrawingBus,
|
||||
DrawingObjectBus>> _busStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _busStorages.Keys.ToList();
|
||||
public List<BusesCollectionInfo> Keys => _busStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
@ -46,7 +46,7 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <param name="pictureHeight"></param>
|
||||
public BusesGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_busStorages = new Dictionary<string, BusesGenericCollection<DrawingBus, DrawingObjectBus>>();
|
||||
_busStorages = new Dictionary<BusesCollectionInfo, BusesGenericCollection<DrawingBus, DrawingObjectBus>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -56,7 +56,7 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
_busStorages.Add(name, new BusesGenericCollection<DrawingBus,
|
||||
_busStorages.Add(new BusesCollectionInfo(name, string.Empty), new BusesGenericCollection<DrawingBus,
|
||||
DrawingObjectBus>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
/// <summary>
|
||||
@ -65,9 +65,9 @@ namespace ProjectTrolleybus.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>
|
||||
/// Доступ к набору
|
||||
@ -78,8 +78,9 @@ namespace ProjectTrolleybus.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_busStorages.ContainsKey(ind))
|
||||
return _busStorages[ind];
|
||||
BusesCollectionInfo indObj = new BusesCollectionInfo(ind, string.Empty);
|
||||
if (_busStorages.ContainsKey(indObj))
|
||||
return _busStorages[indObj];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -95,7 +96,7 @@ namespace ProjectTrolleybus.Generics
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,
|
||||
foreach (KeyValuePair<BusesCollectionInfo,
|
||||
BusesGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
@ -103,7 +104,7 @@ namespace ProjectTrolleybus.Generics
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
@ -161,7 +162,7 @@ namespace ProjectTrolleybus.Generics
|
||||
}
|
||||
}
|
||||
}
|
||||
_busStorages.Add(record[0], collection);
|
||||
_busStorages.Add(new BusesCollectionInfo(record[0], string.Empty), collection);
|
||||
str = streamReader.ReadLine();
|
||||
} while (str != null);
|
||||
}
|
||||
|
59
Trolleybus/Trolleybus/DrawingBusEqutables.cs
Normal file
59
Trolleybus/Trolleybus/DrawingBusEqutables.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectTrolleybus.DrawingObjects;
|
||||
using ProjectTrolleybus.Entities;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ProjectTrolleybus
|
||||
{
|
||||
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 DrawingTrolleybus && y is DrawingTrolleybus)
|
||||
{
|
||||
EntityTrolleybus EntityX = (EntityTrolleybus)x.EntityBus;
|
||||
EntityTrolleybus EntityY = (EntityTrolleybus)y.EntityBus;
|
||||
if (EntityX.Roga != EntityY.Roga)
|
||||
return false;
|
||||
if (EntityX.Battery != EntityY.Battery)
|
||||
return false;
|
||||
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int GetHashCode([DisallowNull] DrawingBus? obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
44
Trolleybus/Trolleybus/FormBusCollection.Designer.cs
generated
44
Trolleybus/Trolleybus/FormBusCollection.Designer.cs
generated
@ -29,6 +29,8 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
groupBoxTrolleybus = new GroupBox();
|
||||
buttonSortByColor = new Button();
|
||||
buttonSortByType = new Button();
|
||||
groupBoxSets = new GroupBox();
|
||||
textBoxStorageName = new TextBox();
|
||||
buttonDelObject = new Button();
|
||||
@ -53,6 +55,8 @@
|
||||
//
|
||||
// groupBoxTrolleybus
|
||||
//
|
||||
groupBoxTrolleybus.Controls.Add(buttonSortByColor);
|
||||
groupBoxTrolleybus.Controls.Add(buttonSortByType);
|
||||
groupBoxTrolleybus.Controls.Add(groupBoxSets);
|
||||
groupBoxTrolleybus.Controls.Add(buttonUpdateCollection);
|
||||
groupBoxTrolleybus.Controls.Add(buttonDeleteBus);
|
||||
@ -67,6 +71,26 @@
|
||||
groupBoxTrolleybus.TabStop = false;
|
||||
groupBoxTrolleybus.Text = "Инструменты";
|
||||
//
|
||||
// buttonSortByColor
|
||||
//
|
||||
buttonSortByColor.Location = new Point(11, 361);
|
||||
buttonSortByColor.Name = "buttonSortByColor";
|
||||
buttonSortByColor.Size = new Size(273, 33);
|
||||
buttonSortByColor.TabIndex = 6;
|
||||
buttonSortByColor.Text = "Сортировка по цвету";
|
||||
buttonSortByColor.UseVisualStyleBackColor = true;
|
||||
buttonSortByColor.Click += ButtonSortByColor_Click;
|
||||
//
|
||||
// buttonSortByType
|
||||
//
|
||||
buttonSortByType.Location = new Point(11, 320);
|
||||
buttonSortByType.Name = "buttonSortByType";
|
||||
buttonSortByType.Size = new Size(275, 35);
|
||||
buttonSortByType.TabIndex = 5;
|
||||
buttonSortByType.Text = "Сортировка по типу";
|
||||
buttonSortByType.UseVisualStyleBackColor = true;
|
||||
buttonSortByType.Click += ButtonSortByType_Click;
|
||||
//
|
||||
// groupBoxSets
|
||||
//
|
||||
groupBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
@ -78,14 +102,14 @@
|
||||
groupBoxSets.Margin = new Padding(3, 4, 3, 4);
|
||||
groupBoxSets.Name = "groupBoxSets";
|
||||
groupBoxSets.Padding = new Padding(3, 4, 3, 4);
|
||||
groupBoxSets.Size = new Size(280, 312);
|
||||
groupBoxSets.Size = new Size(280, 286);
|
||||
groupBoxSets.TabIndex = 4;
|
||||
groupBoxSets.TabStop = false;
|
||||
groupBoxSets.Text = "Наборы";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(7, 29);
|
||||
textBoxStorageName.Location = new Point(7, 28);
|
||||
textBoxStorageName.Margin = new Padding(3, 4, 3, 4);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(266, 27);
|
||||
@ -93,7 +117,7 @@
|
||||
//
|
||||
// buttonDelObject
|
||||
//
|
||||
buttonDelObject.Location = new Point(7, 259);
|
||||
buttonDelObject.Location = new Point(7, 238);
|
||||
buttonDelObject.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonDelObject.Name = "buttonDelObject";
|
||||
buttonDelObject.Size = new Size(266, 40);
|
||||
@ -106,7 +130,7 @@
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 20;
|
||||
listBoxStorages.Location = new Point(7, 125);
|
||||
listBoxStorages.Location = new Point(8, 106);
|
||||
listBoxStorages.Margin = new Padding(3, 4, 3, 4);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(266, 124);
|
||||
@ -116,7 +140,7 @@
|
||||
// buttonAddObject
|
||||
//
|
||||
buttonAddObject.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonAddObject.Location = new Point(7, 83);
|
||||
buttonAddObject.Location = new Point(8, 63);
|
||||
buttonAddObject.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonAddObject.Name = "buttonAddObject";
|
||||
buttonAddObject.Size = new Size(266, 35);
|
||||
@ -127,7 +151,7 @@
|
||||
//
|
||||
// buttonUpdateCollection
|
||||
//
|
||||
buttonUpdateCollection.Location = new Point(11, 499);
|
||||
buttonUpdateCollection.Location = new Point(11, 523);
|
||||
buttonUpdateCollection.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonUpdateCollection.Name = "buttonUpdateCollection";
|
||||
buttonUpdateCollection.Size = new Size(275, 37);
|
||||
@ -138,7 +162,7 @@
|
||||
//
|
||||
// buttonDeleteBus
|
||||
//
|
||||
buttonDeleteBus.Location = new Point(11, 439);
|
||||
buttonDeleteBus.Location = new Point(11, 476);
|
||||
buttonDeleteBus.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonDeleteBus.Name = "buttonDeleteBus";
|
||||
buttonDeleteBus.Size = new Size(275, 39);
|
||||
@ -149,7 +173,7 @@
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Location = new Point(74, 404);
|
||||
maskedTextBoxNumber.Location = new Point(73, 446);
|
||||
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(131, 27);
|
||||
@ -158,7 +182,7 @@
|
||||
// buttonAddBus
|
||||
//
|
||||
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonAddBus.Location = new Point(11, 348);
|
||||
buttonAddBus.Location = new Point(11, 401);
|
||||
buttonAddBus.Margin = new Padding(3, 4, 3, 4);
|
||||
buttonAddBus.Name = "buttonAddBus";
|
||||
buttonAddBus.Size = new Size(275, 37);
|
||||
@ -261,5 +285,7 @@
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private Button buttonSortByColor;
|
||||
private Button buttonSortByType;
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ namespace ProjectTrolleybus
|
||||
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))
|
||||
@ -97,10 +97,10 @@ namespace ProjectTrolleybus
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
catch (ArgumentException)
|
||||
{
|
||||
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||||
MessageBox.Show(ex.Message);
|
||||
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
|
||||
}
|
||||
});
|
||||
form.AddEvent(busDelegate);
|
||||
@ -194,5 +194,22 @@ namespace ProjectTrolleybus
|
||||
}
|
||||
}
|
||||
}
|
||||
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<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.ShowBuses();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,21 +21,26 @@ namespace ProjectTrolleybus.Generics
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
|
||||
public void Insert(T trolleybus)
|
||||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||||
public void Insert(T bus, IEqualityComparer<T>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
Insert(trolleybus, 0);
|
||||
Insert(bus, 0, equal);
|
||||
}
|
||||
|
||||
public void Insert(T trolleybus, int position)
|
||||
public void Insert(T bus, int position, IEqualityComparer<T>? equal = null)
|
||||
{
|
||||
if (_places.Count == _maxCount)
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
if (!(position >= 0 && position <= Count))
|
||||
throw new Exception("Неверная позиция для вставки");
|
||||
_places.Insert(position, trolleybus);
|
||||
if (equal != null)
|
||||
{
|
||||
if (_places.Contains(bus, equal))
|
||||
throw new ArgumentException(nameof(bus));
|
||||
}
|
||||
_places.Insert(position, bus);
|
||||
}
|
||||
|
||||
public void Remove(int position)
|
||||
|
Loading…
Reference in New Issue
Block a user