файлы лабораторной

This commit is contained in:
Ivan_Starostin 2023-12-19 20:12:51 +04:00
parent 0208828b09
commit e0092edb54
4 changed files with 239 additions and 22 deletions

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using ProjectLainer.Entities;
namespace ProjectLainer.DrawningObjects
{
public static class ExtensionDrawningLainer
{
public static DrawingLainer? CreateDrawningLainer(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawingLainer(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 7)
{
return new DrawningSuperLainer(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 DrawingLainer drawningLainer, char separatorForObject)
{
var lainer = drawningLainer.EntityLainer;
if (lainer == null)
{
return string.Empty;
}
var str = $"{lainer.Speed}{separatorForObject}{lainer.Weight}{separatorForObject}{lainer.BodyColor.Name}";
if (lainer is not EntitySuperLainer superLainer)
{
return str;
}
return $"{str}{separatorForObject}{superLainer.AdditionalColor.Name}{separatorForObject}{superLainer.Pools}{separatorForObject}{superLainer.Decks}{separatorForObject}";
}
}
}

View File

@ -37,20 +37,27 @@
textBoxStorageName = new TextBox(); textBoxStorageName = new TextBox();
listBoxStorages = new ListBox(); listBoxStorages = new ListBox();
ButtonDelObject = new Button(); ButtonDelObject = new Button();
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripItem = new ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(16, 14); pictureBoxCollection.Location = new Point(12, 56);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(815, 434); pictureBoxCollection.Size = new Size(706, 434);
pictureBoxCollection.TabIndex = 0; pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
// ButtonAddLainer // ButtonAddLainer
// //
ButtonAddLainer.Location = new Point(872, 282); ButtonAddLainer.Location = new Point(804, 374);
ButtonAddLainer.Name = "ButtonAddLainer"; ButtonAddLainer.Name = "ButtonAddLainer";
ButtonAddLainer.Size = new Size(154, 32); ButtonAddLainer.Size = new Size(154, 32);
ButtonAddLainer.TabIndex = 1; ButtonAddLainer.TabIndex = 1;
@ -60,7 +67,7 @@
// //
// ButtonRemove // ButtonRemove
// //
ButtonRemove.Location = new Point(887, 384); ButtonRemove.Location = new Point(807, 453);
ButtonRemove.Name = "ButtonRemove"; ButtonRemove.Name = "ButtonRemove";
ButtonRemove.Size = new Size(139, 37); ButtonRemove.Size = new Size(139, 37);
ButtonRemove.TabIndex = 2; ButtonRemove.TabIndex = 2;
@ -70,7 +77,7 @@
// //
// ButtonRefresh // ButtonRefresh
// //
ButtonRefresh.Location = new Point(849, 449); ButtonRefresh.Location = new Point(808, 510);
ButtonRefresh.Name = "ButtonRefresh"; ButtonRefresh.Name = "ButtonRefresh";
ButtonRefresh.Size = new Size(164, 29); ButtonRefresh.Size = new Size(164, 29);
ButtonRefresh.TabIndex = 3; ButtonRefresh.TabIndex = 3;
@ -80,14 +87,14 @@
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Location = new Point(888, 351); maskedTextBoxNumber.Location = new Point(812, 412);
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.Size = new Size(125, 27);
maskedTextBoxNumber.TabIndex = 4; maskedTextBoxNumber.TabIndex = 4;
// //
// ButtonAddObject // ButtonAddObject
// //
ButtonAddObject.Location = new Point(856, 55); ButtonAddObject.Location = new Point(790, 133);
ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(170, 29); ButtonAddObject.Size = new Size(170, 29);
ButtonAddObject.TabIndex = 5; ButtonAddObject.TabIndex = 5;
@ -97,16 +104,17 @@
// //
// textBoxStorageName // textBoxStorageName
// //
textBoxStorageName.Location = new Point(892, 22); textBoxStorageName.Location = new Point(808, 69);
textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(125, 27); textBoxStorageName.Size = new Size(125, 27);
textBoxStorageName.TabIndex = 6; textBoxStorageName.TabIndex = 6;
//textBoxStorageName.TextChanged += textBoxStorageName_TextChanged;
// //
// listBoxStorages // listBoxStorages
// //
listBoxStorages.FormattingEnabled = true; listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20; listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(876, 90); listBoxStorages.Location = new Point(808, 168);
listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(150, 104); listBoxStorages.Size = new Size(150, 104);
listBoxStorages.TabIndex = 7; listBoxStorages.TabIndex = 7;
@ -114,7 +122,7 @@
// //
// ButtonDelObject // ButtonDelObject
// //
ButtonDelObject.Location = new Point(888, 210); ButtonDelObject.Location = new Point(808, 293);
ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(138, 29); ButtonDelObject.Size = new Size(138, 29);
ButtonDelObject.TabIndex = 8; ButtonDelObject.TabIndex = 8;
@ -122,11 +130,47 @@
ButtonDelObject.UseVisualStyleBackColor = true; ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click; ButtonDelObject.Click += ButtonDelObject_Click;
// //
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
//
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1027, 28);
menuStrip1.TabIndex = 12;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(58, 24);
файлToolStripMenuItem.Text = "файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(175, 26);
SaveToolStripMenuItem.Text = "сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripItem
//
LoadToolStripItem.Name = "LoadToolStripItem";
LoadToolStripItem.Size = new Size(175, 26);
LoadToolStripItem.Text = "загрузка";
LoadToolStripItem.Click += LoadToolStripItem_Click;
//
// FormLainerCollection // FormLainerCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1038, 490); ClientSize = new Size(1027, 551);
Controls.Add(menuStrip1);
Controls.Add(ButtonDelObject); Controls.Add(ButtonDelObject);
Controls.Add(listBoxStorages); Controls.Add(listBoxStorages);
Controls.Add(textBoxStorageName); Controls.Add(textBoxStorageName);
@ -136,9 +180,12 @@
Controls.Add(ButtonRemove); Controls.Add(ButtonRemove);
Controls.Add(ButtonAddLainer); Controls.Add(ButtonAddLainer);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
MainMenuStrip = menuStrip1;
Name = "FormLainerCollection"; Name = "FormLainerCollection";
Text = "FormLainerCollection"; Text = "FormLainerCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
@ -154,5 +201,11 @@
private TextBox textBoxStorageName; private TextBox textBoxStorageName;
private ListBox listBoxStorages; private ListBox listBoxStorages;
private Button ButtonDelObject; private Button ButtonDelObject;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripItem;
} }
} }

View File

@ -137,5 +137,39 @@ namespace ProjectLainer
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
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 LoadToolStripItem_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

@ -2,14 +2,15 @@
using ProjectLainer.MovementStrategy; using ProjectLainer.MovementStrategy;
namespace ProjectLainer.Generics namespace ProjectLainer.Generics
{ {
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class LainersGenericStorage internal class LainersGenericStorage
{ {
readonly Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages; readonly Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages;
public List<string> Keys => _lainerStorages.Keys.ToList(); public List<string> Keys => _lainerStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
public LainersGenericStorage(int pictureWidth, int pictureHeight) public LainersGenericStorage(int pictureWidth, int pictureHeight)
{ {
@ -17,20 +18,22 @@ namespace ProjectLainer.Generics
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
public void AddSet(string name) public void AddSet(string name)
{
_lainerStorages.Add(name, new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{ {
if (!_lainerStorages.ContainsKey(name)) if (!_lainerStorages.ContainsKey(name))
{ {
_lainerStorages.Add(name, new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight)); return;
}
}
public void DelSet(string name)
{
if (_lainerStorages.ContainsKey(name))
{
_lainerStorages.Remove(name);
} }
_lainerStorages.Remove(name);
} }
public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind] public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind]
{ {
get get
@ -42,6 +45,82 @@ namespace ProjectLainer.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, LainersGenericCollection<DrawingLainer, DrawningObjectLainer>> record in _lainerStorages)
{
StringBuilder records = new();
foreach (DrawingLainer? elem in record.Value.GetLainers)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter sw = new(filename))
{
sw.WriteLine($"LainerStorage{Environment.NewLine}{data}");
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("LainerStorage"))
{
return false;
}
_lainerStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 1)
{
str = sr.ReadLine();
continue;
}
LainersGenericCollection<DrawingLainer, DrawningObjectLainer> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingLainer? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight);
if (lainer != null)
{
if (!(collection + lainer))
{
return false;
}
}
}
_lainerStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
} }
} }