Завершенная лаба 6
This commit is contained in:
parent
a02e68e555
commit
c820f6ad4c
@ -12,6 +12,8 @@ namespace AircraftCarrier.Generics
|
||||
where T : DrawningAircraft
|
||||
where U : IMoveableObject
|
||||
{
|
||||
/// Получение объектов коллекции
|
||||
public IEnumerable<T?> GetCars => _collection.GetAircrafts();
|
||||
/// Ширина окна прорисовки
|
||||
private readonly int _pictureWidth;
|
||||
/// Высота окна прорисовки
|
||||
|
@ -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<string,
|
||||
AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>> 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<DrawningAircraft, DrawningObjectAircraft> 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
50
AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs
Normal file
50
AircraftCarrier/AircraftCarrier/ExtentionDrawningAircraft.cs
Normal file
@ -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}";
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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="menuStrip1.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>153, 17</value>
|
||||
</metadata>
|
||||
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>318, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
Reference in New Issue
Block a user