From 0c379353880fe7c432fa23aba3849aede858b90e Mon Sep 17 00:00:00 2001 From: Daria Date: Mon, 6 May 2024 23:48:52 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../ICollectionGenericObjects.cs | 13 +- .../ListGenericObjects.cs | 15 +- .../MassiveGenericObjects.cs | 19 ++- .../StorageCollection.cs | 141 +++++++++++++++++- .../Excavator/Drawnings/DrawningExcavator.cs | 12 ++ .../Drawnings/DrawningTrackedVehicle.cs | 8 + .../ExtentionDrawningTrackedVehicle.cs | 46 ++++++ .../Excavator/Entities/EntityExcavator.cs | 24 +++ .../Entities/EntityTrackedVehicle.cs | 23 +++ .../FormTrackedVehicleCollection.Designer.cs | 80 ++++++++-- .../Excavator/FormTrackedVehicleCollection.cs | 50 ++++++- .../FormTrackedVehicleCollection.resx | 9 ++ 13 files changed, 420 insertions(+), 22 deletions(-) create mode 100644 Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs diff --git a/Excavator/Excavator/CollectionGenericObjects/AbstractCompany.cs b/Excavator/Excavator/CollectionGenericObjects/AbstractCompany.cs index f48a0a8..9991527 100644 --- a/Excavator/Excavator/CollectionGenericObjects/AbstractCompany.cs +++ b/Excavator/Excavator/CollectionGenericObjects/AbstractCompany.cs @@ -48,7 +48,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs index 20e361f..4824bf3 100644 --- a/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -15,7 +15,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -45,4 +45,15 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции (какого типа будет коллекция) + /// + CollectionType GetCollectionType { get; } + + /// + /// Получение объектов(элементов) коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } \ No newline at end of file diff --git a/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs index 493bfda..1c4dadc 100644 --- a/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -namespace Excavator.CollectionGenericObjects; + +namespace Excavator.CollectionGenericObjects; /// /// Параметризованный набор объектов /// @@ -18,7 +19,9 @@ public class ListGenericObjects : ICollectionGenericObjects public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { get { return _collection.Count; } set { if (value > 0) { _maxCount = value; } } } + + public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор @@ -57,5 +60,11 @@ public class ListGenericObjects : ICollectionGenericObjects return pos; } - + public IEnumerable GetItems() + { + for (int i = 0; i < Count; ++i) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs index 5a4e9ae..cfb5a8b 100644 --- a/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -namespace Excavator.CollectionGenericObjects; + +namespace Excavator.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -14,8 +15,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } set { if (value > 0) @@ -32,6 +37,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -90,4 +97,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects return drawningTrackedVehicle; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; i++) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs index e856bc1..52d34b9 100644 --- a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs +++ b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs @@ -1,11 +1,13 @@ -namespace Excavator.CollectionGenericObjects; +using Excavator.Drawnings; +using System.Text; +namespace Excavator.CollectionGenericObjects; /// /// Класс-хранилище коллекций /// /// public class StorageCollection - where T : class + where T : DrawningTrackedVehicle { /// /// Словарь (хранилище) с коллекциями @@ -71,4 +73,139 @@ public class StorageCollection } } + /// + /// Ключевое слово, с которого должен начинаться файл + /// + private readonly string _collectionKey = "CollectionsStorage"; + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) + { + StringBuilder sb = new(); + sb.Append(Environment.NewLine); + + if (value.Value.Count == 0) + { + continue; + } + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + sb.Append(data); + sb.Append(_separatorItems); + } + writer.Write(sb); + } + + } + + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader fs = File.OpenText(filename)) + { + string str = fs.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith(_collectionKey)) + { + return false; + } + _storages.Clear(); + string strs = ""; + while ((strs = fs.ReadLine()) != null) + { + if (strs == null) + { + return false; + } + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) + { + continue; + } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) + { + return false; + } + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawningTrackedVehicle() is T locomotive) + { + if (collection.Insert(locomotive) == -1) + { + return false; + } + } + } + _storages.Add(record[0], collection); + } + return true; + } + } + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Massive => new MassiveGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } } diff --git a/Excavator/Excavator/Drawnings/DrawningExcavator.cs b/Excavator/Excavator/Drawnings/DrawningExcavator.cs index 79b2753..713be37 100644 --- a/Excavator/Excavator/Drawnings/DrawningExcavator.cs +++ b/Excavator/Excavator/Drawnings/DrawningExcavator.cs @@ -9,6 +9,18 @@ namespace Excavator.Drawnings; /// public class DrawningExcavator : DrawningTrackedVehicle { + private EntityExcavator car; + + public DrawningExcavator(EntityTrackedVehicle car) : base(car) + { + EntityTrackedVehicle = car; + } + + + + + + /// /// Конструктор /// diff --git a/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs b/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs index 01a7b28..4d858ea 100644 --- a/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs +++ b/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs @@ -33,6 +33,7 @@ public class DrawningTrackedVehicle /// Верхняя кооридната прорисовки экскаватора /// protected int? _startPosY; + private EntityTrackedVehicle car; /// /// Ширина прорисовки экскаватора @@ -106,6 +107,13 @@ public class DrawningTrackedVehicle } + public DrawningTrackedVehicle(EntityTrackedVehicle car) + { + this.car = car; + } + + + /// diff --git a/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs new file mode 100644 index 0000000..811e54b --- /dev/null +++ b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs @@ -0,0 +1,46 @@ +using Excavator.Entities; + +namespace Excavator.Drawnings; + +public static class ExtentionDrawningTrackedVehicle +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningTrackedVehicle? CreateDrawningTrackedVehicle(this string info) + { + string[] strs = info.Split(_separatorForObject); + + EntityTrackedVehicle? car = EntityExcavator.CreateEntityExcavator(strs); + if (car != null) + { + return new DrawningExcavator(car); + } + car = EntityExcavator.CreateEntityExcavator(strs); + if (car != null) + { + return new DrawningTrackedVehicle(car); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningTrackedVehicle drawningTrackedVehicle) + { + string[]? array = drawningTrackedVehicle?.EntityTrackedVehicle?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} \ No newline at end of file diff --git a/Excavator/Excavator/Entities/EntityExcavator.cs b/Excavator/Excavator/Entities/EntityExcavator.cs index f233592..a8b85b7 100644 --- a/Excavator/Excavator/Entities/EntityExcavator.cs +++ b/Excavator/Excavator/Entities/EntityExcavator.cs @@ -48,4 +48,28 @@ public class EntityExcavator : EntityTrackedVehicle Bucket = bucket; Supports = supports; } + + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityExcavator), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Bucket.ToString(), Supports.ToString() }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityExcavator? CreateEntityExcavator(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityExcavator)) + { + return null; + } + return new EntityExcavator(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), + Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } } \ No newline at end of file diff --git a/Excavator/Excavator/Entities/EntityTrackedVehicle.cs b/Excavator/Excavator/Entities/EntityTrackedVehicle.cs index 72683d5..e52462a 100644 --- a/Excavator/Excavator/Entities/EntityTrackedVehicle.cs +++ b/Excavator/Excavator/Entities/EntityTrackedVehicle.cs @@ -45,5 +45,28 @@ public class EntityTrackedVehicle } + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityTrackedVehicle), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityTrackedVehicle? CreateEntityShip(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityTrackedVehicle)) + { + return null; + } + return new EntityTrackedVehicle(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } + + } diff --git a/Excavator/Excavator/FormTrackedVehicleCollection.Designer.cs b/Excavator/Excavator/FormTrackedVehicleCollection.Designer.cs index 1275d20..e420733 100644 --- a/Excavator/Excavator/FormTrackedVehicleCollection.Designer.cs +++ b/Excavator/Excavator/FormTrackedVehicleCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); groupBox1.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -59,9 +66,9 @@ groupBox1.Controls.Add(panelStorage); groupBox1.Controls.Add(comboBoxSelectorCompany); groupBox1.Dock = DockStyle.Right; - groupBox1.Location = new Point(939, 0); + groupBox1.Location = new Point(939, 28); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(221, 806); + groupBox1.Size = new Size(221, 778); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; @@ -75,9 +82,9 @@ panelCompanyTools.Controls.Add(buttonDelExcavator); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 473); + panelCompanyTools.Location = new Point(3, 476); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(215, 330); + panelCompanyTools.Size = new Size(215, 299); panelCompanyTools.TabIndex = 9; // // buttonAddTrackedVehicle @@ -94,7 +101,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(18, 273); + buttonRefresh.Location = new Point(18, 221); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(176, 45); buttonRefresh.TabIndex = 6; @@ -105,7 +112,7 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(18, 222); + buttonGoToCheck.Location = new Point(18, 170); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(171, 45); buttonGoToCheck.TabIndex = 5; @@ -115,7 +122,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(13, 125); + maskedTextBox.Location = new Point(10, 75); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(191, 27); @@ -125,7 +132,7 @@ // buttonDelExcavator // buttonDelExcavator.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonDelExcavator.Location = new Point(18, 171); + buttonDelExcavator.Location = new Point(18, 119); buttonDelExcavator.Name = "buttonDelExcavator"; buttonDelExcavator.Size = new Size(171, 45); buttonDelExcavator.TabIndex = 4; @@ -239,12 +246,54 @@ // pictureBox // pictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - pictureBox.Location = new Point(1, 0); + pictureBox.Location = new Point(1, 27); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(938, 800); + pictureBox.Size = new Size(938, 773); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1160, 28); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + SaveToolStripMenuItem.Size = new Size(227, 26); + SaveToolStripMenuItem.Text = "Сохранение"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + LoadToolStripMenuItem.Size = new Size(227, 26); + LoadToolStripMenuItem.Text = "Загрузка"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + // // FormTrackedVehicleCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -252,6 +301,8 @@ ClientSize = new Size(1160, 806); Controls.Add(pictureBox); Controls.Add(groupBox1); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormTrackedVehicleCollection"; Text = "Коллекция экскаваторов"; groupBox1.ResumeLayout(false); @@ -260,7 +311,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -283,5 +337,11 @@ private Button buttonCreateCompany; private Panel panelCompanyTools; private PictureBox pictureBox; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/Excavator/Excavator/FormTrackedVehicleCollection.cs b/Excavator/Excavator/FormTrackedVehicleCollection.cs index 77b0824..2edc8ec 100644 --- a/Excavator/Excavator/FormTrackedVehicleCollection.cs +++ b/Excavator/Excavator/FormTrackedVehicleCollection.cs @@ -45,7 +45,7 @@ public partial class FormTrackedVehicleCollection : Form private void ButtonAddCar_Click(object sender, EventArgs e) { FormTrackedVehicleConfig form = new(); - + form.Show(); form.AddEvent(SetCar); } @@ -61,7 +61,7 @@ public partial class FormTrackedVehicleCollection : Form return; } - if (_company + car != -1) + if (_company + car != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -244,5 +244,49 @@ public partial class FormTrackedVehicleCollection : Form RerfreshListBoxItems(); } - + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + } + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RerfreshListBoxItems(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } + diff --git a/Excavator/Excavator/FormTrackedVehicleCollection.resx b/Excavator/Excavator/FormTrackedVehicleCollection.resx index af32865..ee1748a 100644 --- a/Excavator/Excavator/FormTrackedVehicleCollection.resx +++ b/Excavator/Excavator/FormTrackedVehicleCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 145, 17 + + + 310, 17 + \ No newline at end of file -- 2.25.1 From 258eb869bd4050a23d93b159d32a6bf62d01d73a Mon Sep 17 00:00:00 2001 From: Daria Date: Tue, 7 May 2024 14:30:48 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 72 ++++++++++--------- .../Drawnings/DrawningTrackedVehicle.cs | 4 +- .../ExtentionDrawningTrackedVehicle.cs | 2 +- .../Entities/EntityTrackedVehicle.cs | 4 +- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs index 52d34b9..ff5997b 100644 --- a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs +++ b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs @@ -34,18 +34,16 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (name == null || _storages.ContainsKey(name)) { return; } - switch (collectionType) - + if (string.IsNullOrEmpty(name)) return; + if (_storages.ContainsKey(name)) return; + if (collectionType == CollectionType.None) return; + if (collectionType == CollectionType.Massive) { - case CollectionType.None: - return; - case CollectionType.Massive: - _storages[name] = new MassiveGenericObjects(); - return; - case CollectionType.List: - _storages[name] = new ListGenericObjects(); - return; + _storages[name] = new MassiveGenericObjects(); + } + else if (collectionType == CollectionType.List) + { + _storages[name] = new ListGenericObjects(); } } @@ -97,28 +95,31 @@ public class StorageCollection { return false; } + if (File.Exists(filename)) { File.Delete(filename); } + using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); foreach (KeyValuePair> value in _storages) { - StringBuilder sb = new(); - sb.Append(Environment.NewLine); - + writer.Write(Environment.NewLine); + // не сохраняем пустые коллекции if (value.Value.Count == 0) { continue; } - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); + + writer.Write(value.Key); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.GetCollectionType); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.MaxCount); + writer.Write(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) { string data = item?.GetDataForSave() ?? string.Empty; @@ -126,12 +127,11 @@ public class StorageCollection { continue; } - sb.Append(data); - sb.Append(_separatorItems); - } - writer.Write(sb); - } + writer.Write(data); + writer.Write(_separatorItems); + } + } } return true; @@ -147,9 +147,10 @@ public class StorageCollection { return false; } - using (StreamReader fs = File.OpenText(filename)) + + using (StreamReader reader = File.OpenText(filename)) { - string str = fs.ReadLine(); + string? str = reader.ReadLine(); if (str == null || str.Length == 0) { return false; @@ -159,31 +160,31 @@ public class StorageCollection return false; } _storages.Clear(); - string strs = ""; - while ((strs = fs.ReadLine()) != null) + string? strs = ""; + + while ((strs = reader.ReadLine()) != null) { - if (strs == null) - { - return false; - } string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 4) { continue; } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) { return false; } collection.MaxCount = Convert.ToInt32(record[2]); string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) { - if (elem?.CreateDrawningTrackedVehicle() is T locomotive) + if (elem?.CreateDrawningTrackedVehicle() is T ship) { - if (collection.Insert(locomotive) == -1) + if (collection.Insert(ship) == -1) { return false; } @@ -191,6 +192,7 @@ public class StorageCollection } _storages.Add(record[0], collection); } + return true; } } diff --git a/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs b/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs index 4d858ea..4f5db47 100644 --- a/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs +++ b/Excavator/Excavator/Drawnings/DrawningTrackedVehicle.cs @@ -107,9 +107,9 @@ public class DrawningTrackedVehicle } - public DrawningTrackedVehicle(EntityTrackedVehicle car) + public DrawningTrackedVehicle(EntityTrackedVehicle car) : this() { - this.car = car; + EntityTrackedVehicle = car; } diff --git a/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs index 811e54b..8cbe731 100644 --- a/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs +++ b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs @@ -22,7 +22,7 @@ public static class ExtentionDrawningTrackedVehicle { return new DrawningExcavator(car); } - car = EntityExcavator.CreateEntityExcavator(strs); + car = EntityTrackedVehicle.CreateEntityTrackedVehicle(strs); if (car != null) { return new DrawningTrackedVehicle(car); diff --git a/Excavator/Excavator/Entities/EntityTrackedVehicle.cs b/Excavator/Excavator/Entities/EntityTrackedVehicle.cs index e52462a..c67cd00 100644 --- a/Excavator/Excavator/Entities/EntityTrackedVehicle.cs +++ b/Excavator/Excavator/Entities/EntityTrackedVehicle.cs @@ -59,7 +59,7 @@ public class EntityTrackedVehicle /// /// /// - public static EntityTrackedVehicle? CreateEntityShip(string[] strs) + public static EntityTrackedVehicle? CreateEntityTrackedVehicle(string[] strs) { if (strs.Length != 4 || strs[0] != nameof(EntityTrackedVehicle)) { @@ -68,5 +68,5 @@ public class EntityTrackedVehicle return new EntityTrackedVehicle(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); } - + } -- 2.25.1 From 81703aa1f36c0e1c1fb51a17238f2241f19ff6ef Mon Sep 17 00:00:00 2001 From: Daria Date: Mon, 13 May 2024 13:42:01 +0400 Subject: [PATCH 3/3] / --- .../Excavator/CollectionGenericObjects/StorageCollection.cs | 4 ++-- .../Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs index ff5997b..62c5d10 100644 --- a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs +++ b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs @@ -182,9 +182,9 @@ public class StorageCollection foreach (string elem in set) { - if (elem?.CreateDrawningTrackedVehicle() is T ship) + if (elem?.CreateDrawningTrackedVehicle() is T car) { - if (collection.Insert(ship) == -1) + if (collection.Insert(car) == -1) { return false; } diff --git a/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs index 8cbe731..05622de 100644 --- a/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs +++ b/Excavator/Excavator/Drawnings/ExtentionDrawningTrackedVehicle.cs @@ -32,7 +32,7 @@ public static class ExtentionDrawningTrackedVehicle /// /// Получение данных для сохранения в файл /// - /// Сохраняемый объект + /// Сохраняемый объект /// Строка с данными по объекту public static string GetDataForSave(this DrawningTrackedVehicle drawningTrackedVehicle) { -- 2.25.1