готовая 6ая

This commit is contained in:
dex_moth 2023-12-02 09:51:44 +04:00
parent 4acd470e61
commit 6018455bd6
6 changed files with 275 additions and 30 deletions

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirbus.Entities;
namespace ProjectAirbus.Drawnings
{
public static class ExtentionDrawningAirbus
{
// создание объекта из строки
public static DrawningAirbus? CreateDrawningAirbus(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningAirbus(
Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningPlane(
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 DrawningAirbus drawningAirbus, char separatorForObject)
{
var airbus = drawningAirbus.EntityAirbus;
if (airbus == null)
{
return string.Empty;
}
var str = $"{airbus.Speed}{separatorForObject}{airbus.Weight}{separatorForObject}{airbus.BodyColor.Name}";
if (airbus is not EntityPlane plane)
{
return str;
}
return $"{str}{separatorForObject}{plane.AdditionalColor.Name}{separatorForObject}{plane.IsCompartment}{separatorForObject}{plane.IsAdditionalEngine}";
}
}
}

View File

@ -40,15 +40,23 @@
buttonAddStorage = new Button(); buttonAddStorage = new Button();
buttonDeleteStorage = new Button(); buttonDeleteStorage = new Button();
textBoxStorageName = new TextBox(); textBoxStorageName = new TextBox();
menuStrip = new MenuStrip();
ToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// buttonAddAirbus // buttonAddAirbus
// //
buttonAddAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right; buttonAddAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddAirbus.Location = new Point(572, 332); buttonAddAirbus.Location = new Point(654, 443);
buttonAddAirbus.Margin = new Padding(3, 4, 3, 4);
buttonAddAirbus.Name = "buttonAddAirbus"; buttonAddAirbus.Name = "buttonAddAirbus";
buttonAddAirbus.Size = new Size(137, 28); buttonAddAirbus.Size = new Size(157, 37);
buttonAddAirbus.TabIndex = 0; buttonAddAirbus.TabIndex = 0;
buttonAddAirbus.Text = "Добавить самолёт"; buttonAddAirbus.Text = "Добавить самолёт";
buttonAddAirbus.UseVisualStyleBackColor = true; buttonAddAirbus.UseVisualStyleBackColor = true;
@ -56,9 +64,10 @@
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Location = new Point(0, 28);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(552, 436); pictureBoxCollection.Size = new Size(631, 553);
pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
@ -67,18 +76,19 @@
labelInstruments.Anchor = AnchorStyles.Top | AnchorStyles.Right; labelInstruments.Anchor = AnchorStyles.Top | AnchorStyles.Right;
labelInstruments.AutoSize = true; labelInstruments.AutoSize = true;
labelInstruments.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point); labelInstruments.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
labelInstruments.Location = new Point(580, 0); labelInstruments.Location = new Point(654, 23);
labelInstruments.Name = "labelInstruments"; labelInstruments.Name = "labelInstruments";
labelInstruments.Size = new Size(108, 21); labelInstruments.Size = new Size(136, 28);
labelInstruments.TabIndex = 2; labelInstruments.TabIndex = 2;
labelInstruments.Text = "Инструменты"; labelInstruments.Text = "Инструменты";
// //
// buttonUpdate // buttonUpdate
// //
buttonUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Right; buttonUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonUpdate.Location = new Point(572, 130); buttonUpdate.Location = new Point(654, 173);
buttonUpdate.Margin = new Padding(3, 4, 3, 4);
buttonUpdate.Name = "buttonUpdate"; buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(131, 25); buttonUpdate.Size = new Size(150, 33);
buttonUpdate.TabIndex = 3; buttonUpdate.TabIndex = 3;
buttonUpdate.Text = "Обновить набор"; buttonUpdate.Text = "Обновить набор";
buttonUpdate.UseVisualStyleBackColor = true; buttonUpdate.UseVisualStyleBackColor = true;
@ -87,9 +97,10 @@
// buttonDeleteAirbus // buttonDeleteAirbus
// //
buttonDeleteAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right; buttonDeleteAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonDeleteAirbus.Location = new Point(572, 392); buttonDeleteAirbus.Location = new Point(654, 523);
buttonDeleteAirbus.Margin = new Padding(3, 4, 3, 4);
buttonDeleteAirbus.Name = "buttonDeleteAirbus"; buttonDeleteAirbus.Name = "buttonDeleteAirbus";
buttonDeleteAirbus.Size = new Size(137, 28); buttonDeleteAirbus.Size = new Size(157, 37);
buttonDeleteAirbus.TabIndex = 4; buttonDeleteAirbus.TabIndex = 4;
buttonDeleteAirbus.Text = "Удалить самолёт"; buttonDeleteAirbus.Text = "Удалить самолёт";
buttonDeleteAirbus.UseVisualStyleBackColor = true; buttonDeleteAirbus.UseVisualStyleBackColor = true;
@ -98,10 +109,11 @@
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right;
maskedTextBoxNumber.Location = new Point(572, 365); maskedTextBoxNumber.Location = new Point(654, 487);
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(138, 23); maskedTextBoxNumber.Size = new Size(157, 27);
maskedTextBoxNumber.TabIndex = 5; maskedTextBoxNumber.TabIndex = 5;
maskedTextBoxNumber.ValidatingType = typeof(int); maskedTextBoxNumber.ValidatingType = typeof(int);
// //
@ -109,29 +121,27 @@
// //
label1.Anchor = AnchorStyles.Top | AnchorStyles.Right; label1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
label1.AutoSize = true; label1.AutoSize = true;
label1.Location = new Point(572, 38); label1.Location = new Point(654, 62);
label1.Name = "label1"; label1.Name = "label1";
label1.Size = new Size(52, 15); label1.Size = new Size(66, 20);
label1.TabIndex = 7; label1.TabIndex = 7;
label1.Text = "Наборы"; label1.Text = "Наборы";
// //
// listBoxStorages // listBoxStorages
// //
listBoxStorages.FormattingEnabled = true; listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15; listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(572, 160); listBoxStorages.Location = new Point(654, 213);
listBoxStorages.Margin = new Padding(3, 2, 3, 2);
listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(132, 109); listBoxStorages.Size = new Size(150, 144);
listBoxStorages.TabIndex = 8; listBoxStorages.TabIndex = 8;
listBoxStorages.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged; listBoxStorages.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged;
// //
// buttonAddStorage // buttonAddStorage
// //
buttonAddStorage.Location = new Point(572, 102); buttonAddStorage.Location = new Point(654, 136);
buttonAddStorage.Margin = new Padding(3, 2, 3, 2);
buttonAddStorage.Name = "buttonAddStorage"; buttonAddStorage.Name = "buttonAddStorage";
buttonAddStorage.Size = new Size(131, 25); buttonAddStorage.Size = new Size(150, 33);
buttonAddStorage.TabIndex = 9; buttonAddStorage.TabIndex = 9;
buttonAddStorage.Text = "Добавить набор"; buttonAddStorage.Text = "Добавить набор";
buttonAddStorage.UseVisualStyleBackColor = true; buttonAddStorage.UseVisualStyleBackColor = true;
@ -139,10 +149,9 @@
// //
// buttonDeleteStorage // buttonDeleteStorage
// //
buttonDeleteStorage.Location = new Point(572, 273); buttonDeleteStorage.Location = new Point(654, 364);
buttonDeleteStorage.Margin = new Padding(3, 2, 3, 2);
buttonDeleteStorage.Name = "buttonDeleteStorage"; buttonDeleteStorage.Name = "buttonDeleteStorage";
buttonDeleteStorage.Size = new Size(131, 25); buttonDeleteStorage.Size = new Size(150, 33);
buttonDeleteStorage.TabIndex = 10; buttonDeleteStorage.TabIndex = 10;
buttonDeleteStorage.Text = "Удалить набор"; buttonDeleteStorage.Text = "Удалить набор";
buttonDeleteStorage.UseVisualStyleBackColor = true; buttonDeleteStorage.UseVisualStyleBackColor = true;
@ -150,17 +159,56 @@
// //
// textBoxStorageName // textBoxStorageName
// //
textBoxStorageName.Location = new Point(572, 77); textBoxStorageName.Location = new Point(654, 103);
textBoxStorageName.Margin = new Padding(3, 2, 3, 2);
textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(132, 23); textBoxStorageName.Size = new Size(150, 27);
textBoxStorageName.TabIndex = 11; textBoxStorageName.TabIndex = 11;
// //
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(827, 28);
menuStrip.TabIndex = 12;
menuStrip.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;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
saveFileDialog.OverwritePrompt = false;
//
// FormAirbusCollection // FormAirbusCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(724, 443); ClientSize = new Size(827, 591);
Controls.Add(textBoxStorageName); Controls.Add(textBoxStorageName);
Controls.Add(buttonDeleteStorage); Controls.Add(buttonDeleteStorage);
Controls.Add(buttonAddStorage); Controls.Add(buttonAddStorage);
@ -172,9 +220,14 @@
Controls.Add(buttonUpdate); Controls.Add(buttonUpdate);
Controls.Add(labelInstruments); Controls.Add(labelInstruments);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Margin = new Padding(3, 4, 3, 4);
Name = "FormAirbusCollection"; Name = "FormAirbusCollection";
Text = "Набор самолётов"; Text = "Набор самолётов";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
@ -193,5 +246,11 @@
private Button buttonAddStorage; private Button buttonAddStorage;
private Button buttonDeleteStorage; private Button buttonDeleteStorage;
private TextBox textBoxStorageName; private TextBox textBoxStorageName;
private MenuStrip menuStrip;
private ToolStripMenuItem ToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
} }
} }

View File

@ -24,6 +24,41 @@ namespace ProjectAirbus
_storage = new AirbusGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new AirbusGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
} }
// Обработка нажатия "Сохранение"
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))
{
//var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
//pictureBoxCollection.Image = obj.ShowAirbus();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ReloadObjects();
}
// заполнение лист бокс // заполнение лист бокс
private void ReloadObjects() private void ReloadObjects()
{ {

View File

@ -120,7 +120,16 @@
<metadata name="colorDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="colorDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>156, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>292, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>462, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>36</value> <value>89</value>
</metadata> </metadata>
</root> </root>

View File

@ -46,6 +46,9 @@ namespace ProjectAirbus.Generics
return collect?._collection.Remove(pos) ?? false; return collect?._collection.Remove(pos) ?? false;
} }
// получение объектов коллекции
public IEnumerable<T?> GetAirbus => _collection.GetAirbus();
// получение объекта IMoveableObjecr // получение объекта IMoveableObjecr
public U? GetU(int pos) public U? GetU(int pos)
{ {

View File

@ -21,6 +21,14 @@ namespace ProjectAirbus.Generics
//Высота окна отрисовки //Высота окна отрисовки
private readonly int _pictureHeight; private readonly int _pictureHeight;
// Разделитель для записи ключа и значения элемента словаря
private static readonly char _separatorForKeyValue = '|';
// Разделитель для записей коллекции данных в файл
private readonly char _separatorRecords = ';';
// Разделитель для записи информации по объекту в файл
private static readonly char _separatorForObject = ':';
public AirbusGenericStorage(int pictureWidth, int pictureHeight) public AirbusGenericStorage(int pictureWidth, int pictureHeight)
{ {
_airbusStorages = new Dictionary<string, AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus>>(); _airbusStorages = new Dictionary<string, AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus>>();
@ -64,5 +72,86 @@ namespace ProjectAirbus.Generics
return null; return null;
} }
} }
// Сохранение информации по автомобилям в хранилище в файл
public bool SaveData(string filename)
{
if (_airbusStorages.Count == 0)
return false;
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = File.CreateText(filename))
{
sw.WriteLine($"AirbusStorage");
foreach (var record in _airbusStorages)
{
StringBuilder records = new();
foreach (DrawningAirbus? elem in record.Value.GetAirbus)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}");
}
}
return true;
}
// Загрузка информации по автомобилям в хранилище из файла
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = File.OpenText(filename))
{
// 1-ая строка
string? curLine = sr.ReadLine();
// пустая или не те данные
if (curLine == null || curLine.Length == 0 || !curLine.StartsWith("AirbusStorage"))
{
return false;
}
// очищаем
_airbusStorages.Clear();
// загружаем данные построчно
curLine = sr.ReadLine();
while (curLine != null)
{
// загружаем запись
string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
// загружаем набор
AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> collection = new(_pictureWidth, _pictureHeight);
// record[0] - название набора, record[1] - куча объектов
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningAirbus? airbus = elem?.CreateDrawningAirbus(_separatorForObject, _pictureWidth, _pictureHeight);
// проверяем, не переполнится ли коллекция
if (airbus != null)
{
if (collection + airbus == -1)
{
return false;
}
}
}
_airbusStorages.Add(record[0], collection);
curLine = sr.ReadLine();
}
}
return true;
}
} }
} }