лабораторная 6

This commit is contained in:
DavidMakarov 2023-11-17 17:50:03 +04:00
parent 05f1d8701d
commit 108233b501
6 changed files with 261 additions and 6 deletions

View File

@ -1,5 +1,6 @@
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
using System.Text;
namespace AirplaneWithRadar.Generics
{
@ -22,7 +23,6 @@ namespace AirplaneWithRadar.Generics
collection = new SetGeneric<T>(width * height);
}
public static int? operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
@ -89,5 +89,7 @@ namespace AirplaneWithRadar.Generics
j--;
}
}
public IEnumerable<T?> GetAirplanes => collection.GetAirplanes();
}
}

View File

@ -1,5 +1,6 @@
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
using System.Text;
namespace AirplaneWithRadar.Generics
{
@ -9,12 +10,93 @@ namespace AirplaneWithRadar.Generics
public List<string> Keys => airplaneStorages.Keys.ToList();
private readonly int pictWidth;
private readonly int pictHeight;
private static readonly char separatorForKeyValue = '|';
private readonly char separatorRecords = ';';
private static readonly char separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
airplaneStorages = new Dictionary<string, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>>();
pictWidth = pictureWidth;
pictHeight = pictureHeight;
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>> record in airplaneStorages)
{
StringBuilder records = new();
foreach (PaintAirplane? elem in record.Value.GetAirplanes)
{
records.Append($"{elem?.GetDataForSave(separatorForObject)}{separatorRecords}");
}
data.AppendLine($"{record.Key}{separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string strs = data.ToString();
using (StreamWriter sr = new StreamWriter(filename)) {
sr.WriteLine("AirplaneStorage");
sr.WriteLine(strs);
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader reader = new StreamReader(filename))
{
string checker = reader.ReadLine();
if (checker == null)
{
return false;
}
if (!checker.StartsWith("AirplaneStorage"))
{
return false;
}
airplaneStorages.Clear();
string strs;
bool firstinit = true;
while ((strs = reader.ReadLine()) != null)
{
if (strs == null && firstinit)
return false;
if (strs == null)
break;
if (strs == String.Empty)
break;
firstinit = false;
string name = strs.Split('|')[0];
AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane> collection = new(pictWidth, pictHeight);
foreach (string data in strs.Split('|')[1].Split(';'))
{
PaintAirplane? airplane =
data?.CreatePaintAirplane(separatorForObject, pictWidth, pictHeight);
if (airplane != null)
{
if (collection + airplane == -1)
{
return false;
}
}
}
airplaneStorages.Add(name, collection);
}
return true;
}
}
public void AddSet(string name)
{

View File

@ -0,0 +1,45 @@
using AirplaneWithRadar.Entities;
namespace AirplaneWithRadar.PaintObjects
{
public static class ExtentionPaintAirplane
{
public static PaintAirplane? CreatePaintAirplane(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new PaintAirplane(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new PaintAirplaneWithRadar(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 PaintAirplane paintAirplane, char separatorForObject)
{
var airplane = paintAirplane.AirplaneEntity;
if (airplane == null)
{
return string.Empty;
}
var str = $"{airplane.Speed}{separatorForObject}{airplane.Weight}{separatorForObject}{airplane.BodyColor.Name}";
if (airplane is not AirplaneWithRadarEntity airplaneWithRadar)
{
return str;
}
return $"{str}{separatorForObject}{airplaneWithRadar.AdditColor.Name}{separatorForObject}{airplaneWithRadar.RadarOnBoard}" +
$"{separatorForObject}{airplaneWithRadar.AdditFuelPod}";
}
}
}

View File

@ -39,9 +39,19 @@
ButtonDeleteAirplane = new Button();
ButtonAddAirplane = new Button();
pictureBoxCollection = new PictureBox();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem1 = new ToolStripMenuItem();
сохранитьToolStripMenuItem1 = new ToolStripMenuItem();
загрузитьToolStripMenuItem1 = new ToolStripMenuItem();
сохранитьToolStripMenuItem = new ToolStripMenuItem();
загрузитьToolStripMenuItem = new ToolStripMenuItem();
файлToolStripMenuItem = new ToolStripMenuItem();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// groupBox1
@ -53,7 +63,7 @@
groupBox1.Controls.Add(ButtonAddAirplane);
groupBox1.Location = new Point(749, 9);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(223, 440);
groupBox1.Size = new Size(223, 452);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
@ -148,12 +158,72 @@
// pictureBoxCollection
//
pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(984, 461);
pictureBoxCollection.Size = new Size(984, 437);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// menuStrip1
//
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem1 });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(984, 24);
menuStrip1.TabIndex = 2;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem1
//
файлToolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem1, загрузитьToolStripMenuItem1 });
файлToolStripMenuItem1.Name = айлToolStripMenuItem1";
файлToolStripMenuItem1.Size = new Size(48, 20);
файлToolStripMenuItem1.Text = "Файл";
//
// сохранитьToolStripMenuItem1
//
сохранитьToolStripMenuItem1.Name = "сохранитьToolStripMenuItem1";
сохранитьToolStripMenuItem1.Size = new Size(180, 22);
сохранитьToolStripMenuItem1.Text = "Сохранить";
сохранитьToolStripMenuItem1.Click += SaveToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem1
//
загрузитьToolStripMenuItem1.Name = агрузитьToolStripMenuItem1";
загрузитьToolStripMenuItem1.Size = new Size(180, 22);
загрузитьToolStripMenuItem1.Text = "Загрузить";
загрузитьToolStripMenuItem1.Click += LoadToolStripMenuItem_Click;
//
// сохранитьToolStripMenuItem
//
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
сохранитьToolStripMenuItem.Size = new Size(133, 22);
сохранитьToolStripMenuItem.Text = "Сохранить";
//
// загрузитьToolStripMenuItem
//
загрузитьToolStripMenuItem.Name = агрузитьToolStripMenuItem";
загрузитьToolStripMenuItem.Size = new Size(133, 22);
загрузитьToolStripMenuItem.Text = "Загрузить";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.Checked = true;
файлToolStripMenuItem.CheckState = CheckState.Checked;
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Text = "Файл";
//
// FormAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -161,6 +231,7 @@
ClientSize = new Size(984, 461);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
Controls.Add(menuStrip1);
Name = "FormAirplaneCollection";
Text = "FormAirplaneCollection";
groupBox1.ResumeLayout(false);
@ -168,7 +239,10 @@
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -184,5 +258,14 @@
private Button buttonDelObject;
private Button buttonAddObject;
private TextBox textBoxStorageName;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem1;
private ToolStripMenuItem сохранитьToolStripMenuItem;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem1;
private ToolStripMenuItem загрузитьToolStripMenuItem1;
}
}

View File

@ -51,7 +51,7 @@ namespace AirplaneWithRadar
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
{
storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
@ -131,5 +131,39 @@ namespace AirplaneWithRadar
}
pictureBoxCollection.Image = obj.ShowAirplanes();
}
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);
}
else
{
MessageBox.Show("Не загузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadObjects();
}
}
}
}

View File

@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
@ -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="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>172, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>305, 17</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>434, 17</value>
</metadata>
</root>