diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionShip.cs
new file mode 100644
index 0000000..fe2de09
--- /dev/null
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionShip.cs
@@ -0,0 +1,53 @@
+using ProjectWarmlyShip.DrawningObjects;
+using ProjectWarmlyShip.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectWarmlyShip
+{
+ ///
+ /// Расширение для класса EntityShip
+ ///
+ public static class ExtentionShip
+ {
+ public static DrawningShip? CreateDrawningShip(this string info, char separatorForObject, int width, int height)
+ {
+ string[] strs = info.Split(separatorForObject);
+ if (strs.Length == 3)
+ {
+ return new DrawningShip(Convert.ToInt32(strs[0]),
+ Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
+ }
+ if (strs.Length == 6)
+ {
+ return new DrawningWarmlyShip(
+ 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 DrawningShip ship, char separatorForObject)
+ {
+ var Ship = ship.EntityShip;
+ if (Ship == null)
+ {
+ return string.Empty;
+ }
+ var str = $"{Ship.Speed}{separatorForObject}{Ship.Weight}{separatorForObject}{Ship.BodyColor.Name}";
+ if (Ship is not EntityWarmlyShip warmlyShip)
+ {
+ return str;
+ }
+ return $"{str}{separatorForObject}{warmlyShip.AdditionalColor.Name}{separatorForObject}{warmlyShip.ShipPipes}{separatorForObject}{warmlyShip.ShipFuel}";
+ }
+ }
+}
+
diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs
index 2d12d71..79ba4b0 100644
--- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs
@@ -39,17 +39,24 @@
listBoxStorages = new ListBox();
buttonAddObject = new Button();
textBoxStorageName = new TextBox();
+ файлToolStripMenuItem = new ToolStripMenuItem();
+ сохранениеToolStripMenuItem = new ToolStripMenuItem();
+ загрузкаToolStripMenuItem = new ToolStripMenuItem();
+ menuStrip = new MenuStrip();
+ saveFileDialog = new SaveFileDialog();
+ openFileDialog = new OpenFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
+ menuStrip.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
- pictureBoxCollection.Location = new Point(0, 0);
+ pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection";
- pictureBoxCollection.Size = new Size(800, 450);
+ pictureBoxCollection.Size = new Size(800, 426);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
@@ -154,6 +161,40 @@
textBoxStorageName.Size = new Size(136, 23);
textBoxStorageName.TabIndex = 0;
//
+ // файлToolStripMenuItem
+ //
+ файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранениеToolStripMenuItem, загрузкаToolStripMenuItem });
+ файлToolStripMenuItem.Name = "файлToolStripMenuItem";
+ файлToolStripMenuItem.Size = new Size(48, 20);
+ файлToolStripMenuItem.Text = "файл";
+ //
+ // сохранениеToolStripMenuItem
+ //
+ сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem";
+ сохранениеToolStripMenuItem.Size = new Size(180, 22);
+ сохранениеToolStripMenuItem.Text = "Сохранение";
+ сохранениеToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
+ //
+ // загрузкаToolStripMenuItem
+ //
+ загрузкаToolStripMenuItem.Name = "загрузкаToolStripMenuItem";
+ загрузкаToolStripMenuItem.Size = new Size(180, 22);
+ загрузкаToolStripMenuItem.Text = "Загрузка";
+ загрузкаToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
+ //
+ // menuStrip
+ //
+ menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
+ menuStrip.Location = new Point(0, 0);
+ menuStrip.Name = "menuStrip";
+ menuStrip.Size = new Size(800, 24);
+ menuStrip.TabIndex = 5;
+ menuStrip.Text = "menuStrip1";
+ //
+ // openFileDialog
+ //
+ openFileDialog.FileName = "openFileDialog1";
+ //
// FormShipCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@@ -162,6 +203,8 @@
Controls.Add(groupBox2);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
+ Controls.Add(menuStrip);
+ MainMenuStrip = menuStrip;
Name = "FormShipCollection";
Text = "FormShipCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@@ -169,6 +212,8 @@
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
+ menuStrip.ResumeLayout(false);
+ menuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@@ -186,5 +231,11 @@
private ListBox listBoxStorages;
private Button buttonAddObject;
private TextBox textBoxStorageName;
+ private ToolStripMenuItem файлToolStripMenuItem;
+ private ToolStripMenuItem сохранениеToolStripMenuItem;
+ private ToolStripMenuItem загрузкаToolStripMenuItem;
+ private MenuStrip menuStrip;
+ private SaveFileDialog saveFileDialog;
+ private OpenFileDialog openFileDialog;
}
}
\ No newline at end of file
diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs
index 7c4657e..c79deaa 100644
--- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs
@@ -182,5 +182,47 @@ namespace ProjectWarmlyShip
}
pictureBoxCollection.Image = obj.ShowShips();
}
+ ///
+ /// Обработка нажатия "Сохранение"
+ ///
+ ///
+ ///
+ 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 продумать логику
+ if (openFileDialog.ShowDialog() == DialogResult.OK)
+ {
+ if (_storage.LoadData(openFileDialog.FileName))
+ {
+ ReloadObjects();
+ MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
+ }
+ else
+ {
+ MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+ }
}
}
diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx
index af32865..8b1dfa1 100644
--- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx
@@ -117,4 +117,13 @@
System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+ 17, 17
+
+
+ 126, 17
+
+
+ 261, 17
+
\ No newline at end of file
diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs
index 7a9f6ff..e281639 100644
--- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs
@@ -42,6 +42,7 @@ namespace ProjectWarmlyShip.Generics
///
///
///
+ public IEnumerable GetShips => _collection.GetShips();
public ShipsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs
index b116649..df35baf 100644
--- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs
+++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs
@@ -31,6 +31,18 @@ namespace ProjectWarmlyShip
///
private readonly int _pictureHeight;
///
+ /// Разделитель для записи ключа и значения элемента словаря
+ ///
+ private static readonly char _separatorForKeyValue = '|';
+ ///
+ /// Разделитель для записей коллекции данных в файл
+ ///
+ private readonly char _separatorRecords = ';';
+ ///
+ /// Разделитель для записи информации по объекту в файл
+ ///
+ private static readonly char _separatorForObject = ':';
+ ///
/// Конструктор
///
///
@@ -76,5 +88,86 @@ namespace ProjectWarmlyShip
return null;
}
}
+ ///
+ /// Сохранение информации по автомобилям в хранилище в файл
+ ///
+ /// Путь и имя файла
+ /// true - сохранение прошло успешно, false - ошибка при сохранении данных
+ public bool SaveData(string filename)
+ {
+ if (File.Exists(filename))
+ {
+ File.Delete(filename);
+ }
+ StringBuilder data = new();
+ foreach (KeyValuePair> record in _shipStorages)
+ {
+ StringBuilder records = new();
+ foreach (DrawningShip? elem in record.Value.GetShips)
+ {
+ records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
+ }
+ data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
+ }
+ if (data.Length == 0)
+ {
+ return false;
+ }
+ using (StreamWriter writer = new StreamWriter(filename, false))
+ {
+ writer.WriteLine("ShipStorage");
+ writer.Write(data.ToString());
+ }
+
+ return true;
+ }
+ ///
+ /// Загрузка информации по автомобилям в хранилище из файла
+ ///
+ /// Путь и имя файла
+ /// true - загрузка прошла успешно, false - ошибка при загрузке данных
+ public bool LoadData(string filename)
+ {
+ if (!File.Exists(filename))
+ {
+ return false;
+ }
+
+ using (StreamReader reader = new StreamReader(filename))
+ {
+ string cheker = reader.ReadLine();
+ if (cheker == null)
+ {
+ return false;
+ }
+ if (!cheker.StartsWith("ShipStorage"))
+ {
+ return false;
+ }
+ _shipStorages.Clear();
+ string strs;
+ bool firstinit = true;
+ while ((strs = reader.ReadLine()) != null)
+ {
+ string name = strs.Split(_separatorForKeyValue)[0];
+ ShipsGenericCollection collection = new(_pictureWidth, _pictureHeight);
+ foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords))
+ {
+ DrawningShip? ship =
+ data?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
+ if (ship != null)
+ {
+ if (!(collection + ship))
+ {
+ return false;
+ }
+ }
+ }
+ _shipStorages.Add(name, collection);
+ }
+ return true;
+ }
+ }
}
}