Сделанная 6 лабораторная

This commit is contained in:
Максим Яковлев 2023-12-03 20:18:40 +04:00
parent ff1ab7fbc9
commit a55d9450cf
7 changed files with 239 additions and 17 deletions

View File

@ -0,0 +1,41 @@
using SailBoat.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.DrawningObjects
{
public static class ExtentionDrawningBoat
{
public static DrawningBoat? CreateDrawningBoat(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if(strs.Length == 3)
{
return new DrawningBoat(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if(strs.Length == 6)
{
return new DrawningSailBoat(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 DrawningBoat drawningBoat, char separatorForObject)
{
var boat = drawningBoat.EntityBoat;
if(boat == null)
{
return string.Empty;
}
var str = $"{boat.Speed}{separatorForObject}{boat.Weight}{separatorForObject}{boat.BodyColor.Name}";
if(boat is not EntitySailBoat sailBoat)
{
return str;
}
return $"{str}{separatorForObject}{sailBoat.AdditionalColor.Name}{separatorForObject}{sailBoat.Sail}{separatorForObject}{sailBoat.RainforcedBody}";
}
}
}

View File

@ -39,9 +39,16 @@
buttonRemoveBoat = new Button(); buttonRemoveBoat = new Button();
buttonAddBoat = new Button(); buttonAddBoat = new Button();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
menuStrip = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
tools.SuspendLayout(); tools.SuspendLayout();
sets.SuspendLayout(); sets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// tools // tools
@ -52,9 +59,9 @@
tools.Controls.Add(buttonRemoveBoat); tools.Controls.Add(buttonRemoveBoat);
tools.Controls.Add(buttonAddBoat); tools.Controls.Add(buttonAddBoat);
tools.Dock = DockStyle.Right; tools.Dock = DockStyle.Right;
tools.Location = new Point(716, 0); tools.Location = new Point(716, 24);
tools.Name = "tools"; tools.Name = "tools";
tools.Size = new Size(200, 608); tools.Size = new Size(200, 613);
tools.TabIndex = 0; tools.TabIndex = 0;
tools.TabStop = false; tools.TabStop = false;
tools.Text = "Инструменты"; tools.Text = "Инструменты";
@ -67,14 +74,14 @@
sets.Controls.Add(textBoxStorageName); sets.Controls.Add(textBoxStorageName);
sets.Location = new Point(6, 22); sets.Location = new Point(6, 22);
sets.Name = "sets"; sets.Name = "sets";
sets.Size = new Size(188, 264); sets.Size = new Size(188, 344);
sets.TabIndex = 5; sets.TabIndex = 5;
sets.TabStop = false; sets.TabStop = false;
sets.Text = "Наборы"; sets.Text = "Наборы";
// //
// ButtonDelObject // ButtonDelObject
// //
ButtonDelObject.Location = new Point(6, 215); ButtonDelObject.Location = new Point(6, 263);
ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(176, 41); ButtonDelObject.Size = new Size(176, 41);
ButtonDelObject.TabIndex = 3; ButtonDelObject.TabIndex = 3;
@ -86,7 +93,7 @@
// //
listBoxStorages.FormattingEnabled = true; listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15; listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 100); listBoxStorages.Location = new Point(6, 148);
listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(176, 109); listBoxStorages.Size = new Size(176, 109);
listBoxStorages.TabIndex = 2; listBoxStorages.TabIndex = 2;
@ -94,7 +101,7 @@
// //
// ButtonAddObject // ButtonAddObject
// //
ButtonAddObject.Location = new Point(6, 53); ButtonAddObject.Location = new Point(6, 39);
ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(176, 41); ButtonAddObject.Size = new Size(176, 41);
ButtonAddObject.TabIndex = 1; ButtonAddObject.TabIndex = 1;
@ -104,7 +111,7 @@
// //
// textBoxStorageName // textBoxStorageName
// //
textBoxStorageName.Location = new Point(6, 23); textBoxStorageName.Location = new Point(6, 97);
textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(176, 23); textBoxStorageName.Size = new Size(176, 23);
textBoxStorageName.TabIndex = 0; textBoxStorageName.TabIndex = 0;
@ -120,7 +127,7 @@
// //
// buttonRefreshCollection // buttonRefreshCollection
// //
buttonRefreshCollection.Location = new Point(6, 561); buttonRefreshCollection.Location = new Point(6, 545);
buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(188, 39); buttonRefreshCollection.Size = new Size(188, 39);
buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.TabIndex = 3;
@ -150,19 +157,60 @@
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Location = new Point(0, 24);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(710, 608); pictureBoxCollection.Size = new Size(710, 613);
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.RenderMode = ToolStripRenderMode.System;
menuStrip.Size = new Size(916, 24);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip";
//
// файл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(180, 22);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// FormBoatCollection // FormBoatCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(916, 608); ClientSize = new Size(916, 637);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(tools); Controls.Add(tools);
Controls.Add(menuStrip);
Name = "FormBoatCollection"; Name = "FormBoatCollection";
Text = "Набор лодок"; Text = "Набор лодок";
tools.ResumeLayout(false); tools.ResumeLayout(false);
@ -170,7 +218,10 @@
sets.ResumeLayout(false); sets.ResumeLayout(false);
sets.PerformLayout(); sets.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -186,5 +237,11 @@
private ListBox listBoxStorages; private ListBox listBoxStorages;
private Button ButtonAddObject; private Button ButtonAddObject;
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

@ -133,6 +133,35 @@ namespace SailBoat
pictureBoxCollection.Image = obj.ShowBoats(); pictureBoxCollection.Image = obj.ShowBoats();
} }
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))
{
ReloadObjects();
MessageBox.Show("Загрузка произошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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>132, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>247, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>387, 17</value>
</metadata>
</root> </root>

View File

@ -168,7 +168,7 @@
// //
// panelGreen // panelGreen
// //
panelGreen.BackColor = Color.FromArgb(0, 192, 0); panelGreen.BackColor = Color.LimeGreen;
panelGreen.Location = new Point(71, 22); panelGreen.Location = new Point(71, 22);
panelGreen.Name = "panelGreen"; panelGreen.Name = "panelGreen";
panelGreen.Size = new Size(50, 50); panelGreen.Size = new Size(50, 50);

View File

@ -91,5 +91,7 @@ namespace SailBoat.Generics
i++; i++;
} }
} }
public IEnumerable<T?> GetBoats => _collection.GetBoats();
} }
} }

View File

@ -49,5 +49,89 @@ namespace SailBoat.Generics
return null; 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, BoatsGenericCollection < DrawningBoat, DrawningObjectBoat>> record in _boatStorages)
{
StringBuilder records = new();
foreach (DrawningBoat? elem in record.Value.GetBoats)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.Append($"{record.Key}{_separatorForKeyValue}{records}\n");
}
if (data.Length == 0)
{
return false;
}
using StreamWriter fs = new StreamWriter(filename);
string info = $"BoatStorage{Environment.NewLine}{data}";
fs.Write(info, 0, info.Length);
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader fs = new StreamReader(filename))
{
string str = fs.ReadLine();
if (!str.StartsWith("BoatStorage"))
{
return false;
}
_boatStorages.Clear();
while ((str = fs.ReadLine()) != null)
{
if (str.Length == 0)
{
return false;
}
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
BoatsGenericCollection<DrawningBoat, DrawningObjectBoat> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningBoat? boat = elem?.CreateDrawningBoat(_separatorForObject, _pictureWidth, _pictureHeight);
if (boat != null)
{
if (!(collection + boat))
{
return false;
}
}
}
_boatStorages.Add(record[0], collection);
}
return true;
}
}
} }
} }