PIbd-22_Petrushin_E.A._Lab6 #11
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SelfPropelledArtilleryUnit
|
||||
{
|
||||
internal class Extention
|
||||
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SelfPropelledArtilleryUnit.Entities;
|
||||
|
||||
namespace SelfPropelledArtilleryUnit.DrawningObjects
|
||||
{
|
||||
public static class ExtentionDrawningSPAU
|
||||
{
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info">Строка с данными для создания объекта</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <param name="width">Ширина</param>
|
||||
/// <param name="height">Высота</param>
|
||||
/// <returns>Объект</returns>
|
||||
public static DrawningSPAU? CreateDrawningSPAU(this string info, char separatorForObject, int width, int height)
|
||||
{
|
||||
string[] strs = info.Split(separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawningSPAU(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningSPAUchild(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="drawningCar">Сохраняемый объект</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <returns>Строка с данными по объекту</returns>
|
||||
public static string GetDataForSave(this DrawningSPAU drawningSPAU, char separatorForObject)
|
||||
{
|
||||
var sPAU = drawningSPAU.EntitySPAU;
|
||||
if (sPAU == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var str = $"{sPAU.Speed}{separatorForObject}{sPAU.Weight}{separatorForObject}{sPAU.BodyColor.Name}";
|
||||
if (sPAU is not EntitySPAUchild sPAUchild)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{separatorForObject}{sPAUchild.AdditionalColor.Name}{separatorForObject}{sPAUchild.BodyKit}{separatorForObject}{sPAUchild.Ballon}";
|
||||
}
|
||||
}
|
||||
}
|
@ -39,14 +39,21 @@
|
||||
ButtonDelObject = new Button();
|
||||
ButtonAddObject = new Button();
|
||||
textBoxStorageName = new TextBox();
|
||||
menuStrip = new MenuStrip();
|
||||
toolStripMenuItem1 = new ToolStripMenuItem();
|
||||
openFileDialog = new ToolStripMenuItem();
|
||||
saveFileDialog = new ToolStripMenuItem();
|
||||
openFileDialog_ = new OpenFileDialog();
|
||||
saveFileDialog_ = new OpenFileDialog();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
panel1.SuspendLayout();
|
||||
panel2.SuspendLayout();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// pictureBoxCollection
|
||||
//
|
||||
pictureBoxCollection.Location = new Point(12, 12);
|
||||
pictureBoxCollection.Location = new Point(12, 40);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(680, 432);
|
||||
pictureBoxCollection.TabIndex = 0;
|
||||
@ -95,7 +102,7 @@
|
||||
panel1.Controls.Add(ButtonRemoveSPAU);
|
||||
panel1.Controls.Add(ButtonAddSPAU);
|
||||
panel1.Controls.Add(maskedTextBoxNumber);
|
||||
panel1.Location = new Point(711, 217);
|
||||
panel1.Location = new Point(711, 245);
|
||||
panel1.Name = "panel1";
|
||||
panel1.Size = new Size(176, 227);
|
||||
panel1.TabIndex = 5;
|
||||
@ -106,9 +113,9 @@
|
||||
panel2.Controls.Add(ButtonDelObject);
|
||||
panel2.Controls.Add(ButtonAddObject);
|
||||
panel2.Controls.Add(textBoxStorageName);
|
||||
panel2.Location = new Point(711, 12);
|
||||
panel2.Location = new Point(711, 40);
|
||||
panel2.Name = "panel2";
|
||||
panel2.Size = new Size(174, 198);
|
||||
panel2.Size = new Size(174, 199);
|
||||
panel2.TabIndex = 6;
|
||||
//
|
||||
// listBoxStorages
|
||||
@ -148,14 +155,57 @@
|
||||
textBoxStorageName.Size = new Size(143, 27);
|
||||
textBoxStorageName.TabIndex = 0;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.ImageScalingSize = new Size(20, 20);
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.Size = new Size(899, 28);
|
||||
menuStrip.TabIndex = 7;
|
||||
menuStrip.Text = "menuStrip1";
|
||||
//
|
||||
// toolStripMenuItem1
|
||||
//
|
||||
toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { openFileDialog, saveFileDialog });
|
||||
toolStripMenuItem1.Name = "toolStripMenuItem1";
|
||||
toolStripMenuItem1.Size = new Size(59, 24);
|
||||
toolStripMenuItem1.Text = "Файл";
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.Name = "openFileDialog";
|
||||
openFileDialog.Size = new Size(224, 26);
|
||||
openFileDialog.Text = "Открыть файл";
|
||||
openFileDialog.Click += LoadToolStripMenuItem_Click;
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.Name = "saveFileDialog";
|
||||
saveFileDialog.Size = new Size(224, 26);
|
||||
saveFileDialog.Text = "Сохранить файл";
|
||||
saveFileDialog.Click += SaveToolStripMenuItem_Click;
|
||||
//
|
||||
// openFileDialog_
|
||||
//
|
||||
openFileDialog_.FileName = "openFileDialog";
|
||||
openFileDialog_.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog_
|
||||
//
|
||||
saveFileDialog_.FileName = "saveFileDialog";
|
||||
saveFileDialog_.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormSPAUCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(899, 456);
|
||||
ClientSize = new Size(899, 484);
|
||||
Controls.Add(panel2);
|
||||
Controls.Add(panel1);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Controls.Add(menuStrip);
|
||||
MainMenuStrip = menuStrip;
|
||||
Name = "FormSPAUCollection";
|
||||
Text = "FormSPAUCollection";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
@ -163,7 +213,10 @@
|
||||
panel1.PerformLayout();
|
||||
panel2.ResumeLayout(false);
|
||||
panel2.PerformLayout();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -179,5 +232,11 @@
|
||||
private TextBox textBoxStorageName;
|
||||
private Button ButtonDelObject;
|
||||
private ListBox listBoxStorages;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem toolStripMenuItem1;
|
||||
private ToolStripMenuItem openFileDialog;
|
||||
private ToolStripMenuItem saveFileDialog;
|
||||
private OpenFileDialog openFileDialog_;
|
||||
private OpenFileDialog saveFileDialog_;
|
||||
}
|
||||
}
|
@ -207,7 +207,47 @@ namespace SelfPropelledArtilleryUnit
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowSPAUs();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Сохранение"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Загрузка"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (openFileDialog_.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_storage.LoadData(openFileDialog_.FileName))
|
||||
{
|
||||
MessageBox.Show("Загрузка прошлa успешно", "Результат", 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="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>283, 23</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog_.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>130, 23</value>
|
||||
</metadata>
|
||||
</root>
|
@ -142,7 +142,7 @@
|
||||
//
|
||||
// panelPurple
|
||||
//
|
||||
panelPurple.BackColor = Color.FromArgb(192, 0, 192);
|
||||
panelPurple.BackColor = Color.Magenta;
|
||||
panelPurple.Location = new Point(91, 123);
|
||||
panelPurple.Name = "panelPurple";
|
||||
panelPurple.Size = new Size(60, 25);
|
||||
|
@ -51,6 +51,10 @@ namespace SelfPropelledArtilleryUnit.Generics
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объектов коллекции
|
||||
/// </summary>
|
||||
public IEnumerable<T?> GetCars => _collection.GetSPAUs();
|
||||
/// <summary>
|
||||
/// Перегрузка оператора сложения
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
|
@ -79,6 +79,109 @@ namespace SelfPropelledArtilleryUnit.Generics
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char _separatorRecords = ';';
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение информации по автомобилям в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> record in _SPAUStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningSPAU? elem in record.Value.GetCars.Reverse())
|
||||
{
|
||||
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.WriteLine("SPAUStorage");
|
||||
writer.Write(data.ToString());
|
||||
}
|
||||
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 fs = File.OpenText(filename))
|
||||
{
|
||||
string str = fs.ReadLine();
|
||||
if (str == null || str.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!str.StartsWith("SPAUStorage"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_SPAUStorages.Clear();
|
||||
string strs = "";
|
||||
|
||||
while ((strs = fs.ReadLine()) != null)
|
||||
{
|
||||
if (strs == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawningSPAU? sPAU = elem?.CreateDrawningSPAU(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (sPAU != null)
|
||||
{
|
||||
if ((collection + sPAU) == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_SPAUStorages.Add(record[0], collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
Пустых классов быть не должно