diff --git a/ContainerShip/ContainerShip/DrawingObjectShip.cs b/ContainerShip/ContainerShip/DrawingObjectShip.cs index 963cbb9..f322be6 100644 --- a/ContainerShip/ContainerShip/DrawingObjectShip.cs +++ b/ContainerShip/ContainerShip/DrawingObjectShip.cs @@ -31,7 +31,6 @@ namespace ContainerShip { _ship.SetPosition(x, y, width, height); } - void IDrawingObject.DrawingObject(Graphics g) { if (_ship == null) return; @@ -44,5 +43,7 @@ namespace ContainerShip _ship.DrawTransport(g); } } + public string GetInfo() => _ship?.GetDataForSave(); + public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip()); } } diff --git a/ContainerShip/ContainerShip/ExtentionShip.cs b/ContainerShip/ContainerShip/ExtentionShip.cs new file mode 100644 index 0000000..6ab5428 --- /dev/null +++ b/ContainerShip/ContainerShip/ExtentionShip.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal static class ExtentionShip + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingShip CreateDrawingShip(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingShip(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawingContainerShip(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 DrawingShip drawingShip) + { + var ship = drawingShip.Ship; + var str = $"{ship.Speed}{_separatorForObject}{ship.Weight}{_separatorForObject}{ship.BodyColor.Name}"; + if (ship is not EntityContainerShip containerShip) + { + return str; + } + return $"{str}{_separatorForObject}{containerShip.DopColor.Name}{_separatorForObject}{containerShip.Containers}{_separatorForObject}{containerShip.Crane}"; + } + } +} + diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index bce3b84..70195a7 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -45,9 +45,16 @@ this.ButtonRemoveShip = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); 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.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,9 @@ this.groupBox1.Controls.Add(this.ButtonRemoveShip); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(553, 0); + this.groupBox1.Location = new System.Drawing.Point(553, 33); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(300, 687); + this.groupBox1.Size = new System.Drawing.Size(300, 654); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -147,7 +154,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(131, 569); + this.buttonUp.Location = new System.Drawing.Point(131, 536); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.TabIndex = 9; @@ -159,7 +166,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(187, 625); + this.buttonRight.Location = new System.Drawing.Point(187, 592); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.TabIndex = 8; @@ -171,7 +178,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(131, 625); + this.buttonDown.Location = new System.Drawing.Point(131, 592); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.TabIndex = 7; @@ -183,7 +190,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(75, 625); + this.buttonLeft.Location = new System.Drawing.Point(75, 592); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.TabIndex = 6; @@ -231,12 +238,55 @@ // 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, 33); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(553, 687); + this.pictureBox.Size = new System.Drawing.Size(553, 654); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.ImageScalingSize = new System.Drawing.Size(24, 24); + 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(853, 33); + 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(69, 29); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file|*.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file|*.txt"; + // // FormMapWithSetShips // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); @@ -244,6 +294,8 @@ this.ClientSize = new System.Drawing.Size(853, 687); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetShips"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); @@ -251,7 +303,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 +329,11 @@ private Button ButtonAddMap; private GroupBox groupBoxMaps; private Button ButtonDeleteMap; + 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/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs index 68c660c..97ba096 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -207,5 +207,36 @@ namespace ContainerShip ReloadMaps(); } } + + 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/ContainerShip/ContainerShip/FormMapWithSetShips.resx b/ContainerShip/ContainerShip/FormMapWithSetShips.resx index f298a7b..0034e6c 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.resx +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 163, 17 + + + 357, 17 + \ No newline at end of file diff --git a/ContainerShip/ContainerShip/IDrawingObject.cs b/ContainerShip/ContainerShip/IDrawingObject.cs index 5c50dda..7a4f72c 100644 --- a/ContainerShip/ContainerShip/IDrawingObject.cs +++ b/ContainerShip/ContainerShip/IDrawingObject.cs @@ -36,5 +36,6 @@ namespace ContainerShip /// /// (float Left, float Top, float Right, float Bottom) GetCurrentPosition(); + string GetInfo(); } } diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index 10db820..d87b334 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -74,6 +74,26 @@ namespace ContainerShip } return new(_pictureWidth, _pictureHeight); } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var ship in _setShips.GetShips()) + { + data += $"{ship.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setShips.Insert(DrawingObjectShip.Create(rec) as T); + } + } /// /// Перемещение объекта по крате /// @@ -171,5 +191,6 @@ namespace ContainerShip { return _setShips.Insert(ship); } + } } diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs index aa31364..6eadab9 100644 --- a/ContainerShip/ContainerShip/MapsCollection.cs +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -1,8 +1,13 @@ using System; using System.Collections.Generic; +using System.Drawing.Drawing2D; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.VisualBasic; +using System.IO; +using System.Windows.Forms; namespace ContainerShip { @@ -11,7 +16,7 @@ namespace ContainerShip /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -25,13 +30,21 @@ namespace ContainerShip /// 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; } @@ -46,7 +59,7 @@ namespace ContainerShip if (_mapStorages.ContainsKey(name)) return; //уникальное имя else { - _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); } } @@ -67,7 +80,7 @@ namespace ContainerShip /// /// /// - public MapWithSetShipsGeneric this[string ind] + public MapWithSetShipsGeneric this[string ind] { get { @@ -76,5 +89,77 @@ namespace ContainerShip 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)) + using (StreamWriter sw = new StreamWriter(fs)) + { + sw.Write($"MapsCollection{Environment.NewLine}", sw); + foreach (var storage in _mapStorages) + { + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", sw); + } + } + return true; + } + + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(fs)) + { + string line; + line = sr.ReadLine(); + line.Trim(); + if (line != "MapsCollection") return false; + _mapStorages.Clear(); + while ((line = sr.ReadLine()) != null) + { + line = line.Trim(); + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "ModifyMap": + map = new ModifyMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } + } } }