This commit is contained in:
antoc0der 2023-11-23 11:43:43 +03:00
parent 6d0a86275c
commit 1dedcb6bc1
5 changed files with 196 additions and 19 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,12 +15,96 @@ 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 bool SaveData(string filename)
{
if (File.Exists(filename))
{
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));

View File

@ -0,0 +1,53 @@
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[6]), width, height);
}
return null;
}
public static string GetDataForSave(this DrawningAirplane drawningAirplane,
char separatorForObject)
{
var car = drawningAirplane.EntityAirplane;
if (car == null)
{
return string.Empty;
}
var str =
$"{car.Speed}{separatorForObject}{car.Weight}{separatorForObject}{car.BodyColor.Name}";
if (car is not EntityAirplaneWithRadar airplaneWithRadar)
{
return str;
}
return
$"{str}{separatorForObject}{airplaneWithRadar.AdditionalColor.Name}" +
$"{separatorForObject}{airplaneWithRadar.Radar}" +
$"{separatorForObject}{airplaneWithRadar.DopBak}";
}
}
}

View File

@ -39,9 +39,13 @@
buttonDeleteAirplane = new Button();
buttonAddAirplane = new Button();
maskedTextBoxNumber = new MaskedTextBox();
groupBoxRecord = new GroupBox();
LoadToolStripMenuItem = new Button();
SaveToolStripMenuItem = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).BeginInit();
groupBoxAirplaneWithRadar.SuspendLayout();
groupBoxCollection.SuspendLayout();
groupBoxRecord.SuspendLayout();
SuspendLayout();
//
// pictureBoxAirplanesCollection
@ -56,15 +60,16 @@
//
// groupBoxAirplaneWithRadar
//
groupBoxAirplaneWithRadar.Controls.Add(groupBoxRecord);
groupBoxAirplaneWithRadar.Controls.Add(groupBoxCollection);
groupBoxAirplaneWithRadar.Controls.Add(buttonUpdateCollection);
groupBoxAirplaneWithRadar.Controls.Add(buttonDeleteAirplane);
groupBoxAirplaneWithRadar.Controls.Add(buttonAddAirplane);
groupBoxAirplaneWithRadar.Controls.Add(maskedTextBoxNumber);
groupBoxAirplaneWithRadar.Dock = DockStyle.Right;
groupBoxAirplaneWithRadar.Location = new Point(640, 0);
groupBoxAirplaneWithRadar.Location = new Point(643, 0);
groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar";
groupBoxAirplaneWithRadar.Size = new Size(250, 453);
groupBoxAirplaneWithRadar.Size = new Size(460, 453);
groupBoxAirplaneWithRadar.TabIndex = 1;
groupBoxAirplaneWithRadar.TabStop = false;
groupBoxAirplaneWithRadar.Text = "Инструменты";
@ -156,11 +161,40 @@
maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.TabIndex = 0;
//
// groupBoxRecord
//
groupBoxRecord.Controls.Add(SaveToolStripMenuItem);
groupBoxRecord.Controls.Add(LoadToolStripMenuItem);
groupBoxRecord.Location = new Point(250, 26);
groupBoxRecord.Name = "groupBoxRecord";
groupBoxRecord.Size = new Size(204, 103);
groupBoxRecord.TabIndex = 5;
groupBoxRecord.TabStop = false;
groupBoxRecord.Text = "Запись";
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Location = new Point(6, 61);
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(192, 29);
LoadToolStripMenuItem.TabIndex = 0;
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.UseVisualStyleBackColor = true;
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Location = new Point(6, 26);
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(192, 29);
SaveToolStripMenuItem.TabIndex = 1;
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.UseVisualStyleBackColor = true;
//
// FormAirplanesCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(890, 453);
ClientSize = new Size(1103, 453);
Controls.Add(groupBoxAirplaneWithRadar);
Controls.Add(pictureBoxAirplanesCollection);
Name = "FormAirplanesCollection";
@ -170,6 +204,7 @@
groupBoxAirplaneWithRadar.PerformLayout();
groupBoxCollection.ResumeLayout(false);
groupBoxCollection.PerformLayout();
groupBoxRecord.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
@ -187,5 +222,8 @@
private ListBox listBoxStorages;
private Button buttonAddObject;
private TextBox textBoxStorageName;
private GroupBox groupBoxRecord;
private Button SaveToolStripMenuItem;
private Button LoadToolStripMenuItem;
}
}

View File

@ -47,7 +47,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)
{