diff --git a/Sailboat/Sailboat/BoatCompareByColor.cs b/Sailboat/Sailboat/BoatCompareByColor.cs index 1cb511e..b3aae56 100644 --- a/Sailboat/Sailboat/BoatCompareByColor.cs +++ b/Sailboat/Sailboat/BoatCompareByColor.cs @@ -4,9 +4,47 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics { - internal class BoatCompareByColor + internal class BoatCompareByColor : IComparer { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityBoat is EntitySailboat xEntitySailboat && y.EntityBoat is EntitySailboat yEntitySailboat) + { + var dumpBoxColorCompare = xEntitySailboat.BodyColor.Name.CompareTo(yEntitySailboat.BodyColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntitySailboat.AdditionalColor.Name.CompareTo(yEntitySailboat.AdditionalColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } } } diff --git a/Sailboat/Sailboat/BoatCompareByType.cs b/Sailboat/Sailboat/BoatCompareByType.cs index 6b51faa..32419f8 100644 --- a/Sailboat/Sailboat/BoatCompareByType.cs +++ b/Sailboat/Sailboat/BoatCompareByType.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +namespace Sailboat.Generics { internal class BoatCompareByType : IComparer { diff --git a/Sailboat/Sailboat/BoatsCollectionInfo.cs b/Sailboat/Sailboat/BoatsCollectionInfo.cs new file mode 100644 index 0000000..9a20a0f --- /dev/null +++ b/Sailboat/Sailboat/BoatsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BoatsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BoatsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 9c5176d..bc25791 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -38,6 +38,8 @@ namespace Sailboat.Generics /// private readonly SetGeneric _collection; public IEnumerable GetBoats => _collection.GetBoats(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index a730d37..a4b4fdb 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -15,11 +15,11 @@ namespace Sailboat.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _boatStorages; + readonly Dictionary> _boatStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _boatStorages.Keys.ToList(); + public List Keys => _boatStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -47,7 +47,7 @@ namespace Sailboat.Generics /// public BoatsGenericStorage(int pictureWidth, int pictureHeight) { - _boatStorages = new Dictionary>(); + _boatStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,11 +57,7 @@ namespace Sailboat.Generics /// Название набора public void AddSet(string name) { - if (_boatStorages.ContainsKey(name)) - { - return; - } - _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), new BoatsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -69,11 +65,9 @@ namespace Sailboat.Generics /// Название набора public void DelSet(string name) { - if (!_boatStorages.ContainsKey(name)) - { + if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(name, string.Empty))) return; - } - _boatStorages.Remove(name); + _boatStorages.Remove(new BoatsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -84,10 +78,9 @@ namespace Sailboat.Generics { get { - if (_boatStorages.ContainsKey(ind)) - { - return _boatStorages[ind]; - } + BoatsCollectionInfo indObj = new BoatsCollectionInfo(ind, string.Empty); + if (_boatStorages.ContainsKey(indObj)) + return _boatStorages[indObj]; return null; } } @@ -105,14 +98,14 @@ namespace Sailboat.Generics } StringBuilder data = new(); - foreach (KeyValuePair> record in _boatStorages) + foreach (KeyValuePair> record in _boatStorages) { StringBuilder records = new(); foreach (DrawingBoat? elem in record.Value.GetBoats) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -137,53 +130,41 @@ namespace Sailboat.Generics { throw new FileNotFoundException("Файл не найден"); } - - using (StreamReader reader = new StreamReader(filename)) + using (StreamReader sr = new(filename)) { - string checker = reader.ReadLine(); - if (checker == null) + string str = sr.ReadLine(); + if (str == null || str.Length == 0) + { throw new NullReferenceException("Нет данных для загрузки"); - if (!checker.StartsWith("BoatStorage")) - { - //если нет такой записи, то это не те данные - throw new FormatException("Неверный формат данных"); } - - _boatStorages.Clear(); - string strs; - bool firstinit = true; - while ((strs = reader.ReadLine()) != null) + if (!str.StartsWith("BoatStorage")) { - if (strs == null && firstinit) - throw new NullReferenceException("Нет данных для загрузки"); - if (strs == null) - break; - firstinit = false; - string name = strs.Split('|')[0]; - BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); - foreach (string data in strs.Split('|')[1].Split(';')) + throw new InvalidDataException("Неверный формат данных"); + } + _boatStorages.Clear(); + while ((str = sr.ReadLine()) != null) + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - DrawingBoat? vehicle = data?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); - if (vehicle != null) + continue; + } + BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set.Reverse()) + { + DrawingBoat? truck = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (truck != null) { - try + if (!(collection + truck)) { - _ = collection + vehicle; - } - catch (BoatNotFoundException e) - { - throw e; - } - catch (StorageOverflowException e) - { - throw e; + throw new ApplicationException("Ошибка добавления в коллекцию"); } } } - _boatStorages.Add(name, collection); + _boatStorages.Add(new BoatsCollectionInfo(record[0], string.Empty), collection); } } } - } } diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index f18e0c1..8aa46ba 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -46,6 +46,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelTools.SuspendLayout(); this.panelCollection.SuspendLayout(); @@ -64,6 +66,8 @@ // panelTools // this.panelTools.BackColor = System.Drawing.SystemColors.ScrollBar; + this.panelTools.Controls.Add(this.buttonSortByColor); + this.panelTools.Controls.Add(this.buttonSortByType); this.panelTools.Controls.Add(this.panelCollection); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonRefreshCollection); @@ -82,7 +86,7 @@ this.panelCollection.Controls.Add(this.listBoxStorages); this.panelCollection.Controls.Add(this.buttonAddObject); this.panelCollection.Controls.Add(this.textBoxStorageName); - this.panelCollection.Location = new System.Drawing.Point(20, 166); + this.panelCollection.Location = new System.Drawing.Point(21, 128); this.panelCollection.Name = "panelCollection"; this.panelCollection.Size = new System.Drawing.Size(181, 287); this.panelCollection.TabIndex = 4; @@ -126,7 +130,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 567); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(50, 594); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27); this.maskedTextBoxNumber.TabIndex = 3; @@ -143,7 +147,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(21, 620); + this.buttonRemoveBoat.Location = new System.Drawing.Point(20, 642); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(180, 34); this.buttonRemoveBoat.TabIndex = 1; @@ -153,7 +157,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(20, 490); + this.buttonAddBoat.Location = new System.Drawing.Point(20, 544); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(180, 34); this.buttonAddBoat.TabIndex = 0; @@ -189,14 +193,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -209,6 +213,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 448); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(180, 34); + this.buttonSortByType.TabIndex = 6; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(20, 488); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(180, 34); + this.buttonSortByColor.TabIndex = 7; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // // FormBoatCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -251,5 +275,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 7525a4a..ddac986 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -33,7 +33,7 @@ namespace Sailboat 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)) @@ -220,5 +220,24 @@ namespace Sailboat } } + private void buttonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType()); + + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor()); + + private void CompareBoats(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.ShowBoats(); + } } } diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index 77b93ab..7d72c81 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -22,6 +22,8 @@ namespace Sailboat.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Конструктор /// @@ -59,7 +61,6 @@ namespace Sailboat.Generics if (_places.Count >= _maxCount) throw new StorageOverflowException(_maxCount); - //вот это проверить перед отправкой хз что делает if (equal != null) { if (_places.Contains(boat, equal))