ISEbd-21. Gruzdev A.P. Lab work 06. #7
53
ProjectWarmlyShip/ProjectWarmlyShip/ExtentionShip.cs
Normal file
53
ProjectWarmlyShip/ProjectWarmlyShip/ExtentionShip.cs
Normal file
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Расширение для класса EntityShip
|
||||
/// </summary>
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -182,5 +182,47 @@ namespace ProjectWarmlyShip
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowShips();
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Сохранение"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Загрузка"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,4 +117,13 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>126, 17</value>
|
||||
</metadata>
|
||||
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>261, 17</value>
|
||||
</metadata>
|
||||
</root>
|
@ -42,6 +42,7 @@ namespace ProjectWarmlyShip.Generics
|
||||
/// </summary>
|
||||
/// <param name="picWidth"></param>
|
||||
/// <param name="picHeight"></param>
|
||||
public IEnumerable<T?> GetShips => _collection.GetShips();
|
||||
public ShipsGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
|
@ -31,6 +31,18 @@ namespace ProjectWarmlyShip
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char _separatorRecords = ';';
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
@ -76,5 +88,86 @@ namespace ProjectWarmlyShip
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,
|
||||
ShipsGenericCollection<DrawningShip, DrawningObjectShip>> 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка информации по автомобилям в хранилище из файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
||||
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<DrawningShip, DrawningObjectShip> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user