diff --git a/DumpTruck/DumpTruck/CarsGenericCollection.cs b/DumpTruck/DumpTruck/CarsGenericCollection.cs index ff03ab8..89a88d2 100644 --- a/DumpTruck/DumpTruck/CarsGenericCollection.cs +++ b/DumpTruck/DumpTruck/CarsGenericCollection.cs @@ -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 /// private readonly SetGeneric _collection; /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// /// @@ -60,9 +66,9 @@ namespace DumpTruck.Generics { return -1; } - - collect._collection.Insert(obj); - return 1; + + return collect?._collection.Insert(obj, new DrawningеTruckEqutables()) ?? -1; + } /// /// Перегрузка оператора вычитания diff --git a/DumpTruck/DumpTruck/CarsGenericStorage.cs b/DumpTruck/DumpTruck/CarsGenericStorage.cs index 3e2d479..7479e67 100644 --- a/DumpTruck/DumpTruck/CarsGenericStorage.cs +++ b/DumpTruck/DumpTruck/CarsGenericStorage.cs @@ -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 /// /// Словарь (хранилище) /// - readonly Dictionary> _carStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _carStorages.Keys.ToList(); + public List Keys => _carStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -46,7 +47,7 @@ namespace DumpTruck.Generics /// public CarsGenericStorage(int pictureWidth, int pictureHeight) { - _carStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -57,8 +58,7 @@ namespace DumpTruck.Generics /// Название набора public void AddSet(string name) { - // TODO Прописать логику для добавления - _carStorages.Add(name, new CarsGenericCollection(_pictureWidth, _pictureHeight)); + _carStorages.Add(new TruckCollectionInfo(name, string.Empty), new CarsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -66,11 +66,11 @@ namespace DumpTruck.Generics /// Название набора 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)); } /// /// Доступ к набору @@ -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> record in _carStorages) { StringBuilder records = new(); @@ -105,7 +104,7 @@ CarsGenericCollection> 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); diff --git a/DumpTruck/DumpTruck/DrawningеTruckEqutables.cs b/DumpTruck/DumpTruck/DrawningеTruckEqutables.cs new file mode 100644 index 0000000..ce1760a --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningеTruckEqutables.cs @@ -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 + { + 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(); + } + } +} diff --git a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs index 2bfc252..2a20cba 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormCarCollection.cs b/DumpTruck/DumpTruck/FormCarCollection.cs index bd8b807..30823ff 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.cs @@ -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 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()); + } } diff --git a/DumpTruck/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs index 63c7c47..617c216 100644 --- a/DumpTruck/DumpTruck/SetGeneric.cs +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -39,26 +39,32 @@ namespace DumpTruck.Generics /// /// Добавляемый автомобиль /// - public int Insert(T car) + public int Insert(T car, IEqualityComparer? equal = null) { if (_places.Count == countMax) { throw new StorageOverflowException(countMax); } - Insert(car, 0); + Insert(car, 0, equal); return 1; } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемый автомобиль /// Позиция /// - public int Insert(T car, int position) + public int Insert(T car, int position, IEqualityComparer? 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; } diff --git a/DumpTruck/DumpTruck/TruckCollectionInfo.cs b/DumpTruck/DumpTruck/TruckCollectionInfo.cs new file mode 100644 index 0000000..289abeb --- /dev/null +++ b/DumpTruck/DumpTruck/TruckCollectionInfo.cs @@ -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 + { + 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(); + } + + } +} diff --git a/DumpTruck/DumpTruck/TruckCompareByColor.cs b/DumpTruck/DumpTruck/TruckCompareByColor.cs new file mode 100644 index 0000000..ca82331 --- /dev/null +++ b/DumpTruck/DumpTruck/TruckCompareByColor.cs @@ -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 + { + 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); + } + } +} + diff --git a/DumpTruck/DumpTruck/TruckCompareByType.cs b/DumpTruck/DumpTruck/TruckCompareByType.cs new file mode 100644 index 0000000..aaadbd4 --- /dev/null +++ b/DumpTruck/DumpTruck/TruckCompareByType.cs @@ -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 + { + 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); + } + } +}