From c820f6ad4cb217627c973ac425dd0272db357e2b Mon Sep 17 00:00:00 2001 From: Extrimal Date: Tue, 28 Nov 2023 19:23:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=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 --- .../AircraftsGenericCollection.cs | 2 + .../AircraftsGenericStorage.cs | 79 +++++++++++++++++++ .../ExtentionDrawningAircraft.cs | 50 ++++++++++++ .../FormAircraftCollection.Designer.cs | 61 +++++++++++++- .../AircraftCarrier/FormAircraftCollection.cs | 36 +++++++++ .../FormAircraftCollection.resx | 9 +++ 6 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs index 928955c..6c104eb 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs @@ -12,6 +12,8 @@ namespace AircraftCarrier.Generics where T : DrawningAircraft where U : IMoveableObject { + /// Получение объектов коллекции + public IEnumerable GetCars => _collection.GetAircrafts(); /// Ширина окна прорисовки private readonly int _pictureWidth; /// Высота окна прорисовки diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs index 2a5bd08..a02866d 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AircraftCarrier.DrawningObjects; +using AircraftCarrier.Drawnings; using AircraftCarrier.MovementStrategy; namespace AircraftCarrier.Generics { @@ -19,6 +20,12 @@ namespace AircraftCarrier.Generics private readonly int _pictureWidth; /// Высота окна отрисовки private readonly int _pictureHeight; + /// Разделитель для записи ключа и значения элемента словаря + private static readonly char _separatorForKeyValue = '|'; + /// Разделитель для записей коллекции данных в файл + private readonly char _separatorRecords = ';'; + /// Разделитель для записи информации по объекту в файл + private static readonly char _separatorForObject = ':'; /// Конструктор public AircraftsGenericStorage(int pictureWidth, int pictureHeight) { @@ -56,5 +63,77 @@ namespace AircraftCarrier.Generics return null; } } + /// Сохранение информации по автомобилям в хранилище в файл + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _AircraftStorages) + { + StringBuilder records = new(); + foreach (DrawningAircraft? elem in record.Value.GetCars) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using StreamWriter sw = new(filename); + sw.Write($"AircraftStorage{Environment.NewLine}{data}"); + return true; + } + /// Загрузка информации по автомобилям в хранилище из файла + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("AircraftStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _AircraftStorages.Clear(); + while ((str = sr.ReadLine()) != null) + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + AircraftsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set.Reverse()) + { + DrawningAircraft? aircraft = elem?.CreateDrawningAircraft(_separatorForObject, _pictureWidth, _pictureHeight); + if (aircraft != null) + { + if (collection + aircraft == -1) + { + return false; + } + } + } + _AircraftStorages.Add(record[0], collection); + } + } + return true; + } + } } diff --git a/AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs b/AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs new file mode 100644 index 0000000..f549963 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.DrawningObjects; +using AircraftCarrier.Entities; +namespace AircraftCarrier.Drawnings +{ + /// Расширение для класса EntityCar + public static class ExtentionDrawningAircraft + { + /// Создание объекта из строки + public static DrawningAircraft? CreateDrawningAircraft(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAircraft(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningAircraftCarrier(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 DrawningAircraft drawningAircraft, char separatorForObject) + { + var aircraft = drawningAircraft.EntityAircraft; + if (aircraft == null) + { + return string.Empty; + } + var str = + $"{aircraft.Speed}{separatorForObject}{aircraft.Weight}{separatorForObject}{aircraft.BodyColor.Name}"; + if (aircraft is not EntityAircraftCarrier aircraftcarrier) + { + return str; + } + return $"{str}{separatorForObject}{aircraftcarrier.AdditionalColor.Name}{separatorForObject}{aircraftcarrier.Runway}{separatorForObject}{aircraftcarrier.Cabin}"; + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs index 365da19..fd23b43 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs @@ -41,16 +41,23 @@ ButtonRemoveAircraft = new Button(); MaskedTextBoxNumber = new MaskedTextBox(); ButtonAddAircraft = new Button(); + menuStrip1 = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); PanelTools.SuspendLayout(); PanelSets.SuspendLayout(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 27); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(793, 724); + pictureBoxCollection.Size = new Size(793, 697); pictureBoxCollection.TabIndex = 0; pictureBoxCollection.TabStop = false; // @@ -173,6 +180,46 @@ ButtonAddAircraft.UseVisualStyleBackColor = true; ButtonAddAircraft.Click += ButtonAddAircraft_Click; // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1033, 28); + menuStrip1.TabIndex = 2; + menuStrip1.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(46, 24); + fileToolStripMenuItem.Text = "File"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.Size = new Size(224, 26); + saveToolStripMenuItem.Text = "Сохранить"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.Size = new Size(224, 26); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + // // FormAircraftCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -180,6 +227,7 @@ ClientSize = new Size(1033, 724); Controls.Add(PanelTools); Controls.Add(pictureBoxCollection); + Controls.Add(menuStrip1); Name = "FormAircraftCollection"; Text = "FormAircraftCollection"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -187,7 +235,10 @@ PanelTools.PerformLayout(); PanelSets.ResumeLayout(false); PanelSets.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -205,5 +256,11 @@ private TextBox textBoxStorageName; private Button ButtonRemoveObject; private ListBox listBoxStorage; + private MenuStrip menuStrip1; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs index 90b6a67..366c969 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs @@ -141,5 +141,41 @@ namespace AircraftCarrier } pictureBoxCollection.Image = obj.ShowAircrafts(); } + /// Обработка нажатия "Сохранение" + 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/AircraftCarrier/AircraftCarrier/FormAircraftCollection.resx b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.resx index af32865..769f863 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.resx +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 153, 17 + + + 318, 17 + \ No newline at end of file