Лабораторная работа 6

This commit is contained in:
Efi 2023-12-26 16:16:22 +04:00
parent 970ee90862
commit f52af0e723
6 changed files with 241 additions and 10 deletions

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.Entities;
namespace AirBomber.DrawingObjects
{
public static class ExtentionDrawingWarAirplane
{
public static DrawingWarAirplane? CreateDrawingWarAirplane(this string info, char separatorForObject,
int width, int height)
{
string[] strs = info.Split(separatorForObject);
if(strs.Length == 3)
{
return new DrawingWarAirplane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if(strs.Length == 6)
{
return new DrawingAirBomber(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 DrawingWarAirplane drawingWarAirplane, char separatorForObject)
{
var warAirplane = drawingWarAirplane.EntityWarAirplane;
if(warAirplane == null)
{
return string.Empty;
}
var str = $"{warAirplane.Speed}{separatorForObject}{warAirplane.Weight}" +
$"{separatorForObject}{warAirplane.BodyColor.Name}";
if(warAirplane is not EntityAirBomber airBomber)
{
return str;
}
return $"{str}{separatorForObject}{airBomber.AdditionalColor.Name}{separatorForObject}" +
$"{airBomber.FuelTank}{separatorForObject}{airBomber.Bombs}";
}
}
}

View File

@ -40,9 +40,16 @@
buttonAdd = new Button(); buttonAdd = new Button();
maskedTextBoxNumber = new MaskedTextBox(); maskedTextBoxNumber = new MaskedTextBox();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
menuStrip = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
panelCollection.SuspendLayout(); panelCollection.SuspendLayout();
panelObject.SuspendLayout(); panelObject.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// panelCollection // panelCollection
@ -54,9 +61,9 @@
panelCollection.Controls.Add(buttonAdd); panelCollection.Controls.Add(buttonAdd);
panelCollection.Controls.Add(maskedTextBoxNumber); panelCollection.Controls.Add(maskedTextBoxNumber);
panelCollection.Dock = DockStyle.Right; panelCollection.Dock = DockStyle.Right;
panelCollection.Location = new Point(617, 0); panelCollection.Location = new Point(617, 24);
panelCollection.Name = "panelCollection"; panelCollection.Name = "panelCollection";
panelCollection.Size = new Size(183, 450); panelCollection.Size = new Size(183, 456);
panelCollection.TabIndex = 0; panelCollection.TabIndex = 0;
// //
// panelObject // panelObject
@ -156,19 +163,59 @@
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Dock = DockStyle.Left; pictureBoxCollection.Dock = DockStyle.Left;
pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(617, 450); pictureBoxCollection.Size = new Size(617, 456);
pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
// menuStrip
//
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(800, 24);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(133, 22);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(133, 22);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// FormWarAirplaneCollection // FormWarAirplaneCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 480);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(panelCollection); Controls.Add(panelCollection);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Name = "FormWarAirplaneCollection"; Name = "FormWarAirplaneCollection";
Text = "Набор военных самолётов"; Text = "Набор военных самолётов";
panelCollection.ResumeLayout(false); panelCollection.ResumeLayout(false);
@ -176,7 +223,10 @@
panelObject.ResumeLayout(false); panelObject.ResumeLayout(false);
panelObject.PerformLayout(); panelObject.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -193,5 +243,11 @@
private Button buttonDelObject; private Button buttonDelObject;
private Panel panelObject; private Panel panelObject;
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

@ -157,6 +157,44 @@ namespace AirBomber
} }
pictureBoxCollection.Image = obj.ShowWarAirplane(); pictureBoxCollection.Image = obj.ShowWarAirplane();
} }
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))
{
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"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </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>132, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>271, 17</value>
</metadata>
</root> </root>

View File

@ -18,6 +18,7 @@ namespace AirBomber.Generics
private readonly int _placeSizeWidth = 110; private readonly int _placeSizeWidth = 110;
private readonly int _placeSizeHeight = 110; private readonly int _placeSizeHeight = 110;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public IEnumerable<T?> GetWarAirplanes => _collection.GetWarAirplane();
public WarAirplaneGenericCollection(int picWidth, int picHeight) public WarAirplaneGenericCollection(int picWidth, int picHeight)
{ {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;

View File

@ -15,6 +15,9 @@ namespace AirBomber.Generics
public List<string> Keys => _warAirplaneStorages.Keys.ToList(); public List<string> Keys => _warAirplaneStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public WarAirplaneGenericStorage(int pictureWidth, int pictureHeight) public WarAirplaneGenericStorage(int pictureWidth, int pictureHeight)
{ {
_warAirplaneStorages = new Dictionary<string, WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>>(); _warAirplaneStorages = new Dictionary<string, WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>>();
@ -39,11 +42,6 @@ namespace AirBomber.Generics
_warAirplaneStorages.Remove(name); _warAirplaneStorages.Remove(name);
} }
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>? this[string ind] public WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>? this[string ind]
{ {
get get
@ -55,5 +53,88 @@ namespace AirBomber.Generics
return null; return null;
} }
} }
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach(KeyValuePair<string,WarAirplaneGenericCollection<
DrawingWarAirplane,DrawingObjectWarAirplane>> record in _warAirplaneStorages)
{
StringBuilder records = new();
foreach(DrawingWarAirplane? elem in record.Value.GetWarAirplanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if(data.Length == 0)
{
return false;
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes($"WarAirplaneStorage" +
$"{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length);
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b,0,b.Length) > 0)
{
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("WarAirplaneStorage"))
{
return false;
}
_warAirplaneStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
WarAirplaneGenericCollection<DrawingWarAirplane, DrawingObjectWarAirplane>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingWarAirplane? warAirplane = elem?.CreateDrawingWarAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
if(warAirplane != null)
{
if(!(collection + warAirplane))
{
return false;
}
}
}
_warAirplaneStorages.Add(record[0], collection);
}
return true;
}
} }
} }