From 8f30dca90becf4dc40b96050124c2e8040d6768b Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Wed, 27 Dec 2023 09:23:17 +0400 Subject: [PATCH] TractorGeneriStorage ready --- .../FormTractorCollection.cs | 60 ++++++++++++++++--- .../TractorsCollectionInfo.cs | 29 +++++++++ .../TractorsGenericStorage.cs | 39 ++++++------ .../Шестая лаба.txt | 4 ++ 4 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsCollectionInfo.cs create mode 100644 RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/Шестая лаба.txt diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs index 245df64..4074057 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/FormTractorCollection.cs @@ -261,6 +261,8 @@ namespace ProjectTractor { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBox = new System.Windows.Forms.GroupBox(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.ButtonDelObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -292,6 +294,8 @@ namespace ProjectTractor // // groupBox // + this.groupBox.Controls.Add(this.ButtonSortByColor); + this.groupBox.Controls.Add(this.ButtonSortByType); this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Controls.Add(this.ButtonRefreshCollection); this.groupBox.Controls.Add(this.ButtonRemoveTractor); @@ -305,6 +309,26 @@ namespace ProjectTractor this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(28, 326); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(203, 29); + this.ButtonSortByColor.TabIndex = 12; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(28, 289); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(203, 29); + this.ButtonSortByType.TabIndex = 11; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBox1 // this.groupBox1.Controls.Add(this.ButtonDelObject); @@ -313,14 +337,14 @@ namespace ProjectTractor this.groupBox1.Controls.Add(this.textBoxStorageName); this.groupBox1.Location = new System.Drawing.Point(16, 54); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(224, 279); + this.groupBox1.Size = new System.Drawing.Size(224, 229); this.groupBox1.TabIndex = 9; this.groupBox1.TabStop = false; this.groupBox1.Text = "Наборы"; // // ButtonDelObject // - this.ButtonDelObject.Location = new System.Drawing.Point(12, 227); + this.ButtonDelObject.Location = new System.Drawing.Point(12, 184); this.ButtonDelObject.Name = "ButtonDelObject"; this.ButtonDelObject.Size = new System.Drawing.Size(203, 35); this.ButtonDelObject.TabIndex = 3; @@ -332,7 +356,7 @@ namespace ProjectTractor // this.listBoxStorages.FormattingEnabled = true; this.listBoxStorages.ItemHeight = 20; - this.listBoxStorages.Location = new System.Drawing.Point(12, 116); + this.listBoxStorages.Location = new System.Drawing.Point(12, 94); this.listBoxStorages.Name = "listBoxStorages"; this.listBoxStorages.Size = new System.Drawing.Size(203, 84); this.listBoxStorages.TabIndex = 2; @@ -357,7 +381,7 @@ namespace ProjectTractor // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(28, 463); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(28, 473); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(203, 35); this.ButtonRefreshCollection.TabIndex = 8; @@ -367,7 +391,7 @@ namespace ProjectTractor // // ButtonRemoveTractor // - this.ButtonRemoveTractor.Location = new System.Drawing.Point(28, 410); + this.ButtonRemoveTractor.Location = new System.Drawing.Point(28, 434); this.ButtonRemoveTractor.Name = "ButtonRemoveTractor"; this.ButtonRemoveTractor.Size = new System.Drawing.Size(203, 33); this.ButtonRemoveTractor.TabIndex = 7; @@ -377,7 +401,7 @@ namespace ProjectTractor // // ButtonAddTractor // - this.ButtonAddTractor.Location = new System.Drawing.Point(28, 337); + this.ButtonAddTractor.Location = new System.Drawing.Point(28, 361); this.ButtonAddTractor.Name = "ButtonAddTractor"; this.ButtonAddTractor.Size = new System.Drawing.Size(203, 34); this.ButtonAddTractor.TabIndex = 6; @@ -387,7 +411,7 @@ namespace ProjectTractor // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 377); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 401); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(203, 27); this.maskedTextBoxNumber.TabIndex = 5; @@ -463,6 +487,28 @@ namespace ProjectTractor private ToolStripMenuItem toolStripMenuItemload; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; private PictureBox pictureBoxCollection; + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByColor()); + + private void CompareTractors(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.ShowTractors(); + } } } diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsCollectionInfo.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsCollectionInfo.cs new file mode 100644 index 0000000..487a985 --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsCollectionInfo.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTractor.Generics +{ + internal class TractorsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TractorsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TractorsCollectionInfo? 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/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs index 162d731..80639a9 100644 --- a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/TractorsGenericStorage.cs @@ -17,11 +17,11 @@ namespace ProjectTractor /// /// Словарь (хранилище) /// - readonly Dictionary> _tractorStorages; + readonly Dictionary> _tractorStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _tractorStorages.Keys.ToList(); + public List Keys => _tractorStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -49,8 +49,8 @@ namespace ProjectTractor /// public TractorsGenericStorage(int pictureWidth, int pictureHeight) { - _tractorStorages = new Dictionary>(); + _tractorStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -60,7 +60,7 @@ namespace ProjectTractor /// Название набора public void AddSet(string name) { - _tractorStorages.Add(name, + _tractorStorages.Add(new TractorsCollectionInfo(name, string.Empty), new TractorsGenericCollection(_pictureWidth, _pictureHeight)); } @@ -70,22 +70,22 @@ namespace ProjectTractor /// Название набора public void DelSet(string name) { - if (!_tractorStorages.ContainsKey(name)) + if (!_tractorStorages.ContainsKey(new TractorsCollectionInfo(name, string.Empty))) return; - _tractorStorages.Remove(name); + _tractorStorages.Remove(new TractorsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору /// /// /// - public TractorsGenericCollection? - this[string ind] + public TractorsGenericCollection?this[string ind] { get { - if (_tractorStorages.ContainsKey(ind)) - return _tractorStorages[ind]; + TractorsCollectionInfo indObj = new TractorsCollectionInfo(ind, string.Empty); + if (_tractorStorages.ContainsKey(indObj)) + return _tractorStorages[indObj]; return null; } } @@ -102,15 +102,14 @@ namespace ProjectTractor File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _tractorStorages) + foreach (KeyValuePair> record in _tractorStorages) { StringBuilder records = new(); foreach (DrawningTractor? elem in record.Value.GetTractors) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -136,14 +135,13 @@ namespace ProjectTractor using (StreamReader streamReader = new(filename)) { string str = streamReader.ReadLine(); - string[] strings = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strings == null || strings.Length == 0) + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) { throw new IOException("Нет данных для загрузки"); } - if (!strings[0].StartsWith("TractorStorage")) + if (!strs[0].StartsWith("PlaneStorage")) { - //если нет такой записи, то это не те данные throw new IOException("Неверный формат данных"); } _tractorStorages.Clear(); @@ -156,7 +154,8 @@ namespace ProjectTractor continue; } TractorsGenericCollection collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { DrawningTractor? tractor = elem?.CreateDrawningTractor(_separatorForObject, _pictureWidth, _pictureHeight); @@ -168,7 +167,7 @@ namespace ProjectTractor } } } - _tractorStorages.Add(record[0], collection); + _tractorStorages.Add(new TractorsCollectionInfo(record[0], string.Empty), collection); str = streamReader.ReadLine(); } while (str != null); } diff --git a/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/Шестая лаба.txt b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/Шестая лаба.txt new file mode 100644 index 0000000..95ec4df --- /dev/null +++ b/RPP_FirstLaba_Tractor/RPP_FirstLaba_Tractor/Шестая лаба.txt @@ -0,0 +1,4 @@ +TractorStorage +первый|100:100:Yellow:Red:True:True;100:100:Green; +второй|100:100:Purple:Red:True:False;100:100:Yellow:Blue:False:True; +