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

View File

@ -61,11 +61,11 @@ namespace SailBoat
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if(listBoxStorages.SelectedIndex == -1)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if(MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
@ -133,6 +133,35 @@ namespace SailBoat
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">
<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>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>

View File

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

View File

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

View File

@ -45,9 +45,93 @@ namespace SailBoat.Generics
{
get
{
if(_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
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;
}
}
}
}
}