This commit is contained in:
ValAnn 2023-12-23 11:46:58 +04:00
parent 13ea3332c9
commit d07ca55777
9 changed files with 250 additions and 29 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using DumpTruck.DrawningObjects;
using DumpTruck.Entities;
using DumpTruck.MovementStrategy;
using DumpTruck.Generics;
namespace DumpTruck.Generics
{
@ -35,6 +36,11 @@ namespace DumpTruck.Generics
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
@ -60,9 +66,9 @@ namespace DumpTruck.Generics
{
return -1;
}
collect._collection.Insert(obj);
return 1;
return collect?._collection.Insert(obj, new DrawningеTruckEqutables()) ?? -1;
}
/// <summary>
/// Перегрузка оператора вычитания

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using DumpTruck.DrawningObjects;
using DumpTruck.MovementStrategy;
@ -25,12 +26,12 @@ namespace DumpTruck.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, CarsGenericCollection<DrawningCar,
readonly Dictionary<TruckCollectionInfo, CarsGenericCollection<DrawningCar,
DrawningObjectCar>> _carStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _carStorages.Keys.ToList();
public List<TruckCollectionInfo> Keys => _carStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -46,7 +47,7 @@ namespace DumpTruck.Generics
/// <param name="pictureHeight"></param>
public CarsGenericStorage(int pictureWidth, int pictureHeight)
{
_carStorages = new Dictionary<string,
_carStorages = new Dictionary<TruckCollectionInfo,
CarsGenericCollection<DrawningCar, DrawningObjectCar>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -57,8 +58,7 @@ namespace DumpTruck.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
// TODO Прописать логику для добавления
_carStorages.Add(name, new CarsGenericCollection<DrawningCar, DrawningObjectCar>(_pictureWidth, _pictureHeight));
_carStorages.Add(new TruckCollectionInfo(name, string.Empty), new CarsGenericCollection<DrawningCar, DrawningObjectCar>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
@ -66,11 +66,11 @@ namespace DumpTruck.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_carStorages.ContainsKey(name))
if (!_carStorages.ContainsKey(new TruckCollectionInfo(name, string.Empty)))
{
return;
}
_carStorages.Remove(name);
_carStorages.Remove(new TruckCollectionInfo(name, string.Empty));
}
/// <summary>
/// Доступ к набору
@ -82,10 +82,9 @@ namespace DumpTruck.Generics
{
get
{
if (_carStorages.ContainsKey(ind))
{
return _carStorages[ind];
}
TruckCollectionInfo indObj = new TruckCollectionInfo(ind, string.Empty);
if (_carStorages.ContainsKey(indObj))
return _carStorages[indObj];
return null;
}
}
@ -97,7 +96,7 @@ namespace DumpTruck.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
foreach (KeyValuePair<TruckCollectionInfo,
CarsGenericCollection<DrawningCar, DrawningObjectCar>> record in _carStorages)
{
StringBuilder records = new();
@ -105,7 +104,7 @@ CarsGenericCollection<DrawningCar, DrawningObjectCar>> record in _carStorages)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
@ -171,7 +170,7 @@ StringSplitOptions.RemoveEmptyEntries);
}
}
}
_carStorages.Add(record[0], collection);
_carStorages.Add(new TruckCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);

View File

@ -0,0 +1,60 @@
using DumpTruck.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck.Generics
{
internal class DrawningеTruckEqutables : IEqualityComparer<DrawningCar?>
{
public bool Equals(DrawningCar? x, DrawningCar? y)
{
if (x == null || x.EntityCar == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityCar == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityCar.Speed != y.EntityCar.Speed)
{
return false;
}
if (x.EntityCar.Weight != y.EntityCar.Weight)
{
return false;
}
if (x.EntityCar.BodyColor != y.EntityCar.BodyColor)
{
return false;
}
if (x is DrawningDumpTruck && y is DrawningDumpTruck)
{
EntityDumpTruck EntityX = (EntityDumpTruck)x.EntityCar;
EntityDumpTruck EntityY = (EntityDumpTruck)y.EntityCar;
if (EntityX.Tent != EntityY.Tent)
return false;
if (EntityX.BodyKit != EntityY.BodyKit)
return false;
if (EntityX.BodyKit && EntityX.Tent != EntityY.Tent && EntityY.BodyKit)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningCar obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -30,6 +30,8 @@
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.panelCollection = new System.Windows.Forms.Panel();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
@ -63,17 +65,39 @@
//
// panelCollection
//
this.panelCollection.Controls.Add(this.ButtonSortByColor);
this.panelCollection.Controls.Add(this.ButtonSortByType);
this.panelCollection.Controls.Add(this.ButtonRefreshCollection);
this.panelCollection.Controls.Add(this.panel1);
this.panelCollection.Controls.Add(this.panelObjects);
this.panelCollection.Location = new System.Drawing.Point(636, 0);
this.panelCollection.Name = "panelCollection";
this.panelCollection.Size = new System.Drawing.Size(217, 470);
this.panelCollection.Size = new System.Drawing.Size(217, 482);
this.panelCollection.TabIndex = 1;
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(18, 259);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(187, 24);
this.ButtonSortByColor.TabIndex = 6;
this.ButtonSortByColor.Text = "Сортировка по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(18, 229);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(187, 24);
this.ButtonSortByType.TabIndex = 5;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(20, 414);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(19, 438);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(181, 41);
this.ButtonRefreshCollection.TabIndex = 1;
@ -86,7 +110,7 @@
this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonRemoveCar);
this.panel1.Controls.Add(this.ButtonAddCar);
this.panel1.Location = new System.Drawing.Point(17, 265);
this.panel1.Location = new System.Drawing.Point(17, 304);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(187, 128);
this.panel1.TabIndex = 3;
@ -127,12 +151,12 @@
this.panelObjects.Controls.Add(this.ButtonAddObject);
this.panelObjects.Location = new System.Drawing.Point(17, 3);
this.panelObjects.Name = "panelObjects";
this.panelObjects.Size = new System.Drawing.Size(187, 241);
this.panelObjects.Size = new System.Drawing.Size(187, 217);
this.panelObjects.TabIndex = 4;
//
// ButtonDelObject_
//
this.ButtonDelObject_.Location = new System.Drawing.Point(3, 194);
this.ButtonDelObject_.Location = new System.Drawing.Point(3, 173);
this.ButtonDelObject_.Name = "ButtonDelObject_";
this.ButtonDelObject_.Size = new System.Drawing.Size(181, 41);
this.ButtonDelObject_.TabIndex = 3;
@ -152,7 +176,7 @@
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(3, 109);
this.listBoxStorages.Location = new System.Drawing.Point(3, 88);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(181, 79);
this.listBoxStorages.TabIndex = 2;
@ -190,14 +214,14 @@
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
@ -254,5 +278,7 @@
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -39,7 +39,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
@ -86,6 +86,11 @@ pictureBoxCollection.Height);
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
catch (ArgumentException ex)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(carDelegate);
@ -239,6 +244,25 @@ MessageBoxIcon.Question) == DialogResult.Yes)
}
}
private void CompareCars(IComparer<DrawningCar?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowCars();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCars(new TruckCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCars(new TruckCompareByColor());
}
}

View File

@ -39,26 +39,32 @@ namespace DumpTruck.Generics
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T car)
public int Insert(T car, IEqualityComparer<T>? equal = null)
{
if (_places.Count == countMax) { throw new StorageOverflowException(countMax); }
Insert(car, 0);
Insert(car, 0, equal);
return 1;
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T car, int position)
public int Insert(T car, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == countMax)
throw new StorageOverflowException(countMax);
if (!(position >= 0 && position <= Count)) return -1;
if (equal != null)
{
if (_places.Contains(car, equal))
throw new ArgumentException(nameof(car));
}
_places.Insert(position, car);
return position;
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
{
internal class TruckCollectionInfo : IEquatable<TruckCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public TruckCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(TruckCollectionInfo? other)
{
if (other == null || other.Name == null)
throw new ArgumentNullException(nameof(other));
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

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

View File

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