diff --git a/Laba1Loco/Laba1Loco/App.config b/Laba1Loco/Laba1Loco/App.config index ecdcf8a..4bfa005 100644 --- a/Laba1Loco/Laba1Loco/App.config +++ b/Laba1Loco/Laba1Loco/App.config @@ -1,6 +1,6 @@ - + diff --git a/Laba1Loco/Laba1Loco/ExtentionDrawingTrain.cs b/Laba1Loco/Laba1Loco/ExtentionDrawingTrain.cs new file mode 100644 index 0000000..d687db3 --- /dev/null +++ b/Laba1Loco/Laba1Loco/ExtentionDrawingTrain.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Laba1Loco +{ + internal static class ExtentionDrawingTrain + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingTrain CreateDrawingTrain(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingTrain(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingLoco(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), + Convert.ToBoolean(strs[6]), width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingTrain drawningTrain, char separatorForObject) + { + var train = drawningTrain.EntityTrain; + if (train == null) + { + return string.Empty; + } + var str = $"{train.Speed}{separatorForObject}{train.Weight}{separatorForObject}{train.BodyColor.Name}"; + if (!(train is EntityLoco)) + { + return str; + } + return $"{str}{separatorForObject}{(train as EntityLoco).AdditionalColor.Name}{separatorForObject}{(train as EntityLoco).Tube}{separatorForObject}{(train as EntityLoco).FuelTank}{separatorForObject}{(train as EntityLoco).LocoLine}"; + } + } +} diff --git a/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs b/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs index a7b343f..f33b15a 100644 --- a/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs +++ b/Laba1Loco/Laba1Loco/FormTrainCollection.Designer.cs @@ -28,146 +28,209 @@ /// private void InitializeComponent() { - this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); - this.panel1 = new System.Windows.Forms.Panel(); - this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); - this.ButtonAddTrain = new System.Windows.Forms.Button(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); - this.ButtonRemoveTrain = new System.Windows.Forms.Button(); - this.panel2 = new System.Windows.Forms.Panel(); - this.textBoxSetName = new System.Windows.Forms.TextBox(); - this.buttonAddSet = new System.Windows.Forms.Button(); - this.listBoxStorage = new System.Windows.Forms.ListBox(); - this.buttonRemoveSet = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); - this.panel1.SuspendLayout(); - this.panel2.SuspendLayout(); - this.SuspendLayout(); + pictureBoxCollection = new System.Windows.Forms.PictureBox(); + panel1 = new System.Windows.Forms.Panel(); + panel2 = new System.Windows.Forms.Panel(); + buttonRemoveSet = new System.Windows.Forms.Button(); + listBoxStorage = new System.Windows.Forms.ListBox(); + buttonAddSet = new System.Windows.Forms.Button(); + textBoxSetName = new System.Windows.Forms.TextBox(); + maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + ButtonAddTrain = new System.Windows.Forms.Button(); + ButtonRefreshCollection = new System.Windows.Forms.Button(); + ButtonRemoveTrain = new System.Windows.Forms.Button(); + menuStrip1 = new System.Windows.Forms.MenuStrip(); + filesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + openFileDialog = new System.Windows.Forms.OpenFileDialog(); + saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panel1.SuspendLayout(); + panel2.SuspendLayout(); + menuStrip1.SuspendLayout(); + SuspendLayout(); // // pictureBoxCollection // - this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Left; - this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); - this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(633, 473); - this.pictureBoxCollection.TabIndex = 0; - this.pictureBoxCollection.TabStop = false; + pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Left; + pictureBoxCollection.Location = new System.Drawing.Point(0, 0); + pictureBoxCollection.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new System.Drawing.Size(738, 546); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; // // panel1 // - this.panel1.AccessibleName = ""; - this.panel1.Controls.Add(this.panel2); - this.panel1.Controls.Add(this.maskedTextBoxNumber); - this.panel1.Controls.Add(this.ButtonAddTrain); - this.panel1.Controls.Add(this.ButtonRefreshCollection); - this.panel1.Controls.Add(this.ButtonRemoveTrain); - this.panel1.Dock = System.Windows.Forms.DockStyle.Right; - this.panel1.Location = new System.Drawing.Point(639, 0); - this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(166, 473); - this.panel1.TabIndex = 1; - // - // maskedTextBoxNumber - // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(33, 365); - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20); - this.maskedTextBoxNumber.TabIndex = 4; - // - // ButtonAddTrain - // - this.ButtonAddTrain.Location = new System.Drawing.Point(43, 327); - this.ButtonAddTrain.Name = "ButtonAddTrain"; - this.ButtonAddTrain.Size = new System.Drawing.Size(75, 23); - this.ButtonAddTrain.TabIndex = 3; - this.ButtonAddTrain.Text = "Add Train"; - this.ButtonAddTrain.UseVisualStyleBackColor = true; - this.ButtonAddTrain.Click += new System.EventHandler(this.ButtonAddTrain_Click); - // - // ButtonRefreshCollection - // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(21, 438); - this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(121, 23); - this.ButtonRefreshCollection.TabIndex = 2; - this.ButtonRefreshCollection.Text = "Refresh Collection"; - this.ButtonRefreshCollection.UseVisualStyleBackColor = true; - this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); - // - // ButtonRemoveTrain - // - this.ButtonRemoveTrain.Location = new System.Drawing.Point(33, 400); - this.ButtonRemoveTrain.Name = "ButtonRemoveTrain"; - this.ButtonRemoveTrain.Size = new System.Drawing.Size(100, 23); - this.ButtonRemoveTrain.TabIndex = 0; - this.ButtonRemoveTrain.Text = "Remove Train"; - this.ButtonRemoveTrain.UseVisualStyleBackColor = true; - this.ButtonRemoveTrain.Click += new System.EventHandler(this.ButtonRemoveTrain_Click); + panel1.AccessibleName = ""; + panel1.Controls.Add(panel2); + panel1.Controls.Add(maskedTextBoxNumber); + panel1.Controls.Add(ButtonAddTrain); + panel1.Controls.Add(ButtonRefreshCollection); + panel1.Controls.Add(ButtonRemoveTrain); + panel1.Controls.Add(menuStrip1); + panel1.Dock = System.Windows.Forms.DockStyle.Right; + panel1.Location = new System.Drawing.Point(745, 0); + panel1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + panel1.Name = "panel1"; + panel1.Size = new System.Drawing.Size(194, 546); + panel1.TabIndex = 1; // // panel2 // - this.panel2.AccessibleName = ""; - this.panel2.Controls.Add(this.buttonRemoveSet); - this.panel2.Controls.Add(this.listBoxStorage); - this.panel2.Controls.Add(this.buttonAddSet); - this.panel2.Controls.Add(this.textBoxSetName); - this.panel2.Location = new System.Drawing.Point(14, 12); - this.panel2.Name = "panel2"; - this.panel2.Size = new System.Drawing.Size(140, 208); - this.panel2.TabIndex = 5; - // - // textBoxSetName - // - this.textBoxSetName.Location = new System.Drawing.Point(8, 12); - this.textBoxSetName.Name = "textBoxSetName"; - this.textBoxSetName.Size = new System.Drawing.Size(120, 20); - this.textBoxSetName.TabIndex = 0; - // - // buttonAddSet - // - this.buttonAddSet.Location = new System.Drawing.Point(8, 38); - this.buttonAddSet.Name = "buttonAddSet"; - this.buttonAddSet.Size = new System.Drawing.Size(120, 23); - this.buttonAddSet.TabIndex = 1; - this.buttonAddSet.Text = "Add set"; - this.buttonAddSet.UseVisualStyleBackColor = true; - this.buttonAddSet.Click += new System.EventHandler(this.buttonAddSet_Click); - // - // listBoxStorage - // - this.listBoxStorage.FormattingEnabled = true; - this.listBoxStorage.Location = new System.Drawing.Point(8, 67); - this.listBoxStorage.Name = "listBoxStorage"; - this.listBoxStorage.Size = new System.Drawing.Size(120, 95); - this.listBoxStorage.TabIndex = 2; - this.listBoxStorage.SelectedIndexChanged += new System.EventHandler(this.listBoxStorage_SelectedIndexChanged); + panel2.AccessibleName = ""; + panel2.Controls.Add(buttonRemoveSet); + panel2.Controls.Add(listBoxStorage); + panel2.Controls.Add(buttonAddSet); + panel2.Controls.Add(textBoxSetName); + panel2.Location = new System.Drawing.Point(18, 98); + panel2.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + panel2.Name = "panel2"; + panel2.Size = new System.Drawing.Size(163, 240); + panel2.TabIndex = 5; // // buttonRemoveSet // - this.buttonRemoveSet.Location = new System.Drawing.Point(8, 168); - this.buttonRemoveSet.Name = "buttonRemoveSet"; - this.buttonRemoveSet.Size = new System.Drawing.Size(120, 23); - this.buttonRemoveSet.TabIndex = 3; - this.buttonRemoveSet.Text = "Remove set"; - this.buttonRemoveSet.UseVisualStyleBackColor = true; - this.buttonRemoveSet.Click += new System.EventHandler(this.buttonRemoveSet_Click); + buttonRemoveSet.Location = new System.Drawing.Point(9, 194); + buttonRemoveSet.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + buttonRemoveSet.Name = "buttonRemoveSet"; + buttonRemoveSet.Size = new System.Drawing.Size(140, 27); + buttonRemoveSet.TabIndex = 3; + buttonRemoveSet.Text = "Remove set"; + buttonRemoveSet.UseVisualStyleBackColor = true; + buttonRemoveSet.Click += buttonRemoveSet_Click; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 15; + listBoxStorage.Location = new System.Drawing.Point(9, 77); + listBoxStorage.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new System.Drawing.Size(139, 109); + listBoxStorage.TabIndex = 2; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; + // + // buttonAddSet + // + buttonAddSet.Location = new System.Drawing.Point(9, 44); + buttonAddSet.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + buttonAddSet.Name = "buttonAddSet"; + buttonAddSet.Size = new System.Drawing.Size(140, 27); + buttonAddSet.TabIndex = 1; + buttonAddSet.Text = "Add set"; + buttonAddSet.UseVisualStyleBackColor = true; + buttonAddSet.Click += buttonAddSet_Click; + // + // textBoxSetName + // + textBoxSetName.Location = new System.Drawing.Point(9, 14); + textBoxSetName.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + textBoxSetName.Name = "textBoxSetName"; + textBoxSetName.Size = new System.Drawing.Size(139, 23); + textBoxSetName.TabIndex = 0; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new System.Drawing.Point(38, 421); + maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new System.Drawing.Size(116, 23); + maskedTextBoxNumber.TabIndex = 4; + // + // ButtonAddTrain + // + ButtonAddTrain.Location = new System.Drawing.Point(50, 377); + ButtonAddTrain.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + ButtonAddTrain.Name = "ButtonAddTrain"; + ButtonAddTrain.Size = new System.Drawing.Size(88, 27); + ButtonAddTrain.TabIndex = 3; + ButtonAddTrain.Text = "Add Train"; + ButtonAddTrain.UseVisualStyleBackColor = true; + ButtonAddTrain.Click += ButtonAddTrain_Click; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new System.Drawing.Point(24, 505); + ButtonRefreshCollection.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new System.Drawing.Size(141, 27); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Refresh Collection"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // ButtonRemoveTrain + // + ButtonRemoveTrain.Location = new System.Drawing.Point(38, 462); + ButtonRemoveTrain.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + ButtonRemoveTrain.Name = "ButtonRemoveTrain"; + ButtonRemoveTrain.Size = new System.Drawing.Size(117, 27); + ButtonRemoveTrain.TabIndex = 0; + ButtonRemoveTrain.Text = "Remove Train"; + ButtonRemoveTrain.UseVisualStyleBackColor = true; + ButtonRemoveTrain.Click += ButtonRemoveTrain_Click; + // + // menuStrip1 + // + menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { filesToolStripMenuItem }); + menuStrip1.Location = new System.Drawing.Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new System.Drawing.Size(194, 24); + menuStrip1.TabIndex = 6; + menuStrip1.Text = "menuStrip1"; + // + // filesToolStripMenuItem + // + filesToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + filesToolStripMenuItem.Name = "filesToolStripMenuItem"; + filesToolStripMenuItem.Size = new System.Drawing.Size(42, 20); + filesToolStripMenuItem.Text = "Files"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + saveToolStripMenuItem.Text = "Save"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + loadToolStripMenuItem.Text = "Load"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "TrainSave.txt"; + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.FileName = "TrainSave.txt"; + saveFileDialog.Filter = "txt file | *.txt"; // // FormTrainCollection // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(805, 473); - this.Controls.Add(this.panel1); - this.Controls.Add(this.pictureBoxCollection); - this.Name = "FormTrainCollection"; - this.Text = "FormTrainCollection"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); - this.panel1.ResumeLayout(false); - this.panel1.PerformLayout(); - this.panel2.ResumeLayout(false); - this.panel2.PerformLayout(); - this.ResumeLayout(false); - + AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + ClientSize = new System.Drawing.Size(939, 546); + Controls.Add(panel1); + Controls.Add(pictureBoxCollection); + MainMenuStrip = menuStrip1; + Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + Name = "FormTrainCollection"; + Text = "FormTrainCollection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panel1.ResumeLayout(false); + panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); + ResumeLayout(false); } #endregion @@ -183,5 +246,11 @@ private System.Windows.Forms.ListBox listBoxStorage; private System.Windows.Forms.Button buttonAddSet; private System.Windows.Forms.TextBox textBoxSetName; + private System.Windows.Forms.MenuStrip menuStrip1; + private System.Windows.Forms.ToolStripMenuItem filesToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem loadToolStripMenuItem; + private System.Windows.Forms.OpenFileDialog openFileDialog; + private System.Windows.Forms.SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Laba1Loco/Laba1Loco/FormTrainCollection.cs b/Laba1Loco/Laba1Loco/FormTrainCollection.cs index f1f84a0..8abcb65 100644 --- a/Laba1Loco/Laba1Loco/FormTrainCollection.cs +++ b/Laba1Loco/Laba1Loco/FormTrainCollection.cs @@ -189,5 +189,50 @@ namespace Laba1Loco } } + + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + ReloadObjects(); + } + } } diff --git a/Laba1Loco/Laba1Loco/FormTrainCollection.resx b/Laba1Loco/Laba1Loco/FormTrainCollection.resx index 1af7de1..8a67c96 100644 --- a/Laba1Loco/Laba1Loco/FormTrainCollection.resx +++ b/Laba1Loco/Laba1Loco/FormTrainCollection.resx @@ -1,17 +1,17 @@  - @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 271, 17 + \ No newline at end of file diff --git a/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs b/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs index adf066c..28e4cc4 100644 --- a/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs +++ b/Laba1Loco/Laba1Loco/TrainsGenericCollection.cs @@ -11,6 +11,10 @@ namespace Laba1Loco where T : DrawingTrain where U : IMoveableObject { + /// + /// Получение объектов коллекции + /// + public IEnumerable GetTrains => _collection.GetTrains(); /// /// Ширина окна прорисовки /// diff --git a/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs b/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs index c581eb7..a4df6aa 100644 --- a/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs +++ b/Laba1Loco/Laba1Loco/TrainsGenericStorage.cs @@ -1,13 +1,105 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Laba1Loco { internal class TrainsGenericStorage { + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по поездам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при cохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new StringBuilder(); + foreach (KeyValuePair> record in _trainStorages) + { + StringBuilder records = new StringBuilder(); + foreach (DrawingTrain elem in record.Value.GetTrains) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter sr = new StreamWriter(filename)) + { + sr.Write($"TrainStorage{Environment.NewLine}{data}"); + } + return true; + } + /// + /// Загрузка информации по поездам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка призагрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string s = sr.ReadLine(); + if (s == null || s.Length == 0) + return false; + if (!s.StartsWith("TrainStorage"))//если нет такой записи, то это не те данные + return false; + _trainStorages.Clear(); + s = sr.ReadLine(); + while (s != null && s.Length != 0) + { + string[] record = s.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + s = sr.ReadLine(); + if (record.Length != 2) + { + continue; + } + TrainsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set.Reverse()) + { + DrawingTrain train = elem?.CreateDrawingTrain(_separatorForObject, _pictureWidth, _pictureHeight); + if (train != null) + { + if (collection + train == -1) + { + return false; + } + } + } + _trainStorages.Add(record[0], collection); + } + } + return true; + } /// /// Словарь (хранилище) ///