diff --git a/Liner/Drawing/ExtentionDrawingLiner.cs b/Liner/Drawing/ExtentionDrawingLiner.cs new file mode 100644 index 0000000..12aeaab --- /dev/null +++ b/Liner/Drawing/ExtentionDrawingLiner.cs @@ -0,0 +1,69 @@ +using Liner.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner.Drawing +{ + /// + /// Расширение для класса EntityLiner + /// + public static class ExtentionDrawningLiner + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingLiner? CreateDrawingLiner(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingLiner(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawingBigLiner(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 DrawingLiner drawingLiner, + char separatorForObject) + { + var liner = drawingLiner.EntityLiner; + if (liner == null) + { + return string.Empty; + } + var str = + $"{liner.Speed}{separatorForObject}{liner.Weight}{separatorForObject}{liner.BottomColor.Name}"; + if (liner is not EntityBigLiner bigLiner) + { + return str; + } + return + $"{str}{separatorForObject}{bigLiner.BodyColor.Name}{separatorForObject}{bigLiner.SwimmingPool}{separatorForObject}" + + $"{bigLiner.Deck}"; + } + } +} diff --git a/Liner/FormLinerCollection.Designer.cs b/Liner/FormLinerCollection.Designer.cs index 8e30369..054ece4 100644 --- a/Liner/FormLinerCollection.Designer.cs +++ b/Liner/FormLinerCollection.Designer.cs @@ -39,9 +39,16 @@ textBoxNumber = new TextBox(); buttonAddLiner = new Button(); pictureBoxCollection = new PictureBox(); + FileMenuStrip = new MenuStrip(); + FileToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); groupBoxTools.SuspendLayout(); Storages.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + FileMenuStrip.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -64,9 +71,9 @@ Storages.Controls.Add(buttonDeleteSet); Storages.Controls.Add(textBoxStorageName); Storages.Controls.Add(buttonAddStorage); - Storages.Location = new Point(7, 19); + Storages.Location = new Point(7, 22); Storages.Name = "Storages"; - Storages.Size = new Size(200, 276); + Storages.Size = new Size(200, 273); Storages.TabIndex = 4; Storages.TabStop = false; Storages.Text = "Sets"; @@ -147,19 +154,60 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(2, 0); + pictureBoxCollection.Location = new Point(2, 27); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(731, 565); + pictureBoxCollection.Size = new Size(731, 538); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // FileMenuStrip + // + FileMenuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); + FileMenuStrip.Location = new Point(0, 0); + FileMenuStrip.Name = "FileMenuStrip"; + FileMenuStrip.Size = new Size(951, 24); + FileMenuStrip.TabIndex = 2; + FileMenuStrip.Text = "FileMenuStrip"; + // + // FileToolStripMenuItem + // + FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + FileToolStripMenuItem.Size = new Size(40, 20); + FileToolStripMenuItem.Text = "FILE"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(105, 22); + SaveToolStripMenuItem.Text = "SAVE"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(105, 22); + LoadToolStripMenuItem.Text = "LOAD"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.FileName = "saveFile"; + saveFileDialog.Filter = "txt file | *.txt"; + // // FormLinerCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(951, 568); + Controls.Add(FileMenuStrip); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxTools); + MainMenuStrip = FileMenuStrip; Name = "FormLinerCollection"; Text = "Liner Collection"; groupBoxTools.ResumeLayout(false); @@ -167,7 +215,10 @@ Storages.ResumeLayout(false); Storages.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + FileMenuStrip.ResumeLayout(false); + FileMenuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -183,5 +234,11 @@ private Button buttonDeleteSet; private TextBox textBoxStorageName; private Button buttonAddStorage; + private MenuStrip FileMenuStrip; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Liner/FormLinerCollection.cs b/Liner/FormLinerCollection.cs index 5ea71d7..1107931 100644 --- a/Liner/FormLinerCollection.cs +++ b/Liner/FormLinerCollection.cs @@ -191,5 +191,38 @@ namespace Liner listBoxStorages.SelectedIndex = index; } } + 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); + ReloadObjects(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } + } } } diff --git a/Liner/FormLinerCollection.resx b/Liner/FormLinerCollection.resx index a395bff..b1d9aeb 100644 --- a/Liner/FormLinerCollection.resx +++ b/Liner/FormLinerCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 144, 17 + + + 284, 17 + \ No newline at end of file diff --git a/Liner/Generics/LinerGenericCollection.cs b/Liner/Generics/LinerGenericCollection.cs index ce412f2..b33de5e 100644 --- a/Liner/Generics/LinerGenericCollection.cs +++ b/Liner/Generics/LinerGenericCollection.cs @@ -50,7 +50,11 @@ namespace Liner.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } + /// + /// Получение объектов коллекции /// + public IEnumerable GetLiners => _collection.GetLiners(); + /// Перегрузка оператора сложения /// /// diff --git a/Liner/Generics/LinersGenericStorage.cs b/Liner/Generics/LinersGenericStorage.cs index 72feb94..8540114 100644 --- a/Liner/Generics/LinersGenericStorage.cs +++ b/Liner/Generics/LinersGenericStorage.cs @@ -30,6 +30,18 @@ namespace Liner.Generics /// private readonly int _pictureHeight; /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// /// Конструктор /// /// @@ -76,5 +88,93 @@ namespace Liner.Generics return null; } } + /// + /// Сохранение информации по лайнерам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _linerStorages) + { + StringBuilder records = new(); + foreach (DrawingLiner? elem in record.Value.GetLiners) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + string dataStr = data.ToString(); + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.WriteLine("LinerStorage"); + writer.WriteLine(dataStr); + } + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader reader = new StreamReader(filename)) + { + string checker = reader.ReadLine(); + if (checker == null) + { + return false; + } + if (!checker.StartsWith("LinerStorage")) + { + return false; + } + _linerStorages.Clear(); + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) + { + if (strs == null && firstinit) + return false; + if (strs == null) + break; + if (strs == string.Empty) + break; + firstinit = false; + string name = strs.Split('|')[0]; + LinerGenericCollection collection = new(_pictureWidth, _pictureHeight); + foreach (string data in strs.Split('|')[1].Split(';').Reverse()) + { + DrawingLiner? liner = + data?.CreateDrawingLiner(_separatorForObject, _pictureWidth, _pictureHeight); + if (liner != null) + { + if (collection + liner == -1) + { + return false; + } + } + + } + _linerStorages.Add(name, collection); + } + return true; + } + } } }