diff --git a/Lab/CarsGenericCollection.cs b/Lab/CarsGenericCollection.cs index ab86536..da5024b 100644 --- a/Lab/CarsGenericCollection.cs +++ b/Lab/CarsGenericCollection.cs @@ -47,6 +47,7 @@ namespace Lab.Generics return obj; } + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public U? GetU(int pos) { return (U?)_collection[pos]?.GetMoveableObject; diff --git a/Lab/CarsGenericStorage.cs b/Lab/CarsGenericStorage.cs index 238d4b3..55136d9 100644 --- a/Lab/CarsGenericStorage.cs +++ b/Lab/CarsGenericStorage.cs @@ -6,13 +6,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace Lab.Generics { internal class CarsGenericStorage { - readonly Dictionary> _carStorages; - public List Keys => _carStorages.Keys.ToList(); + readonly Dictionary> _carStorages; + public List Keys => _carStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; @@ -22,20 +23,22 @@ namespace Lab.Generics public CarsGenericStorage(int pictureWidth, int pictureHeight) { - _carStorages = new Dictionary>(); + _carStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - if (_carStorages.ContainsKey(name)) return; - _carStorages[name] = new CarsGenericCollection(_pictureWidth, _pictureHeight); + TankerCollectionInfo Info = new TankerCollectionInfo(name, string.Empty); + if (_carStorages.ContainsKey(Info)) return; + _carStorages[Info] = new CarsGenericCollection(_pictureWidth, _pictureHeight); } public void DelSet(string name) { - if (!_carStorages.ContainsKey(name)) return; - _carStorages.Remove(name); + TankerCollectionInfo Info = new TankerCollectionInfo(name, string.Empty); + if (!_carStorages.ContainsKey(Info)) return; + _carStorages.Remove(Info); } public CarsGenericCollection? @@ -43,7 +46,8 @@ namespace Lab.Generics { get { - if (_carStorages.ContainsKey(ind)) return _carStorages[ind]; + TankerCollectionInfo Info = new TankerCollectionInfo(ind, string.Empty); + if (_carStorages.ContainsKey(Info)) return _carStorages[Info]; return null; } } @@ -54,14 +58,14 @@ namespace Lab.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _carStorages) + foreach (KeyValuePair> record in _carStorages) { StringBuilder records = new(); foreach (DrawTanker? elem in record.Value.GetCars) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -118,7 +122,7 @@ namespace Lab.Generics } } } - _carStorages.Add(name, collection); + _carStorages.Add(new TankerCollectionInfo(name, string.Empty), collection); } } } diff --git a/Lab/CollectionsFrame.Designer.cs b/Lab/CollectionsFrame.Designer.cs index a8760c8..c000677 100644 --- a/Lab/CollectionsFrame.Designer.cs +++ b/Lab/CollectionsFrame.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { panel1 = new Panel(); + SortColorButton = new Button(); + SortTypeButton = new Button(); panel2 = new Panel(); DeleteCollectButton = new Button(); CollectionListBox = new ListBox(); @@ -40,21 +42,23 @@ AddButton = new Button(); CarTextBox = new TextBox(); label1 = new Label(); - DrawTank = new PictureBox(); - openFileDialog = new OpenFileDialog(); - saveFileDialog = new SaveFileDialog(); menuStrip1 = new MenuStrip(); StripMenu = new ToolStripMenuItem(); SaveItem = new ToolStripMenuItem(); LoadItem = new ToolStripMenuItem(); + DrawTank = new PictureBox(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); panel1.SuspendLayout(); panel2.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); menuStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(SortColorButton); + panel1.Controls.Add(SortTypeButton); panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); panel1.Controls.Add(DeleteButton); @@ -68,6 +72,26 @@ panel1.Size = new Size(250, 514); panel1.TabIndex = 0; // + // SortColorButton + // + SortColorButton.Location = new Point(19, 301); + SortColorButton.Name = "SortColorButton"; + SortColorButton.Size = new Size(208, 29); + SortColorButton.TabIndex = 8; + SortColorButton.Text = "Сортировка по цвету"; + SortColorButton.UseVisualStyleBackColor = true; + SortColorButton.Click += ButtonSortByColor_Click; + // + // SortTypeButton + // + SortTypeButton.Location = new Point(19, 266); + SortTypeButton.Name = "SortTypeButton"; + SortTypeButton.Size = new Size(208, 29); + SortTypeButton.TabIndex = 7; + SortTypeButton.Text = "Сортировка по типу"; + SortTypeButton.UseVisualStyleBackColor = true; + SortTypeButton.Click += ButtonSortByType_Click; + // // panel2 // panel2.Controls.Add(DeleteCollectButton); @@ -77,7 +101,7 @@ panel2.Controls.Add(label2); panel2.Location = new Point(16, 38); panel2.Name = "panel2"; - panel2.Size = new Size(214, 220); + panel2.Size = new Size(214, 217); panel2.TabIndex = 5; // // DeleteCollectButton @@ -128,7 +152,7 @@ // // UpdateButton // - UpdateButton.Location = new Point(6, 383); + UpdateButton.Location = new Point(6, 465); UpdateButton.Name = "UpdateButton"; UpdateButton.Size = new Size(228, 37); UpdateButton.TabIndex = 4; @@ -138,7 +162,7 @@ // // DeleteButton // - DeleteButton.Location = new Point(6, 340); + DeleteButton.Location = new Point(6, 422); DeleteButton.Name = "DeleteButton"; DeleteButton.Size = new Size(228, 37); DeleteButton.TabIndex = 3; @@ -148,7 +172,7 @@ // // AddButton // - AddButton.Location = new Point(6, 264); + AddButton.Location = new Point(6, 346); AddButton.Name = "AddButton"; AddButton.Size = new Size(228, 37); AddButton.TabIndex = 2; @@ -158,7 +182,7 @@ // // CarTextBox // - CarTextBox.Location = new Point(6, 307); + CarTextBox.Location = new Point(6, 389); CarTextBox.Name = "CarTextBox"; CarTextBox.Size = new Size(228, 27); CarTextBox.TabIndex = 1; @@ -172,19 +196,6 @@ label1.TabIndex = 0; label1.Text = "Инструменты"; // - // DrawTank - // - DrawTank.Dock = DockStyle.Fill; - DrawTank.Location = new Point(0, 0); - DrawTank.Name = "DrawTank"; - DrawTank.Size = new Size(550, 514); - DrawTank.TabIndex = 1; - DrawTank.TabStop = false; - // - // openFileDialog - // - openFileDialog.FileName = "openFileDialog1"; - // // menuStrip1 // menuStrip1.ImageScalingSize = new Size(20, 20); @@ -205,17 +216,30 @@ // SaveItem // SaveItem.Name = "SaveItem"; - SaveItem.Size = new Size(224, 26); + SaveItem.Size = new Size(166, 26); SaveItem.Text = "Сохранить"; SaveItem.Click += SaveToolStripMenuItem_Click; // // LoadItem // LoadItem.Name = "LoadItem"; - LoadItem.Size = new Size(224, 26); + LoadItem.Size = new Size(166, 26); LoadItem.Text = "Загрузить"; LoadItem.Click += LoadToolStripMenuItem_Click; // + // DrawTank + // + DrawTank.Dock = DockStyle.Fill; + DrawTank.Location = new Point(0, 0); + DrawTank.Name = "DrawTank"; + DrawTank.Size = new Size(550, 514); + DrawTank.TabIndex = 1; + DrawTank.TabStop = false; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + // // CollectionsFrame // AutoScaleDimensions = new SizeF(8F, 20F); @@ -230,9 +254,9 @@ panel1.PerformLayout(); panel2.ResumeLayout(false); panel2.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); ResumeLayout(false); } @@ -257,5 +281,7 @@ private ToolStripMenuItem StripMenu; private ToolStripMenuItem SaveItem; private ToolStripMenuItem LoadItem; + private Button SortColorButton; + private Button SortTypeButton; } } \ No newline at end of file diff --git a/Lab/CollectionsFrame.cs b/Lab/CollectionsFrame.cs index 86af8d5..7e109be 100644 --- a/Lab/CollectionsFrame.cs +++ b/Lab/CollectionsFrame.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Extensions.Logging; +using System.Drawing.Text; namespace Lab { @@ -31,7 +32,7 @@ namespace Lab CollectionListBox.Items.Clear(); foreach (var key in _storage.Keys) { - CollectionListBox.Items.Add(key); + CollectionListBox.Items.Add(key.Name); } if (CollectionListBox.Items.Count > 0 && (index == -1 || index >= CollectionListBox.Items.Count)) @@ -57,11 +58,9 @@ namespace Lab ReloadObjects(); _logger.LogInformation($"Добавлен набор: {SetTextBox.Text}"); } - private void ListBoxObjects_SelectedIndexChanged(object sender, - EventArgs e) + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { - DrawTank.Image = - _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCars(); + DrawTank.Image = _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCars(); } private void ButtonDelObject_Click(object sender, EventArgs e) { @@ -91,14 +90,19 @@ namespace Lab return; } try - { + { _ = obj + tanker; - + MessageBox.Show("Объект добавлен"); DrawTank.Image = obj.ShowCars(); _logger.LogInformation($"Добавлен объект в набор {CollectionListBox.SelectedItem.ToString()}"); } - catch (Exception ex) + catch (ArgumentException ex) + { + MessageBox.Show("Такой объект уже существует"); + _logger.LogWarning($"{ex.Message} в наборе {CollectionListBox.SelectedItem.ToString()}"); + } + catch (Exception ex) { MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"{ex.Message} в наборе {CollectionListBox.SelectedItem.ToString()}"); @@ -147,14 +151,13 @@ namespace Lab _logger.LogWarning($"Не удалось удалить объект из набора {CollectionListBox.SelectedItem.ToString()}"); } } - catch (CarNotFoundException ex) + catch (CarNotFoundException ex) { MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message} из набора {CollectionListBox.SelectedItem.ToString()}"); } } - private void ButtonRefreshCollection_Click(object sender, EventArgs - e) + private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (CollectionListBox.SelectedIndex == -1) { @@ -204,5 +207,22 @@ namespace Lab } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTanker(new TankerCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTanker(new TankerCompareByColor()); + + private void CompareTanker(IComparer comparer) + { + if (CollectionListBox.SelectedIndex == -1) + return; + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + return; + obj.Sort(comparer); + DrawTank.Image = obj.ShowCars(); + } + + } } diff --git a/Lab/DrawiningTankerEqutables.cs b/Lab/DrawiningTankerEqutables.cs new file mode 100644 index 0000000..63bdda4 --- /dev/null +++ b/Lab/DrawiningTankerEqutables.cs @@ -0,0 +1,67 @@ +using Lab.DrawningObjects; +using Lab.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab +{ + internal class DrawiningTankerEqutables : IEqualityComparer + { + public bool Equals(DrawTanker? x, DrawTanker? y) + { + if (x == null || x._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType() != y.GetType()) + { + return false; + } + if (x._gasolineTanker.Speed != y._gasolineTanker.Speed) + { + return false; + } + if (x._gasolineTanker.Weight != y._gasolineTanker.Weight) + { + return false; + } + if (x._gasolineTanker.BodyColor != y._gasolineTanker.BodyColor) + { + return false; + } + if (x is DrawGasolineTanker && y is DrawGasolineTanker) + { + if ((x._gasolineTanker as GasolineTanker).AdditionalColor != (y._gasolineTanker as GasolineTanker).AdditionalColor) + { + return false; + } + if ((x._gasolineTanker as GasolineTanker).Wing != (y._gasolineTanker as GasolineTanker).Wing) + { + return false; + } + if ((x._gasolineTanker as GasolineTanker).BodyKit != (y._gasolineTanker as GasolineTanker).BodyKit) + { + return false; + } + if ((x._gasolineTanker as GasolineTanker).SportLine != (y._gasolineTanker as GasolineTanker).SportLine) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull]DrawTanker obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Lab/SetGeneric.cs b/Lab/SetGeneric.cs index c026c56..e1e14ae 100644 --- a/Lab/SetGeneric.cs +++ b/Lab/SetGeneric.cs @@ -19,17 +19,24 @@ namespace Lab.Generics _places = new List(_maxCount); } - public bool Insert(T car) + public bool Insert(T car, IEqualityComparer? equal = null) { - return Insert(car, 0); + return Insert(car, 0, equal); } - public bool Insert(T car, int position) + public bool Insert(T car, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new CarNotFoundException(position); - if (Count >= _maxCount) throw new StorageOverflowException(position); + if (equal != null) + { + foreach (var i in _places) + { + if (equal.Equals(i, car)) + throw new ArgumentException($"Объект {car} уже существует"); + } + } _places.Insert(0, car); return true; } @@ -74,5 +81,7 @@ namespace Lab.Generics } } + public void SortSet(IComparer comparer) => _places.Sort(comparer); + } } diff --git a/Lab/TankerCollectionInfo.cs b/Lab/TankerCollectionInfo.cs new file mode 100644 index 0000000..f439e3f --- /dev/null +++ b/Lab/TankerCollectionInfo.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Lab.DrawningObjects; +using Lab.Generics; +namespace Lab +{ + internal class TankerCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TankerCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TankerCollectionInfo? other) + { + if (other == null || Name == null || other.Name == null) return false; + if (Name == other.Name) return true; + throw new NotImplementedException(); + } + public override int GetHashCode() + { + return Name?.GetHashCode() ?? 0; + } + } +} diff --git a/Lab/TankerCompareByColor.cs b/Lab/TankerCompareByColor.cs new file mode 100644 index 0000000..c22c9a7 --- /dev/null +++ b/Lab/TankerCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Lab.DrawningObjects; + +namespace Lab +{ + internal class TankerCompareByColor : IComparer + { + public int Compare(DrawTanker? x, DrawTanker? y) + { + if (x == null || x._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x._gasolineTanker.BodyColor != y._gasolineTanker.BodyColor) + { + return x._gasolineTanker.BodyColor.Name.CompareTo(y._gasolineTanker.BodyColor.Name); + } + var speedCompare = x._gasolineTanker.Speed.CompareTo(y._gasolineTanker.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x._gasolineTanker.Weight.CompareTo(y._gasolineTanker.Weight); + } + } +} diff --git a/Lab/TankerCompareByType.cs b/Lab/TankerCompareByType.cs new file mode 100644 index 0000000..8054d0e --- /dev/null +++ b/Lab/TankerCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Lab.DrawningObjects; + +namespace Lab +{ + internal class TankerCompareByType : IComparer + { + public int Compare(DrawTanker? x, DrawTanker? y) + { + if (x == null || x._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y._gasolineTanker == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x._gasolineTanker.Speed.CompareTo(y._gasolineTanker.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x._gasolineTanker.Weight.CompareTo(y._gasolineTanker.Weight); + } + } +}