From 032bf5d2a245dcc0156b600ff415f29c55f684da Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 18:29:36 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20ExtentionLocomotive,=20=D1=80?= =?UTF-8?q?=D0=B5=D1=88=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB?= =?UTF-8?q?=D0=B5=D0=BC=D0=B0=20=D1=81=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20=D1=86=D0=B2=D0=B5=D1=82=D0=BE=D0=B2=20=D0=B2=20?= =?UTF-8?q?FormLocomotiveConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotive/Locomotive/ExtentionLocomotive.cs | 48 +++++++++++++++++++ .../FormLocomotiveConfig.Designer.cs | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Locomotive/Locomotive/ExtentionLocomotive.cs diff --git a/Locomotive/Locomotive/ExtentionLocomotive.cs b/Locomotive/Locomotive/ExtentionLocomotive.cs new file mode 100644 index 0000000..056e5f1 --- /dev/null +++ b/Locomotive/Locomotive/ExtentionLocomotive.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomotive +{ + internal static class ExtentionLocomotive + { + private static readonly char _separatorForObject = ':'; + public static string getDataForSave(this DrawningLocomotive drawningLocomotive) + { + var locomotive = drawningLocomotive.Locomotive; + var str = $"{locomotive.Speed}{_separatorForObject}{locomotive.Weight}{_separatorForObject}{locomotive.BodyColor.Name}"; + if (locomotive is not EntityWarmlyLocomotive warmlyLocomotive) + { + return str; + } + return $"{str}{_separatorForObject}{warmlyLocomotive.ExtraColor.Name}{_separatorForObject}{warmlyLocomotive.Pipe}{_separatorForObject}{warmlyLocomotive.FuelStorage}"; + } + + public static DrawningLocomotive createDrawningLocomotive(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningLocomotive( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]) + ); + } + if (strs.Length == 6) + { + return new DrawningWarmlyLocomotive( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]) + ); + } + return null; + } + } +} diff --git a/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs b/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs index 77cf4bf..d100b8e 100644 --- a/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs +++ b/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs @@ -159,7 +159,7 @@ // // panelGreen // - this.panelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); + this.panelGreen.BackColor = System.Drawing.Color.Green; this.panelGreen.Location = new System.Drawing.Point(72, 31); this.panelGreen.Name = "panelGreen"; this.panelGreen.Size = new System.Drawing.Size(43, 40); -- 2.25.1 From 6f35bd337be03f8162f5078cdd5957d6b971000e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 18:37:11 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=B0=20IDrawningObject=20=D0=B8=20=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?DrawningObjectLocomotive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotive/Locomotive/DrawningObjectLocomotive.cs | 4 +++- Locomotive/Locomotive/IDrawningObject.cs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Locomotive/Locomotive/DrawningObjectLocomotive.cs b/Locomotive/Locomotive/DrawningObjectLocomotive.cs index 4a518dc..ab86474 100644 --- a/Locomotive/Locomotive/DrawningObjectLocomotive.cs +++ b/Locomotive/Locomotive/DrawningObjectLocomotive.cs @@ -26,7 +26,6 @@ namespace Locomotive { return _locomotive?.GetCurrentPosition() ?? default; } - public void MoveObject(Direction direction) { _locomotive?.MoveTransport(direction); @@ -37,5 +36,8 @@ namespace Locomotive _locomotive?.SetPosition(x, y, width, height); } + public string getInfo() => _locomotive?.getDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.createDrawningLocomotive()); } } diff --git a/Locomotive/Locomotive/IDrawningObject.cs b/Locomotive/Locomotive/IDrawningObject.cs index 5a7c000..37473dd 100644 --- a/Locomotive/Locomotive/IDrawningObject.cs +++ b/Locomotive/Locomotive/IDrawningObject.cs @@ -19,5 +19,7 @@ namespace Locomotive /// Получение текущей позиции объекта (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + // Получение информации по объекту + string getInfo(); } } -- 2.25.1 From dded2fe5dca36bc504f6ebffc19f4545bfcb32ea Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 18:46:34 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20?= =?UTF-8?q?MapWithSetLocomotivesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetLocomotivesGeneric.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs index 8f1b26e..fc6ecfa 100644 --- a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs +++ b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs @@ -158,5 +158,26 @@ namespace Locomotive } } + /// Получение данных в виде строки + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var locomotive in _setLocomotives.GetLocomotives()) + { + data += $"{locomotive.getInfo()}{separatorData}"; + } + return data; + } + + /// Загрузка списка из массива строк + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setLocomotives.Insert(DrawningObjectLocomotive.Create(rec) as T); + } + } + + } } -- 2.25.1 From 11c83fd8b0bdbfc5c489cedbf2e16e9913adb1a8 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 19:00:29 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=BF=D0=B8=D1=81=D0=B8,=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=B2=20MapsCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotive/Locomotive/MapsCollection.cs | 86 +++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/Locomotive/Locomotive/MapsCollection.cs b/Locomotive/Locomotive/MapsCollection.cs index 1ea1022..50d3d9e 100644 --- a/Locomotive/Locomotive/MapsCollection.cs +++ b/Locomotive/Locomotive/MapsCollection.cs @@ -9,7 +9,7 @@ namespace Locomotive internal class MapsCollection { /// Словарь (хранилище) с картами - readonly Dictionary> _mapStorages; /// Возвращение списка названий карт public List Keys => _mapStorages.Keys.ToList(); @@ -17,11 +17,16 @@ namespace Locomotive private readonly int _pictureWidth; /// Высота окна отрисовки private readonly int _pictureHeight; + + // Сепараторы + private readonly char separatorDict = '|'; + private readonly char separatorData = ';'; + /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new Dictionary>(); + MapWithSetLocomotivesGeneric>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -29,7 +34,7 @@ namespace Locomotive public void AddMap(string name, AbstractMap map) { // Логика для добавления - if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); } /// Удаление карты public void DelMap(string name) @@ -38,7 +43,7 @@ namespace Locomotive if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); } /// Доступ к парковке - public MapWithSetLocomotivesGeneric this[string ind] + public MapWithSetLocomotivesGeneric this[string ind] { get { @@ -48,5 +53,78 @@ namespace Locomotive } } + + /// Метод записи информации в файл + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + /// Сохранение информации по автомобилям в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict,separatorData)}{Environment.NewLine}", fs); + } + } + return true; + } + /// Загрузка нформации по автомобилям на парковках из файла + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "Simple Map": + map = new SimpleMap(); + break; + case "Spike Map": + map = new SpikeMap(); + break; + case "Rail Map": + map = new RailroadMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } + + } } -- 2.25.1 From 75671e027227e87723014aac463f423cd38a344e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 19:14:59 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D0=9E=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2.=20=D0=9E=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=D0=B4=20=D1=82?= =?UTF-8?q?=D1=80=D0=B5=D0=B1=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotives.Designer.cs | 69 +++++++++++++++++-- .../Locomotive/FormMapWithSetLocomotives.cs | 31 +++++++++ .../Locomotive/FormMapWithSetLocomotives.resx | 12 ++++ 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/Locomotive/Locomotive/FormMapWithSetLocomotives.Designer.cs b/Locomotive/Locomotive/FormMapWithSetLocomotives.Designer.cs index c3c2388..5242822 100644 --- a/Locomotive/Locomotive/FormMapWithSetLocomotives.Designer.cs +++ b/Locomotive/Locomotive/FormMapWithSetLocomotives.Designer.cs @@ -45,9 +45,16 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddLocomotive = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loadFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBoxTools.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -63,9 +70,9 @@ this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddLocomotive); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(580, 0); + this.groupBoxTools.Location = new System.Drawing.Point(580, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(220, 546); + this.groupBoxTools.Size = new System.Drawing.Size(220, 540); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; @@ -228,19 +235,62 @@ // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(580, 546); + this.pictureBox.Size = new System.Drawing.Size(580, 540); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.fileToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(800, 28); + this.menuStrip.TabIndex = 2; + // + // fileToolStripMenuItem + // + this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveToolStripMenuItem, + this.loadToolStripMenuItem}); + this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24); + this.fileToolStripMenuItem.Text = "File"; + // + // saveToolStripMenuItem + // + this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + this.saveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.saveToolStripMenuItem.Text = "Save"; + 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.Text = "Load"; + this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click); + // + // loadFileDialog + // + this.loadFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetLocomotives // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 546); + this.ClientSize = new System.Drawing.Size(800, 568); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetLocomotives"; this.Text = "FormMapWithSetLocomotives"; this.groupBoxTools.ResumeLayout(false); @@ -248,7 +298,10 @@ this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -271,5 +324,11 @@ private Button buttonAddMap; private Button buttonDeleteMap; private ListBox listBoxMaps; + private MenuStrip menuStrip; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private OpenFileDialog loadFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs index 4c9a0ad..69b44ff 100644 --- a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs +++ b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs @@ -186,5 +186,36 @@ namespace Locomotive pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Saved successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Saving failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void loadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (loadFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadData(loadFileDialog.FileName)) + { + MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Loading failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + ReloadMaps(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + } } } diff --git a/Locomotive/Locomotive/FormMapWithSetLocomotives.resx b/Locomotive/Locomotive/FormMapWithSetLocomotives.resx index f298a7b..a0c7756 100644 --- a/Locomotive/Locomotive/FormMapWithSetLocomotives.resx +++ b/Locomotive/Locomotive/FormMapWithSetLocomotives.resx @@ -57,4 +57,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 144, 17 + + + 311, 17 + + + 33 + \ No newline at end of file -- 2.25.1 From 630de13d2cc3da4a2cc7854924476ad3b089875e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 4 Nov 2022 19:32:14 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D0=B1=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B,=20=D0=BB=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=20=D0=B3=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotive/Locomotive/MapsCollection.cs | 81 +++++++++++-------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/Locomotive/Locomotive/MapsCollection.cs b/Locomotive/Locomotive/MapsCollection.cs index 50d3d9e..a5cbd09 100644 --- a/Locomotive/Locomotive/MapsCollection.cs +++ b/Locomotive/Locomotive/MapsCollection.cs @@ -53,14 +53,7 @@ namespace Locomotive } } - - /// Метод записи информации в файл - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); - } - /// Сохранение информации по автомобилям в хранилище в файл + /// Сохранение информации по локомотивам в хранилище в файл public bool SaveData(string filename) { if (File.Exists(filename)) @@ -68,63 +61,63 @@ namespace Locomotive File.Delete(filename); } using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.WriteLine("MapsCollection"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict,separatorData)}{Environment.NewLine}", fs); + sw.WriteLine( + $"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" + ); } } return true; } - /// Загрузка нформации по автомобилям на парковках из файла + /// Загрузка нформации по локомотивам в депо из файла public bool LoadData(string filename) { if (!File.Exists(filename)) { return false; } - string bufferTextFromFile = ""; using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string curLine = sr.ReadLine(); + + if (!curLine.Contains("MapsCollection")) { - bufferTextFromFile += temp.GetString(b); + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - switch (elem[1]) + + _mapStorages.Clear(); + while ((curLine = sr.ReadLine()) != null) { - case "Simple Map": - map = new SimpleMap(); - break; - case "Spike Map": - map = new SpikeMap(); - break; - case "Rail Map": - map = new RailroadMap(); - break; + var elems = curLine.Split(separatorDict); + AbstractMap map = null; + + switch (elems[1]) + { + case "Simple Map": + map = new SimpleMap(); + break; + case "Spike Map": + map = new SpikeMap(); + break; + case "Rail Map": + map = new RailroadMap(); + break; + } + + _mapStorages.Add(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elems[0]].LoadData(elems[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } - _mapStorages.Add(elem[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + + return true; } - return true; + + } - - } } -- 2.25.1 From 7f15f74c84aae140c658a95bbab7357ab199abf5 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 8 Nov 2022 14:56:42 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs index fc6ecfa..f132ff7 100644 --- a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs +++ b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs @@ -172,7 +172,7 @@ namespace Locomotive /// Загрузка списка из массива строк public void LoadData(string[] records) { - foreach (var rec in records) + foreach (var rec in records.Reverse()) { _setLocomotives.Insert(DrawningObjectLocomotive.Create(rec) as T); } -- 2.25.1