lab6 DONE

This commit is contained in:
Галина Федоренко 2023-11-18 18:35:07 +04:00
parent aa83710ef8
commit c711032928
6 changed files with 244 additions and 6 deletions

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hydroplane.Entities;
namespace Hydroplane.DrawningObjects
{
public static class ExtentionDrawningPlane
{
public static DrawningPlane? CreateDrawningPlane(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningPlane(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningHydroplane(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 DrawningPlane drawningPlane,
char separatorForObject)
{
var plane = drawningPlane.EntityPlane;
if (plane == null)
{
return string.Empty;
}
var str = $"{plane.Speed}{separatorForObject}{plane.Weight}{separatorForObject}{plane.BodyColor.Name}";
if (plane is not EntityHydroplane hydroplane)
{
return str;
}
return
$"{str}{separatorForObject}{hydroplane.AdditionalColor.Name}{separatorForObject}{hydroplane.Boat}{separatorForObject}{hydroplane.Bobber}";
}
}
}

View File

@ -41,9 +41,16 @@
PlaneTextBox = new TextBox();
label1 = new Label();
DrawPlane = new PictureBox();
StripMenu = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
saveToolStripMenuItem = new ToolStripMenuItem();
loadToolStripMenuItem = new ToolStripMenuItem();
SaveFileDialog = new SaveFileDialog();
OpenFileDialog = new OpenFileDialog();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit();
StripMenu.SuspendLayout();
SuspendLayout();
//
// panel1
@ -55,10 +62,10 @@
panel1.Controls.Add(PlaneTextBox);
panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(646, 0);
panel1.Location = new Point(646, 24);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1";
panel1.Size = new Size(219, 386);
panel1.Size = new Size(219, 362);
panel1.TabIndex = 0;
//
// panel2
@ -177,13 +184,52 @@
// DrawPlane
//
DrawPlane.Dock = DockStyle.Fill;
DrawPlane.Location = new Point(0, 0);
DrawPlane.Location = new Point(0, 24);
DrawPlane.Margin = new Padding(3, 2, 3, 2);
DrawPlane.Name = "DrawPlane";
DrawPlane.Size = new Size(646, 386);
DrawPlane.Size = new Size(646, 362);
DrawPlane.TabIndex = 1;
DrawPlane.TabStop = false;
//
// StripMenu
//
StripMenu.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
StripMenu.Location = new Point(0, 0);
StripMenu.Name = "StripMenu";
StripMenu.Size = new Size(865, 24);
StripMenu.TabIndex = 2;
StripMenu.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
fileToolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.Size = new Size(100, 22);
saveToolStripMenuItem.Text = "Save";
saveToolStripMenuItem.Click += SaveToolStripMenu_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.Size = new Size(100, 22);
loadToolStripMenuItem.Text = "Load";
loadToolStripMenuItem.Click += LoadToolStripMenu_Click;
//
// SaveFileDialog
//
SaveFileDialog.Filter = "txt file | *.txt";
//
// OpenFileDialog
//
OpenFileDialog.FileName = "openFileDialog1";
OpenFileDialog.Filter = "txt file | *.txt";
//
// FormHydroplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -191,6 +237,8 @@
ClientSize = new Size(865, 386);
Controls.Add(DrawPlane);
Controls.Add(panel1);
Controls.Add(StripMenu);
MainMenuStrip = StripMenu;
Margin = new Padding(3, 2, 3, 2);
Name = "FormHydroplaneCollection";
Text = "Гаражи гидропланов";
@ -199,7 +247,10 @@
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit();
StripMenu.ResumeLayout(false);
StripMenu.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -217,5 +268,11 @@
private Button AddCollectButton;
private TextBox SetTextBox;
private Label label2;
private MenuStrip StripMenu;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog SaveFileDialog;
private OpenFileDialog OpenFileDialog;
}
}

View File

@ -166,8 +166,7 @@ namespace Hydroplane
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
@ -181,5 +180,40 @@ namespace Hydroplane
}
DrawPlane.Image = obj.ShowPlanes();
}
private void SaveToolStripMenu_Click(object sender, EventArgs e)
{
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(SaveFileDialog.FileName))
{
MessageBox.Show("Save Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Save Not Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(OpenFileDialog.FileName))
{
MessageBox.Show("Load Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadObjects();
}
else
{
MessageBox.Show("Load Not Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -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="StripMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>282, 17</value>
</metadata>
<metadata name="SaveFileDialog.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>147, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>128</value>
</metadata>
</root>

View File

@ -88,5 +88,7 @@ namespace Hydroplane.Generics
}
}
}
public IEnumerable<T?> GetPlanes => _collection.GetPlanes();
}
}

View File

@ -41,5 +41,88 @@ namespace Hydroplane.Generics
return null;
}
}
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>> record in _planeStorages)
{
StringBuilder records = new();
foreach (DrawningPlane? elem in record.Value.GetPlanes)
{
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($"PlaneStorage{Environment.NewLine}{data}");
}
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.Length == 0 || strs == null)
{
return false;
}
if (!strs[0].StartsWith("PlaneStorage"))
{
return false;
}
_planeStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
PlanesGenericCollection<DrawningPlane, DrawningObjectPlane> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningPlane? plane = elem.CreateDrawningPlane(_separatorForObject, _pictureWidth, _pictureHeight);
if (plane != null)
{
if (!(collection + plane))
{
return false;
}
}
}
_planeStorages.Add(record[0], collection);
}
return true;
}
}
}