From 4664385343fd3d408136ca8fee138539fd1c1b00 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Sun, 6 Nov 2022 17:17:49 +0400 Subject: [PATCH 1/5] init full --- .../DrawningObjectBoat.cs | 2 + .../ExtensionBoat.cs | 57 +++++++++++ .../FormMapWithSetBoats.Designer.cs | 72 ++++++++++++-- .../FormMapWithSetBoats.cs | 41 +++++++- .../FormMapWithSetBoats.resx | 9 ++ .../IDrawningObject.cs | 5 + .../MapWithSetBoatsGeneric.cs | 25 +++++ .../MapsCollection.cs | 94 ++++++++++++++++++- 8 files changed, 292 insertions(+), 13 deletions(-) create mode 100644 PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs index ac982cb..6dc06c4 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs @@ -22,6 +22,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { return _boat?.GetCurrentPosition() ?? default; } + public string GetInfo() => _boat?.GetDataForSave(); public void MoveObject(Direction direction) { _boat.MoveTransport(direction); @@ -30,5 +31,6 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { _boat.SetPosition(x, y, width, height); } + public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat()); } } diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs new file mode 100644 index 0000000..5002401 --- /dev/null +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/ExtensionBoat.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Drawing.Text; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base +{ + /// + /// Расширение для класса DrawningBoat + /// + internal static class ExtentionBoat + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningBoat CreateDrawningBoat(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 7) + { + return new DrawningSpeedboat(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])); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave (this DrawningBoat drawningBoat) + { + var boat = drawningBoat.Boat; + var str = $"{boat.Speed}{_separatorForObject}{boat.Weight}{_separatorForObject}{boat.BodyColor.Name}"; + if (boat is not EntitySpeedboat speedboat) + { + return str; + } + return $"{str}{_separatorForObject}{speedboat.DopColor.Name}{_separatorForObject}{speedboat.BodyKit}{_separatorForObject}{speedboat.Wing}{_separatorForObject}{speedboat.SportLine}"; + } + } +} diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs index 837846c..531b27e 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs @@ -45,9 +45,16 @@ this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonAddBoat = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBoxTools.SuspendLayout(); this.groupBoxMap.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -63,9 +70,9 @@ this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddBoat); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(690, 0); + this.groupBoxTools.Location = new System.Drawing.Point(690, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 587); + this.groupBoxTools.Size = new System.Drawing.Size(204, 563); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -167,7 +174,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 537); + this.buttonDown.Location = new System.Drawing.Point(91, 513); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -179,7 +186,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 537); + this.buttonRight.Location = new System.Drawing.Point(127, 513); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -191,7 +198,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 537); + this.buttonLeft.Location = new System.Drawing.Point(55, 513); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -203,7 +210,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 501); + this.buttonUp.Location = new System.Drawing.Point(91, 477); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -233,12 +240,50 @@ // 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, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(690, 587); + this.pictureBox.Size = new System.Drawing.Size(690, 563); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(894, 24); + this.menuStrip.TabIndex = 2; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Text = "Загрузка"; + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetBoats // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -246,6 +291,8 @@ this.ClientSize = new System.Drawing.Size(894, 587); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetBoats"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); @@ -253,7 +300,10 @@ this.groupBoxMap.ResumeLayout(false); this.groupBoxMap.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -276,5 +326,11 @@ private ListBox listBoxMaps; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs index 721c00c..497cf9b 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs @@ -1,4 +1,5 @@ -using static System.Windows.Forms.DataFormats; +using System.Windows.Forms; +using static System.Windows.Forms.DataFormats; namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { @@ -211,5 +212,43 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base 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("Сохранение прошло успешно", "Результат", 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 (_mapsCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } \ No newline at end of file diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx index f298a7b..934ed35 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs index 282547e..e2ce4e7 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs @@ -36,6 +36,11 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } \ No newline at end of file diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs index 467690d..0f6e139 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs @@ -109,6 +109,31 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base return new(_pictureWidth, _pictureHeight); } /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var boat in _setBoats.GetBoats()) + { + data += $"{boat.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setBoats.Insert(DrawningObjectBoat.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs index c94f3cc..0a95df3 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs @@ -14,7 +14,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -28,13 +28,21 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// private readonly int _pictureHeight; /// + /// Разделитель для записи ифнормации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// /// /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -45,7 +53,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// Карта public void AddMap(string name, AbstractMap map) { - _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -60,7 +68,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// /// /// - public MapWithSetBoatsGeneric this[string ind] + public MapWithSetBoatsGeneric this[string ind] { get { @@ -71,5 +79,83 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base return null; } } + /// + /// Метод записи информации в файл + /// + /// Строка, которую следует записать + /// Поток для записи + 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 "SimpleMap": + map = new SimpleMap(); + break; + case "MySecondMap": + map = new MySecondMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } } } -- 2.25.1 From 53a65a328eb8ca55c1e5ed6863a405bd814ec245 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Sun, 6 Nov 2022 19:21:45 +0400 Subject: [PATCH 2/5] final --- .../FormMapWithSetBoats.Designer.cs | 2 + .../FormMapWithSetBoats.cs | 1 + .../MapsCollection.cs | 66 +++++++++---------- ok.txt | 2 + 4 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 ok.txt diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs index 531b27e..c4ab4c3 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs @@ -269,12 +269,14 @@ this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // // openFileDialog // diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs index 497cf9b..0732c8a 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs @@ -242,6 +242,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { if (_mapsCollection.LoadData(openFileDialog.FileName)) { + ReloadMaps(); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } else diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs index 0a95df3..21d785c 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs @@ -101,11 +101,12 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base File.Delete(filename); } using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(fs)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.WriteLine($"MapsCollection{Environment.NewLine}"); 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)}{Environment.NewLine}"); } } return true; @@ -121,41 +122,40 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str; + int count = 0; + while ((str = sr.ReadLine()) != null) { - bufferTextFromFile += temp.GetString(b); + if (count == 0) + { + if (!str.Contains("MapsCollection")) + { + return false; + } else + { + count++; + continue; + } + } + _mapStorages.Clear(); + var elem = str.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MySecondMap": + map = new MySecondMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } + return true; } - 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 "SimpleMap": - map = new SimpleMap(); - break; - case "MySecondMap": - map = new MySecondMap(); - break; - } - _mapStorages.Add(elem[0], new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); - } - return true; } } } diff --git a/ok.txt b/ok.txt new file mode 100644 index 0000000..6f43650 --- /dev/null +++ b/ok.txt @@ -0,0 +1,2 @@ +MapsCollection +ok|SimpleMap|100:100:Blue;100:100:Yellow:Blue:False:True:True; -- 2.25.1 From 5be7b09d73e646681d1b57d395cfab26d72eeffd Mon Sep 17 00:00:00 2001 From: Zyzf Date: Mon, 7 Nov 2022 10:29:23 +0400 Subject: [PATCH 3/5] done --- PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs | 1 - PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs | 7 ++++--- ok.txt | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs index 790b152..b530db9 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs @@ -19,6 +19,5 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base Weight = weight <= 0 ? rnd.Next(30, 100) : weight; BodyColor = bodyColor; } - } } diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs index 21d785c..00e41fa 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapsCollection.cs @@ -103,10 +103,10 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base using (FileStream fs = new(filename, FileMode.Create)) using (StreamWriter sw = new StreamWriter(fs)) { - sw.WriteLine($"MapsCollection{Environment.NewLine}"); + sw.WriteLine("MapsCollection"); foreach (var storage in _mapStorages) { - sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); } } return true; @@ -126,8 +126,10 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { string str; int count = 0; + _mapStorages.Clear(); while ((str = sr.ReadLine()) != null) { + if (str == "") { break; } if (count == 0) { if (!str.Contains("MapsCollection")) @@ -139,7 +141,6 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base continue; } } - _mapStorages.Clear(); var elem = str.Split(separatorDict); AbstractMap map = null; switch (elem[1]) diff --git a/ok.txt b/ok.txt index 6f43650..35f65f9 100644 --- a/ok.txt +++ b/ok.txt @@ -1,2 +1,6 @@ MapsCollection -ok|SimpleMap|100:100:Blue;100:100:Yellow:Blue:False:True:True; + +ok|SimpleMap|100:100:Gray;100:100:Yellow:Purple:False:True:True;100:100:Black:White:True:False:False; + +ok1|MySecondMap|100:100:Blue:Green:False:True:False;100:100:Blue; + -- 2.25.1 From aa2ee20a5b42a1b62ae5af5a9cc32903ae59c99d Mon Sep 17 00:00:00 2001 From: Zyzf Date: Mon, 7 Nov 2022 10:29:54 +0400 Subject: [PATCH 4/5] rm txts --- ok.txt | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 ok.txt diff --git a/ok.txt b/ok.txt deleted file mode 100644 index 35f65f9..0000000 --- a/ok.txt +++ /dev/null @@ -1,6 +0,0 @@ -MapsCollection - -ok|SimpleMap|100:100:Gray;100:100:Yellow:Purple:False:True:True;100:100:Black:White:True:False:False; - -ok1|MySecondMap|100:100:Blue:Green:False:True:False;100:100:Blue; - -- 2.25.1 From df9723e9c726f1355ecc2e7c263244df37eda7c8 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Mon, 7 Nov 2022 11:00:53 +0400 Subject: [PATCH 5/5] for pull rq --- PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs index b530db9..f53a5f0 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/EntityBoat.cs @@ -19,5 +19,6 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base Weight = weight <= 0 ? rnd.Next(30, 100) : weight; BodyColor = bodyColor; } + } } -- 2.25.1