PIbd-23_Nasyrov_A_G_Lab6 #8

Closed
gaillard wants to merge 3 commits from lab6 into lab5
6 changed files with 253 additions and 20 deletions

View File

@ -21,6 +21,7 @@ namespace ProjectAirplaneWithRadar.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public IEnumerable<T?> GetAirplanes => _collection.GetAirplanes();
public static bool operator +(AirplanesGenericCollection<T, U> collect, T? obj)
{
if (obj == null || collect == null)

View File

@ -15,30 +15,114 @@ namespace ProjectAirplaneWithRadar.Generics
public List<string> Keys => _airplanesStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_airplanesStorages = new Dictionary<string,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
public bool SaveData(string filename)
{
_airplanesStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_airplanesStorages.ContainsKey(name))
return;
_airplanesStorages.Remove(name);
}
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind]
{
get
if (File.Exists(filename))
{
if (_airplanesStorages.ContainsKey(ind))
return _airplanesStorages[ind];
return null;
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplanesStorages)
{
StringBuilder records = new();
foreach (DrawningAirplane? elem in record.Value.GetAirplanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string toWrite = $"AirplanesStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("AirplanesStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_airplanesStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningAirplane? airplane =
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
if (airplane != null)
{
if (!(collection + airplane))
{
return false;
}
}
}
_airplanesStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
public void AddSet(string name)
{
_airplanesStorages.Add(name, new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_airplanesStorages.ContainsKey(name))
return;
_airplanesStorages.Remove(name);
}
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind]
{
get
{
if (_airplanesStorages.ContainsKey(ind))
return _airplanesStorages[ind];
return null;
}
}
}
}

View File

@ -0,0 +1,50 @@
using ProjectAirplaneWithRadar.Entities;
using ProjectAirplaneWithRadar.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirplaneWithRadar
{
public static class ExtentionDrawningAirplane
{
public static DrawningAirplane? CreateDrawningAirplane(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningAirplane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningAirplaneWithRadar(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 DrawningAirplane drawningAirplane,char separatorForObject)
{
var airplane = drawningAirplane.EntityAirplane;
if (airplane == null)
{
return string.Empty;
}
var str = $"{airplane.Speed}{separatorForObject}{airplane.Weight}{separatorForObject}{airplane.BodyColor.Name}";
if (airplane is not EntityAirplaneWithRadar airplaneWithRadar)
{
return str;
}
return
$"{str}{separatorForObject}{airplaneWithRadar.AdditionalColor.Name}" +
$"{separatorForObject}{airplaneWithRadar.Radar}" +
$"{separatorForObject}{airplaneWithRadar.DopBak}";
}
}
}

View File

@ -39,14 +39,21 @@
buttonDeleteAirplane = new Button();
buttonAddAirplane = new Button();
maskedTextBoxNumber = new MaskedTextBox();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).BeginInit();
groupBoxAirplaneWithRadar.SuspendLayout();
groupBoxCollection.SuspendLayout();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// pictureBoxAirplanesCollection
//
pictureBoxAirplanesCollection.Location = new Point(-9, 0);
pictureBoxAirplanesCollection.Location = new Point(0, 38);
pictureBoxAirplanesCollection.Name = "pictureBoxAirplanesCollection";
pictureBoxAirplanesCollection.Size = new Size(650, 454);
pictureBoxAirplanesCollection.SizeMode = PictureBoxSizeMode.AutoSize;
@ -62,9 +69,9 @@
groupBoxAirplaneWithRadar.Controls.Add(buttonAddAirplane);
groupBoxAirplaneWithRadar.Controls.Add(maskedTextBoxNumber);
groupBoxAirplaneWithRadar.Dock = DockStyle.Right;
groupBoxAirplaneWithRadar.Location = new Point(640, 0);
groupBoxAirplaneWithRadar.Location = new Point(650, 28);
groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar";
groupBoxAirplaneWithRadar.Size = new Size(250, 453);
groupBoxAirplaneWithRadar.Size = new Size(251, 464);
groupBoxAirplaneWithRadar.TabIndex = 1;
groupBoxAirplaneWithRadar.TabStop = false;
groupBoxAirplaneWithRadar.Text = "Инструменты";
@ -156,13 +163,49 @@
maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.TabIndex = 0;
//
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(901, 28);
menuStrip1.TabIndex = 2;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
//
// FormAirplanesCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(890, 453);
ClientSize = new Size(901, 492);
Controls.Add(groupBoxAirplaneWithRadar);
Controls.Add(pictureBoxAirplanesCollection);
Controls.Add(menuStrip1);
Name = "FormAirplanesCollection";
Text = "FormAirplaneWithRadar";
((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).EndInit();
@ -170,6 +213,8 @@
groupBoxAirplaneWithRadar.PerformLayout();
groupBoxCollection.ResumeLayout(false);
groupBoxCollection.PerformLayout();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@ -187,5 +232,11 @@
private ListBox listBoxStorages;
private Button buttonAddObject;
private TextBox textBoxStorageName;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -2,6 +2,7 @@ using ProjectAirplaneWithRadar.DrawningObjects;
using ProjectAirplaneWithRadar.MovementStrategy;
using ProjectAirplaneWithRadar.Generics;
using System.Diagnostics.Metrics;
using System.Windows.Forms;
namespace ProjectAirplaneWithRadar
{
@ -47,7 +48,8 @@ namespace ProjectAirplaneWithRadar
}
FormAirplaneConfig form = new(pictureBoxAirplanesCollection.Width, pictureBoxAirplanesCollection.Height);
form.Show();
Action < DrawningAirplane >? airplaneDelegate = new((m) => {
Action<DrawningAirplane>? airplaneDelegate = new((m) =>
{
bool q = (obj + m);
if (q)
{
@ -138,5 +140,41 @@ namespace ProjectAirplaneWithRadar
{
}
private void SaveToolStripMenuItem_Click_1(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);
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
}
else
{
MessageBox.Show("Íå çàãðóçèëîñü", "Ðåçóëüòàò",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -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="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>153, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>323, 17</value>
</metadata>
</root>