From d1e6e62c059ed57fdcc9803c76e1110a0269b121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=9F=D1=83=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD?= Date: Fri, 11 Nov 2022 00:45:12 +0400 Subject: [PATCH] LabWork6 Complete --- .../DrawningObjectAirplane.cs | 4 + .../AirplaneWithRadar/ExtentionAirplane.cs | 53 +++++++++++ .../FormMapWithSetAirplanes.Designer.cs | 75 +++++++++++++-- .../FormMapWithSetAirplanes.cs | 30 ++++++ .../FormMapWithSetAirplanes.resx | 9 ++ .../AirplaneWithRadar/IDrawningObject.cs | 5 + .../MapWithSetAirplanesGeneric.cs | 27 ++++++ .../AirplaneWithRadar/MapsCollection.cs | 94 ++++++++++++++++++- 8 files changed, 284 insertions(+), 13 deletions(-) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/ExtentionAirplane.cs diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs index b5bd95a..1675811 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs @@ -31,5 +31,9 @@ namespace AirplaneWithRadar { _airplane.DrawTransport(g); } + + public string GetInfo() => _airplane?.GetDataForSave(); + public static IDrawningObject Create(string data) => new DrawningObjectAirplane(data.CreateDrawningAirplane()); + } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/ExtentionAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/ExtentionAirplane.cs new file mode 100644 index 0000000..6c6b9aa --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/ExtentionAirplane.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + internal static class ExtentionAirplane + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningAirplane CreateDrawningAirplane(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirplane(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningAirplaneWithRadar(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; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave(this DrawningAirplane drawningAirplane) + { + var airplane = drawningAirplane.Airplane; + var str = $"{airplane.Speed}{_separatorForObject}{airplane.Weight}{_separatorForObject}{airplane.BodyColor.Name}"; + if (airplane is not EntityAirplaneWithRadar airplaneWithRadar) + { + return str; + } + return $"{str}{_separatorForObject}{airplaneWithRadar.DopColor.Name}{_separatorForObject}{airplaneWithRadar.Radar}{_separatorForObject}{airplaneWithRadar.FuelTanks}"; + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs index 7622575..33e0540 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs @@ -45,9 +45,16 @@ this.buttonRemoveAirplane = new System.Windows.Forms.Button(); this.buttonAddAirplane = 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.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -63,9 +70,9 @@ this.groupBoxTools.Controls.Add(this.buttonRemoveAirplane); this.groupBoxTools.Controls.Add(this.buttonAddAirplane); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(808, 0); + this.groupBoxTools.Location = new System.Drawing.Point(808, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 604); + this.groupBoxTools.Size = new System.Drawing.Size(200, 580); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -145,7 +152,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(119, 558); + this.buttonRight.Location = new System.Drawing.Point(119, 534); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 8; @@ -157,7 +164,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(47, 558); + this.buttonLeft.Location = new System.Drawing.Point(47, 534); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 7; @@ -169,7 +176,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(83, 558); + this.buttonDown.Location = new System.Drawing.Point(83, 534); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 6; @@ -181,7 +188,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(83, 522); + this.buttonUp.Location = new System.Drawing.Point(83, 498); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 5; @@ -231,12 +238,53 @@ // 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(808, 604); + this.pictureBox.Size = new System.Drawing.Size(808, 580); 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(1008, 24); + this.menuStrip.TabIndex = 2; + this.menuStrip.Text = "menuStrip1"; + // + // файл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 = "Сохранение"; + 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); + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetAirplanes // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -244,6 +292,8 @@ this.ClientSize = new System.Drawing.Size(1008, 604); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetAirplanes"; this.Text = "FormMapWithSetAirplanes"; this.groupBoxTools.ResumeLayout(false); @@ -251,7 +301,10 @@ this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -274,5 +327,11 @@ private ListBox listBoxMaps; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs index 1aa6a42..220f7a3 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs @@ -211,5 +211,35 @@ } 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); + ReloadMaps(); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.resx b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.resx index f298a7b..ab8db54 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.resx +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 261, 17 + \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs index 98e8a7f..1a63bcd 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs @@ -36,5 +36,10 @@ namespace AirplaneWithRadar /// (float Left, float Top, float Right, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs index 78c48d8..62eee4e 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs @@ -110,6 +110,33 @@ } return new(_pictureWidth, _pictureHeight); } + + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var car in _setAirplanes.GetAirplanes()) + { + data += $"{car.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setAirplanes.Insert(DrawningObjectAirplane.Create(rec) as T); + } + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs index fe7f308..3ff0150 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapsCollection.cs @@ -1,11 +1,13 @@ -namespace AirplaneWithRadar +using System.Text; + +namespace AirplaneWithRadar { internal class MapsCollection { /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -19,13 +21,21 @@ /// 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; } @@ -36,7 +46,11 @@ /// Карта public void AddMap(string name, AbstractMap map) { - _mapStorages[name] = new(_pictureWidth, _pictureHeight, map); + _mapStorages.TryGetValue(name, out var buffer); + if (buffer == null) + { + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); + } } /// /// Удаление карты @@ -51,7 +65,7 @@ /// /// /// - public MapWithSetAirplanesGeneric this[string ind] + public MapWithSetAirplanesGeneric this[string ind] { get { @@ -60,5 +74,75 @@ } } + /// + /// Метод записи информации в файл + /// + /// Строка, которую следует записать + /// Поток для записи + 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 (StreamWriter fs = new(filename)) + { + fs.Write($"MapsCollection{Environment.NewLine}"); + foreach (var storage in _mapStorages) + { + fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); + } + } + return true; + } + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader fs = new(filename)) + { + if (!fs.ReadLine().Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + while (!fs.EndOfStream) + { + var elem = fs.ReadLine().Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetAirplanesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + } + return true; + } } }