ПИбд-21 Ярускин Салих 6 лаб простая #6
@ -14,6 +14,11 @@ namespace AirBomber.Generics
|
||||
where T : DrawningBomber
|
||||
where U : IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение объектов коллекции
|
||||
/// </summary>
|
||||
public IEnumerable<T?> GetPlane => _collection.GetPlane();
|
||||
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
private readonly int _placeSizeWidth = 155;
|
||||
@ -38,15 +43,14 @@ namespace AirBomber.Generics
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
|
||||
public static bool operator -(BomberGenericCollection<T, U> collect, int
|
||||
pos)
|
||||
public static T? operator -(BomberGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj == null)
|
||||
if (obj != null)
|
||||
{
|
||||
return false;
|
||||
collect._collection.Remove(pos);
|
||||
}
|
||||
return collect._collection.Remove(pos);
|
||||
return obj;
|
||||
}
|
||||
|
||||
public U? GetU(int pos)
|
||||
|
@ -25,6 +25,18 @@ namespace AirBomber.Generics
|
||||
/// </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>
|
||||
@ -65,5 +77,91 @@ namespace AirBomber.Generics
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string, BomberGenericCollection<DrawningBomber, DrawningObjectBomber>> record in _bomberStorage)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningBomber? elem in record.Value.GetPlane)
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using (StreamWriter writer = new StreamWriter(filename))
|
||||
{
|
||||
writer.Write($"BomberStorage{Environment.NewLine}{data}");
|
||||
}
|
||||
|
||||
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("BomberStorage"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_bomberStorage.Clear();
|
||||
string strs;
|
||||
bool firstinit = true;
|
||||
while ((strs = reader.ReadLine()) != null)
|
||||
{
|
||||
if (strs == null && firstinit)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (strs == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
firstinit = false;
|
||||
string name = strs.Split(_separatorForKeyValue)[0];
|
||||
BomberGenericCollection<DrawningBomber, DrawningObjectBomber> collection = new(_pictureWidth, _pictureHeight);
|
||||
foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords))
|
||||
{
|
||||
DrawningBomber? air =
|
||||
data?.CreateDrawningBomber(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (air != null)
|
||||
{
|
||||
int? result = collection + air;
|
||||
if (result == null || result.Value == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_bomberStorage.Add(name, collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
62
AirBomber/ExtentionDrawningBomber.cs
Normal file
62
AirBomber/ExtentionDrawningBomber.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using AirBomber.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber.DrawningObjects
|
||||
{
|
||||
public static class ExtentionDrawningBomber
|
||||
{
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info">Строка с данными для создания объекта</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <param name="width">Ширина</param>
|
||||
/// <param name="height">Высота</param>
|
||||
/// <returns>Объект</returns>
|
||||
public static DrawningBomber? CreateDrawningBomber(this string info, char separatorForObject, int width, int height)
|
||||
{
|
||||
string[] strs = info.Split(separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawningBomber(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
|
||||
}
|
||||
else if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningAirBomber(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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningBomber">Сохраняемый объект</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <returns>Строка с данными по объекту</returns>
|
||||
public static string GetDataForSave(this DrawningBomber drawningBomber,
|
||||
char separatorForAir)
|
||||
{
|
||||
var air = drawningBomber.EntityBomber;
|
||||
if (air == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var str =
|
||||
$"{air.Speed}{separatorForAir}{air.Weight}{separatorForAir}{air.BodyColor.Name}";
|
||||
if (air is not EntityAirBomber airBomber)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{separatorForAir}{airBomber.DopColor.Name}{separatorForAir}{airBomber.Toplivo}{separatorForAir}{airBomber.Rocket}";
|
||||
}
|
||||
}
|
||||
}
|
67
AirBomber/FormBomberCollection.Designer.cs
generated
67
AirBomber/FormBomberCollection.Designer.cs
generated
@ -39,9 +39,16 @@
|
||||
ButtonAddBomber = new Button();
|
||||
MessageBoxBomber = new TextBox();
|
||||
PicBoxBomberCollection = new PictureBox();
|
||||
menuStrip = new MenuStrip();
|
||||
fileToolStripMenuItem = new ToolStripMenuItem();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
LoadToolStripMenuItem = new ToolStripMenuItem();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
saveFileDialog = new OpenFileDialog();
|
||||
Tools.SuspendLayout();
|
||||
Kit.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)PicBoxBomberCollection).BeginInit();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// Tools
|
||||
@ -110,7 +117,7 @@
|
||||
//
|
||||
// ButtonRefreshCollection
|
||||
//
|
||||
ButtonRefreshCollection.Location = new Point(28, 481);
|
||||
ButtonRefreshCollection.Location = new Point(28, 448);
|
||||
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
|
||||
ButtonRefreshCollection.Size = new Size(193, 37);
|
||||
ButtonRefreshCollection.TabIndex = 3;
|
||||
@ -120,7 +127,7 @@
|
||||
//
|
||||
// ButtonRemoveBomber
|
||||
//
|
||||
ButtonRemoveBomber.Location = new Point(28, 416);
|
||||
ButtonRemoveBomber.Location = new Point(28, 401);
|
||||
ButtonRemoveBomber.Name = "ButtonRemoveBomber";
|
||||
ButtonRemoveBomber.Size = new Size(193, 41);
|
||||
ButtonRemoveBomber.TabIndex = 2;
|
||||
@ -147,19 +154,62 @@
|
||||
//
|
||||
// PicBoxBomberCollection
|
||||
//
|
||||
PicBoxBomberCollection.Location = new Point(1, -2);
|
||||
PicBoxBomberCollection.Location = new Point(0, 38);
|
||||
PicBoxBomberCollection.Name = "PicBoxBomberCollection";
|
||||
PicBoxBomberCollection.Size = new Size(473, 563);
|
||||
PicBoxBomberCollection.TabIndex = 1;
|
||||
PicBoxBomberCollection.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.ImageScalingSize = new Size(20, 20);
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(800, 28);
|
||||
menuStrip.TabIndex = 2;
|
||||
menuStrip.Text = "Файл";
|
||||
//
|
||||
// fileToolStripMenuItem
|
||||
//
|
||||
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
|
||||
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
|
||||
fileToolStripMenuItem.Size = new Size(59, 24);
|
||||
fileToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// 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;
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.FileName = "openFileDialog";
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.FileName = "saveFileDialog";
|
||||
saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormBomberCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 581);
|
||||
ClientSize = new Size(800, 619);
|
||||
Controls.Add(PicBoxBomberCollection);
|
||||
Controls.Add(Tools);
|
||||
Controls.Add(menuStrip);
|
||||
MainMenuStrip = menuStrip;
|
||||
Name = "FormBomberCollection";
|
||||
Text = "FormBomberCollection";
|
||||
Tools.ResumeLayout(false);
|
||||
@ -167,7 +217,10 @@
|
||||
Kit.ResumeLayout(false);
|
||||
Kit.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)PicBoxBomberCollection).EndInit();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -183,5 +236,11 @@
|
||||
private Button RemoveKit;
|
||||
private Button AddKit;
|
||||
private TextBox KitTextbox;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem fileToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private OpenFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
@ -147,5 +147,38 @@ namespace AirBomber
|
||||
PicBoxBomberCollection.Image =
|
||||
_bomber[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBomber();
|
||||
}
|
||||
|
||||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_bomber.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 (_bomber.LoadData(openFileDialog.FileName))
|
||||
{
|
||||
MessageBox.Show("Данные успешно загружены.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
ReloadObjects();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Ошибка при загрузке данных.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,4 +117,16 @@
|
||||
<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="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>145, 1</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>315, 1</value>
|
||||
</metadata>
|
||||
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>25</value>
|
||||
</metadata>
|
||||
</root>
|
@ -69,10 +69,8 @@ namespace ProjectBomber.Generics
|
||||
public bool Remove(int position)
|
||||
{
|
||||
/// Проверка позиции
|
||||
if (position < 0 || position >= _places.Count)
|
||||
return false;
|
||||
/// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
_places[position] = null;
|
||||
if ((position < 0) || (position > _maxCount)) return false;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user