PIbd-22. Stroev V.M. Lab Work 06 #8

Closed
StroevVladimir wants to merge 2 commits from Lab06 into Lab05
6 changed files with 245 additions and 3 deletions
Showing only changes of commit 215d2447dd - Show all commits

View File

@ -0,0 +1,52 @@
using Monorail.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.DrawningObjects
{
public static class ExtentionDrawningMonorail
{
public static DrawningMonorail? CreateDrawningMonorail(this string info,
char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if(strs.Length == 3)
{
return new DrawningMonorail(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawningSecondMonorail(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 DrawningMonorail drawningMonorail,
char separatorForObject)
{
var monorail = drawningMonorail.EntityMonorail;
if (monorail == null)
{
return string.Empty;
}
var str =
$"{monorail.Speed}{separatorForObject}{monorail.Weight}{separatorForObject}{monorail.BodyColor.Name}";
if (monorail is not EntitySecondMonorail secondMonorail)
{
return str;
}
return
$"{str}{separatorForObject}{secondMonorail.AdditionalColor.Name}{separatorForObject}" +
$"{secondMonorail.Monorails}{separatorForObject}{secondMonorail.SecondCabin}{separatorForObject}";
}
}
}

View File

@ -41,16 +41,23 @@
buttonDelMonorail = new Button();
buttonAddMonorail = new Button();
label1 = new Label();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
сохранитьToolStripMenuItem = new ToolStripMenuItem();
загрузитьToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panel1.SuspendLayout();
panel2.SuspendLayout();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(5, -1);
pictureBoxCollection.Location = new Point(5, 31);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(1017, 517);
pictureBoxCollection.Size = new Size(1017, 485);
pictureBoxCollection.TabIndex = 5;
pictureBoxCollection.TabStop = false;
//
@ -97,6 +104,7 @@
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(144, 104);
listBoxStorages.TabIndex = 9;
listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged;
//
// textBoxStorageName
//
@ -176,6 +184,47 @@
label1.TabIndex = 0;
label1.Text = "Инструменты";
//
// 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(1228, 28);
menuStrip1.TabIndex = 6;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Text = "Файл";
//
// сохранитьToolStripMenuItem
//
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
сохранитьToolStripMenuItem.Size = new Size(166, 26);
сохранитьToolStripMenuItem.Text = "Сохранить";
сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem
//
загрузитьToolStripMenuItem.Name = агрузитьToolStripMenuItem";
загрузитьToolStripMenuItem.Size = new Size(166, 26);
загрузитьToolStripMenuItem.Text = "Загрузить";
загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.FileName = "saveFileDialog";
saveFileDialog.Filter = "txt file | *.txt";
//
// FormMonorailCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -184,6 +233,8 @@
ClientSize = new Size(1228, 518);
Controls.Add(pictureBoxCollection);
Controls.Add(panel1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormMonorailCollection";
Text = "Набор Монорельсов";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -191,7 +242,10 @@
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -209,5 +263,11 @@
private ListBox listBoxStorages;
private TextBox textBoxStorageName;
private Button ButtonAddObject;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -168,5 +168,37 @@ EventArgs e)
}
pictureBoxCollection.Image = obj.ShowMonorails();
}
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);
}
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="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>330, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>176, 17</value>
</metadata>
</root>

View File

@ -12,6 +12,7 @@ namespace Monorail.Generics
where T: DrawningMonorail
where U : IMoveableObject
{
public IEnumerable<T?> GetMonorails => _collection.GetMonorails();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 295;

View File

@ -10,6 +10,10 @@ namespace Monorail.Generics
{
internal class MonorailsGenericStorage
{
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, MonorailsGenericCollection<DrawningMonorail,
DrawningObjectMonorail>> _monorailsStorages;
public List<string> Keys => _monorailsStorages.Keys.ToList();
@ -48,5 +52,89 @@ namespace Monorail.Generics
return _monorailsStorages[ind];
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>>
record in _monorailsStorages)
{
StringBuilder records = new();
foreach (DrawningMonorail? elem in record.Value.GetMonorails)
{
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($"MonorailStorage{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("MonorailStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_monorailsStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningMonorail? monorail =
elem?.CreateDrawningMonorail(_separatorForObject, _pictureWidth, _pictureHeight);
if (monorail != null)
{
if ((collection + monorail)==-1)
{
return false;
}
}
}
_monorailsStorages.Add(record[0], collection);
}
return true;
}
}
}