From 6f5482f0afacb1735f88f282634d9148ebccf3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Mon, 20 Nov 2023 23:21:11 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= =?UTF-8?q?=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 14 ++++++++++++++ Sailboat/Sailboat/DrawingSailboat.cs | 5 +++++ Sailboat/Sailboat/EntityBoat.cs | 3 +-- Sailboat/Sailboat/EntitySailboat.cs | 3 +-- Sailboat/Sailboat/FormBoatConfig.cs | 4 ++-- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs index daa3898..26dd29e 100644 --- a/Sailboat/Sailboat/DrawingBoat.cs +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -144,6 +144,20 @@ namespace Sailboat.DrawingObjects g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40); } + public void SetColor(Color color) + { + if (EntityBoat == null) + { + return; + } + EntityBoat.BodyColor = color; + } + + public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight) + { + _pictureWidth = pictureBoxWidth; + _pictureHeight = pictureBoxHeight; + } } } diff --git a/Sailboat/Sailboat/DrawingSailboat.cs b/Sailboat/Sailboat/DrawingSailboat.cs index 130b9cc..4417181 100644 --- a/Sailboat/Sailboat/DrawingSailboat.cs +++ b/Sailboat/Sailboat/DrawingSailboat.cs @@ -64,5 +64,10 @@ namespace Sailboat.DrawingObjects g.DrawLine(pen, new Point(_startPosX + 65, _startPosY + 120), new Point(_startPosX + 65, _startPosY)); } } + + public void SetAddColor(Color color) + { + ((EntitySailboat)EntityBoat).AdditionalColor = color; + } } } diff --git a/Sailboat/Sailboat/EntityBoat.cs b/Sailboat/Sailboat/EntityBoat.cs index 7312ee4..8db9e23 100644 --- a/Sailboat/Sailboat/EntityBoat.cs +++ b/Sailboat/Sailboat/EntityBoat.cs @@ -10,8 +10,7 @@ namespace Sailboat.Entities { public int Speed { get; private set; } public double Weight { get; private set; } - public Color BodyColor { get; private set; } - public void setBodyColor(Color color) { BodyColor = color; } + public Color BodyColor { get; set; } public double Step => (double)Speed * 100 / Weight; public EntityBoat(int speed, double weight, Color bodyColor) { diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index af78d9b..7f58f36 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -8,8 +8,7 @@ namespace Sailboat.Entities { public class EntitySailboat : EntityBoat { - public Color AdditionalColor { get; private set; } - public void setAdditionalColor(Color color) { AdditionalColor = color; } + public Color AdditionalColor { get; set; } public bool Hull { get; private set; } public bool Sail { get; private set; } public EntitySailboat(int speed, double weight, Color bodyColor, Color diff --git a/Sailboat/Sailboat/FormBoatConfig.cs b/Sailboat/Sailboat/FormBoatConfig.cs index a79335f..bb461bd 100644 --- a/Sailboat/Sailboat/FormBoatConfig.cs +++ b/Sailboat/Sailboat/FormBoatConfig.cs @@ -101,12 +101,12 @@ namespace Sailboat switch (((Label)sender).Name) { case "labelColor": - _boat.EntityBoat.setBodyColor((Color)e.Data.GetData(typeof(Color))); + _boat.SetColor((Color)e.Data.GetData(typeof(Color))); break; case "labelAddColor": if (!(_boat is DrawingSailboat)) return; - (_boat.EntityBoat as EntitySailboat).setAdditionalColor((Color)e.Data.GetData(typeof(Color))); + (_boat as DrawingSailboat).SetAddColor((Color)e.Data.GetData(typeof(Color))); break; } DrawBoat(); -- 2.25.1 From ee6765df72a0da5c6056c02988cb6e1d75767767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Tue, 21 Nov 2023 16:50:10 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= =?UTF-8?q?=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericCollection.cs | 1 + Sailboat/Sailboat/BoatsGenericStorage.cs | 108 +++++++++++++++++- Sailboat/Sailboat/ExtentionDrawingBoat.cs | 64 +++++++++++ .../Sailboat/FormBoatCollection.Designer.cs | 83 ++++++++++++-- Sailboat/Sailboat/FormBoatCollection.cs | 33 ++++++ Sailboat/Sailboat/FormBoatCollection.resx | 15 +++ 6 files changed, 293 insertions(+), 11 deletions(-) create mode 100644 Sailboat/Sailboat/ExtentionDrawingBoat.cs diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 7535a70..4736a47 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -37,6 +37,7 @@ namespace Sailboat.Generics /// Набор объектов /// private readonly SetGeneric _collection; + public IEnumerable GetBoats => _collection.GetBoats(); /// /// Конструктор /// diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 8dd5aaa..8b68b99 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -28,6 +28,18 @@ namespace Sailboat.Generics /// private readonly int _pictureHeight; /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// /// Конструктор /// /// @@ -68,8 +80,7 @@ namespace Sailboat.Generics /// /// /// - public BoatsGenericCollection? - this[string ind] + public BoatsGenericCollection? this[string ind] { get { @@ -80,5 +91,98 @@ namespace Sailboat.Generics return null; } } + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _boatStorages) + { + StringBuilder records = new(); + foreach (DrawingBoat? elem in record.Value.GetBoats) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка призагрузке данных + 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 == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("CarStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _boatStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + BoatsGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingBoat? boat = + elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (boat != null) + { + if (!(collection + boat)) + { + return false; + } + } + } + _boatStorages.Add(record[0], collection); + } + return true; + } } } diff --git a/Sailboat/Sailboat/ExtentionDrawingBoat.cs b/Sailboat/Sailboat/ExtentionDrawingBoat.cs new file mode 100644 index 0000000..190503f --- /dev/null +++ b/Sailboat/Sailboat/ExtentionDrawingBoat.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects +{ + public static class ExtentionDrawingBoat + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingBoat? CreateDrawingBoat(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingBoat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingSailboat(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), + width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingBoat drawingBoat, char separatorForObject) + { + var boat = drawingBoat.EntityBoat; + if (boat == null) + { + return string.Empty; + } + + var str = $"{boat.Speed}{separatorForObject}{boat.Weight}{separatorForObject}{boat.BodyColor.Name}"; + + if (boat is not EntitySailboat sailboat) + { + return str; + } + return $"{str}{separatorForObject}{sailboat.AdditionalColor.Name}{separatorForObject}{sailboat.Hull}{separatorForObject}{sailboat.Sail}"; + } + } +} diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index a3be7c3..871bc2d 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -39,16 +39,24 @@ this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.buttonRemoveBoat = new System.Windows.Forms.Button(); this.buttonAddBoat = new System.Windows.Forms.Button(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + 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(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelTools.SuspendLayout(); this.panelCollection.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(750, 600); + this.pictureBoxCollection.Size = new System.Drawing.Size(750, 720); this.pictureBoxCollection.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; @@ -61,9 +69,10 @@ this.panelTools.Controls.Add(this.buttonRefreshCollection); this.panelTools.Controls.Add(this.buttonRemoveBoat); this.panelTools.Controls.Add(this.buttonAddBoat); - this.panelTools.Location = new System.Drawing.Point(756, 12); + this.panelTools.Controls.Add(this.menuStrip); + this.panelTools.Location = new System.Drawing.Point(756, 0); this.panelTools.Name = "panelTools"; - this.panelTools.Size = new System.Drawing.Size(209, 588); + this.panelTools.Size = new System.Drawing.Size(209, 722); this.panelTools.TabIndex = 1; // // panelCollection @@ -73,7 +82,7 @@ this.panelCollection.Controls.Add(this.listBoxStorages); this.panelCollection.Controls.Add(this.buttonAddObject); this.panelCollection.Controls.Add(this.textBoxStorageName); - this.panelCollection.Location = new System.Drawing.Point(16, 15); + this.panelCollection.Location = new System.Drawing.Point(20, 166); this.panelCollection.Name = "panelCollection"; this.panelCollection.Size = new System.Drawing.Size(181, 287); this.panelCollection.TabIndex = 4; @@ -117,14 +126,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(45, 416); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 567); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27); this.maskedTextBoxNumber.TabIndex = 3; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(16, 531); + this.buttonRefreshCollection.Location = new System.Drawing.Point(20, 682); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(180, 34); this.buttonRefreshCollection.TabIndex = 2; @@ -134,7 +143,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 469); + this.buttonRemoveBoat.Location = new System.Drawing.Point(21, 620); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(180, 34); this.buttonRemoveBoat.TabIndex = 1; @@ -144,7 +153,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(16, 339); + this.buttonAddBoat.Location = new System.Drawing.Point(20, 490); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(180, 34); this.buttonAddBoat.TabIndex = 0; @@ -152,13 +161,60 @@ this.buttonAddBoat.UseVisualStyleBackColor = true; this.buttonAddBoat.Click += new System.EventHandler(this.buttonAddBoat_Click); // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItem1, + this.ToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(209, 28); + this.menuStrip.TabIndex = 5; + // + // toolStripMenuItem1 + // + this.toolStripMenuItem1.Name = "toolStripMenuItem1"; + this.toolStripMenuItem1.Size = new System.Drawing.Size(14, 24); + // + // 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(59, 24); + this.ToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Text = "Сохранение"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Text = "Загрузка"; + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormBoatCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(969, 604); + this.ClientSize = new System.Drawing.Size(969, 728); this.Controls.Add(this.panelTools); this.Controls.Add(this.pictureBoxCollection); + this.MainMenuStrip = this.menuStrip; this.Name = "FormBoatCollection"; this.Text = "FormBoatCollection"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -166,6 +222,8 @@ this.panelTools.PerformLayout(); this.panelCollection.ResumeLayout(false); this.panelCollection.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -184,5 +242,12 @@ private ListBox listBoxStorages; private Button buttonAddObject; private TextBox textBoxStorageName; + private MenuStrip menuStrip; + private ToolStripMenuItem toolStripMenuItem1; + private ToolStripMenuItem ToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index a86d336..8ea390a 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -165,5 +165,38 @@ namespace Sailboat ReloadObjects(); } } + + ///// + ///// Обработка нажатия "Сохранение" + ///// + ///// + ///// + //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) + //{ + // // TODO продумать логику + //} + } } diff --git a/Sailboat/Sailboat/FormBoatCollection.resx b/Sailboat/Sailboat/FormBoatCollection.resx index f298a7b..66d62b4 100644 --- a/Sailboat/Sailboat/FormBoatCollection.resx +++ b/Sailboat/Sailboat/FormBoatCollection.resx @@ -57,4 +57,19 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 17, 17 + + + 144, 0 + + + 144, 0 + + + 25 + \ No newline at end of file -- 2.25.1 From 813efb47b7707aca4f96e23649a7193d847b1353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Tue, 21 Nov 2023 17:27:23 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericStorage.cs | 10 ++- Sailboat/Sailboat/ExtentionDrawingBoat.cs | 2 +- .../Sailboat/FormBoatCollection.Designer.cs | 2 + Sailboat/Sailboat/FormBoatCollection.cs | 63 ++++++++++--------- Sailboat/Sailboat/FormBoatCollection.resx | 3 - 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 8b68b99..c282fdc 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -119,7 +119,7 @@ namespace Sailboat.Generics } using FileStream fs = new(filename, FileMode.Create); byte[] info = new - UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); + UTF8Encoding(true).GetBytes($"BoatStorage{Environment.NewLine}{data}"); fs.Write(info, 0, info.Length); return true; } @@ -150,7 +150,7 @@ namespace Sailboat.Generics { return false; } - if (!strs[0].StartsWith("CarStorage")) + if (!strs[0].StartsWith("BoatStorage")) { //если нет такой записи, то это не те данные return false; @@ -158,8 +158,7 @@ namespace Sailboat.Generics _boatStorages.Clear(); foreach (string data in strs) { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { continue; @@ -170,8 +169,7 @@ namespace Sailboat.Generics StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - DrawingBoat? boat = - elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); if (boat != null) { if (!(collection + boat)) diff --git a/Sailboat/Sailboat/ExtentionDrawingBoat.cs b/Sailboat/Sailboat/ExtentionDrawingBoat.cs index 190503f..07da6f2 100644 --- a/Sailboat/Sailboat/ExtentionDrawingBoat.cs +++ b/Sailboat/Sailboat/ExtentionDrawingBoat.cs @@ -26,7 +26,7 @@ namespace Sailboat.DrawingObjects return new DrawingBoat(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); } - if (strs.Length == 7) + if (strs.Length == 6) { return new DrawingSailboat(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index 871bc2d..f18e0c1 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -191,12 +191,14 @@ this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.SaveToolStripMenuItem.Text = "Сохранение"; + 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 = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // // openFileDialog // diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 8ea390a..1aa637c 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -166,37 +166,40 @@ namespace Sailboat } } - ///// - ///// Обработка нажатия "Сохранение" - ///// - ///// - ///// - //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 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) - //{ - // // TODO продумать логику - //} + 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/Sailboat/Sailboat/FormBoatCollection.resx b/Sailboat/Sailboat/FormBoatCollection.resx index 66d62b4..3198a4b 100644 --- a/Sailboat/Sailboat/FormBoatCollection.resx +++ b/Sailboat/Sailboat/FormBoatCollection.resx @@ -60,9 +60,6 @@ 17, 17 - - 17, 17 - 144, 0 -- 2.25.1 From 5ef5d78937fd3fe2f11eea57730e001361cf5c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 22 Nov 2023 11:07:39 +0400 Subject: [PATCH 4/8] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericStorage.cs | 76 ++++++++++++------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index c282fdc..266823c 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -117,10 +117,10 @@ namespace Sailboat.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"BoatStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write($"PlaneStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -134,53 +134,51 @@ namespace Sailboat.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + + using (StreamReader fs = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = fs.ReadLine(); + if (str == null || str.Length == 0) { - bufferTextFromFile += temp.GetString(b); + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("BoatStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _boatStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + if (!str.StartsWith("BoatStorage")) { - continue; + return false; } - BoatsGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + + _boatStorages.Clear(); + string strs = ""; + + while ((strs = fs.ReadLine()) != null) { - DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); - if (boat != null) + if (strs == null) { - if (!(collection + boat)) + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (boat != null) { - return false; + if (!(collection + boat)) + { + return false; + } } } + _boatStorages.Add(record[0], collection); } - _boatStorages.Add(record[0], collection); + return true; } - return true; } } } -- 2.25.1 From 8791edafb80f98e2000558e00591b4dfd13ca0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Tue, 5 Dec 2023 23:30:55 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=206=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/EntityBoat.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Sailboat/Sailboat/EntityBoat.cs b/Sailboat/Sailboat/EntityBoat.cs index 8db9e23..8797f39 100644 --- a/Sailboat/Sailboat/EntityBoat.cs +++ b/Sailboat/Sailboat/EntityBoat.cs @@ -11,6 +11,7 @@ namespace Sailboat.Entities public int Speed { get; private set; } public double Weight { get; private set; } public Color BodyColor { get; set; } + public void setBodyColor(Color color) { BodyColor = color; } public double Step => (double)Speed * 100 / Weight; public EntityBoat(int speed, double weight, Color bodyColor) { -- 2.25.1 From c9a2398e47b7792ddaa998c393b2bc3f9497add7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 6 Dec 2023 10:12:12 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D1=8B=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 14 -------------- Sailboat/Sailboat/DrawingSailboat.cs | 5 ----- Sailboat/Sailboat/EntitySailboat.cs | 1 + Sailboat/Sailboat/FormBoatConfig.cs | 4 ++-- 4 files changed, 3 insertions(+), 21 deletions(-) diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs index 26dd29e..daa3898 100644 --- a/Sailboat/Sailboat/DrawingBoat.cs +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -144,20 +144,6 @@ namespace Sailboat.DrawingObjects g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40); } - public void SetColor(Color color) - { - if (EntityBoat == null) - { - return; - } - EntityBoat.BodyColor = color; - } - - public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight) - { - _pictureWidth = pictureBoxWidth; - _pictureHeight = pictureBoxHeight; - } } } diff --git a/Sailboat/Sailboat/DrawingSailboat.cs b/Sailboat/Sailboat/DrawingSailboat.cs index 4417181..130b9cc 100644 --- a/Sailboat/Sailboat/DrawingSailboat.cs +++ b/Sailboat/Sailboat/DrawingSailboat.cs @@ -64,10 +64,5 @@ namespace Sailboat.DrawingObjects g.DrawLine(pen, new Point(_startPosX + 65, _startPosY + 120), new Point(_startPosX + 65, _startPosY)); } } - - public void SetAddColor(Color color) - { - ((EntitySailboat)EntityBoat).AdditionalColor = color; - } } } diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index 7f58f36..9865a77 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -11,6 +11,7 @@ namespace Sailboat.Entities public Color AdditionalColor { get; set; } public bool Hull { get; private set; } public bool Sail { get; private set; } + public void setAdditionalColor(Color color) { AdditionalColor = color; } public EntitySailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail) : base (speed, weight, bodyColor) { diff --git a/Sailboat/Sailboat/FormBoatConfig.cs b/Sailboat/Sailboat/FormBoatConfig.cs index bb461bd..a79335f 100644 --- a/Sailboat/Sailboat/FormBoatConfig.cs +++ b/Sailboat/Sailboat/FormBoatConfig.cs @@ -101,12 +101,12 @@ namespace Sailboat switch (((Label)sender).Name) { case "labelColor": - _boat.SetColor((Color)e.Data.GetData(typeof(Color))); + _boat.EntityBoat.setBodyColor((Color)e.Data.GetData(typeof(Color))); break; case "labelAddColor": if (!(_boat is DrawingSailboat)) return; - (_boat as DrawingSailboat).SetAddColor((Color)e.Data.GetData(typeof(Color))); + (_boat.EntityBoat as EntitySailboat).setAdditionalColor((Color)e.Data.GetData(typeof(Color))); break; } DrawBoat(); -- 2.25.1 From 8c059e02622e7cc16929a6b591d02106997e7675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 6 Dec 2023 10:17:12 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BA=D0=B8=20=D0=BB=D0=B0=D0=B1=D1=8B=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/EntitySailboat.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index 9865a77..6d8cb2c 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -9,9 +9,9 @@ namespace Sailboat.Entities public class EntitySailboat : EntityBoat { public Color AdditionalColor { get; set; } + public void setAdditionalColor(Color color) { AdditionalColor = color; } public bool Hull { get; private set; } public bool Sail { get; private set; } - public void setAdditionalColor(Color color) { AdditionalColor = color; } public EntitySailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail) : base (speed, weight, bodyColor) { -- 2.25.1 From 3ef5d8874e66df2ba450374178ef2fbf4ca176ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 6 Dec 2023 10:40:27 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BA=D0=B8=20=D0=BB=D0=B0=D0=B1=D1=8B=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/EntityBoat.cs | 2 +- Sailboat/Sailboat/EntitySailboat.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sailboat/Sailboat/EntityBoat.cs b/Sailboat/Sailboat/EntityBoat.cs index 8797f39..7312ee4 100644 --- a/Sailboat/Sailboat/EntityBoat.cs +++ b/Sailboat/Sailboat/EntityBoat.cs @@ -10,7 +10,7 @@ namespace Sailboat.Entities { public int Speed { get; private set; } public double Weight { get; private set; } - public Color BodyColor { get; set; } + public Color BodyColor { get; private set; } public void setBodyColor(Color color) { BodyColor = color; } public double Step => (double)Speed * 100 / Weight; public EntityBoat(int speed, double weight, Color bodyColor) diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index 6d8cb2c..af78d9b 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -8,7 +8,7 @@ namespace Sailboat.Entities { public class EntitySailboat : EntityBoat { - public Color AdditionalColor { get; set; } + public Color AdditionalColor { get; private set; } public void setAdditionalColor(Color color) { AdditionalColor = color; } public bool Hull { get; private set; } public bool Sail { get; private set; } -- 2.25.1