PIbd-22. Safiulova K.N. LabWork_06 #8
@ -38,6 +38,10 @@ namespace Catamaran.Generics
|
||||
/// </summary>
|
||||
private readonly SetGeneric<T> _collection;
|
||||
/// <summary>
|
||||
/// Получение объектов коллекции
|
||||
/// </summary>
|
||||
public IEnumerable<T?> GetCatamarans => _collection.GetCatamarans();
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="picWidth"></param>
|
||||
|
@ -1,5 +1,7 @@
|
||||
using Catamaran.DrawningObjects;
|
||||
using Catamaran.MovementStrategy;
|
||||
using System.Text;
|
||||
|
||||
namespace Catamaran.Generics
|
||||
{
|
||||
/// <summary>
|
||||
@ -25,6 +27,18 @@ namespace Catamaran.Generics
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Разделитель для записи ключа и значения элемента словаря
|
||||
/// </summary>
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char _separatorRecords = ';';
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
@ -73,5 +87,89 @@ namespace Catamaran.Generics
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Сохранение информации по катамаранам в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
StringBuilder data = new();
|
||||
foreach (KeyValuePair<string,CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>> record in _catamaranStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningCatamaran? elem in record.Value.GetCatamarans)
|
||||
{
|
||||
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($"CatamaranStorage{Environment.NewLine}{data}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using (StreamReader fs = File.OpenText(filename))
|
||||
{
|
||||
string str = fs.ReadLine();
|
||||
if (str == null || str.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!str.StartsWith("CatamaranStorage"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_catamaranStorages.Clear();
|
||||
string strs = "";
|
||||
|
||||
while ((strs = fs.ReadLine()) != null)
|
||||
{
|
||||
if (strs == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawningCatamaran? catamaran = elem?.CreateDrawningCatamaran(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (catamaran != null)
|
||||
{
|
||||
if (!(collection + catamaran))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_catamaranStorages.Add(record[0], collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -33,7 +33,8 @@ namespace Catamaran.Entities
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityCatamaran(int speed, double weight, Color bodyColor)
|
||||
|
||||
public EntityCatamaran(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
|
66
base/Catamaran/Catamaran/ExtentionDrawningCatamaran.cs
Normal file
66
base/Catamaran/Catamaran/ExtentionDrawningCatamaran.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Catamaran.Entities;
|
||||
namespace Catamaran.DrawningObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Расширение для класса EntityCatamaran
|
||||
/// </summary>
|
||||
public static class ExtentionDrawningCatamaran
|
||||
{
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info">Строка с данными для создания объекта</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <param name="width">Ширина</param>
|
||||
/// <param name="height">Высота</param>
|
||||
/// <returns>Объект</returns>
|
||||
public static DrawningCatamaran? CreateDrawningCatamaran(this string info, char
|
||||
separatorForObject, int width, int height)
|
||||
{
|
||||
string[] strs = info.Split(separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawningCatamaran(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningSailCatamaran(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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningCatamaran">Сохраняемый объект</param>
|
||||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||||
/// <returns>Строка с данными по объекту</returns>
|
||||
public static string GetDataForSave(this DrawningCatamaran drawningCatamaran,
|
||||
char separatorForObject)
|
||||
{
|
||||
var catamaran = drawningCatamaran.EntityCatamaran;
|
||||
if (catamaran == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var str = $"{catamaran.Speed}{separatorForObject}{catamaran.Weight}{separatorForObject}{catamaran.BodyColor.Name}";
|
||||
if (catamaran is not EntitySailCatamaran sailCatamaran)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return
|
||||
$"{str}{separatorForObject}{sailCatamaran.AdditionalColor.Name}{separatorForObject}{sailCatamaran.Sail}{separatorForObject}{sailCatamaran.FloatDetail}";
|
||||
}
|
||||
}
|
||||
}
|
@ -39,9 +39,16 @@
|
||||
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
|
||||
this.ButtonRemoveCatamaran = new System.Windows.Forms.Button();
|
||||
this.ButtonAddCatamaran = new System.Windows.Forms.Button();
|
||||
this.menuStrip = new System.Windows.Forms.MenuStrip();
|
||||
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
|
||||
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxSets.SuspendLayout();
|
||||
this.menuStrip.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// pictureBoxCollection
|
||||
@ -59,6 +66,7 @@
|
||||
this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection);
|
||||
this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran);
|
||||
this.groupBoxTools.Controls.Add(this.ButtonAddCatamaran);
|
||||
this.groupBoxTools.Controls.Add(this.menuStrip);
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(739, 1);
|
||||
this.groupBoxTools.Name = "groupBoxTools";
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(147, 460);
|
||||
@ -72,23 +80,23 @@
|
||||
this.groupBoxSets.Controls.Add(this.buttonDelObject);
|
||||
this.groupBoxSets.Controls.Add(this.listBoxStorages);
|
||||
this.groupBoxSets.Controls.Add(this.buttonAddObject);
|
||||
this.groupBoxSets.Location = new System.Drawing.Point(3, 21);
|
||||
this.groupBoxSets.Location = new System.Drawing.Point(3, 63);
|
||||
this.groupBoxSets.Name = "groupBoxSets";
|
||||
this.groupBoxSets.Size = new System.Drawing.Size(138, 245);
|
||||
this.groupBoxSets.Size = new System.Drawing.Size(138, 203);
|
||||
this.groupBoxSets.TabIndex = 4;
|
||||
this.groupBoxSets.TabStop = false;
|
||||
this.groupBoxSets.Text = "Наборы";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
this.textBoxStorageName.Location = new System.Drawing.Point(3, 36);
|
||||
this.textBoxStorageName.Location = new System.Drawing.Point(6, 22);
|
||||
this.textBoxStorageName.Name = "textBoxStorageName";
|
||||
this.textBoxStorageName.Size = new System.Drawing.Size(134, 23);
|
||||
this.textBoxStorageName.Size = new System.Drawing.Size(130, 23);
|
||||
this.textBoxStorageName.TabIndex = 4;
|
||||
//
|
||||
// buttonDelObject
|
||||
//
|
||||
this.buttonDelObject.Location = new System.Drawing.Point(3, 209);
|
||||
this.buttonDelObject.Location = new System.Drawing.Point(3, 167);
|
||||
this.buttonDelObject.Name = "buttonDelObject";
|
||||
this.buttonDelObject.Size = new System.Drawing.Size(134, 27);
|
||||
this.buttonDelObject.TabIndex = 3;
|
||||
@ -100,17 +108,17 @@
|
||||
//
|
||||
this.listBoxStorages.FormattingEnabled = true;
|
||||
this.listBoxStorages.ItemHeight = 15;
|
||||
this.listBoxStorages.Location = new System.Drawing.Point(3, 102);
|
||||
this.listBoxStorages.Location = new System.Drawing.Point(6, 82);
|
||||
this.listBoxStorages.Name = "listBoxStorages";
|
||||
this.listBoxStorages.Size = new System.Drawing.Size(134, 94);
|
||||
this.listBoxStorages.Size = new System.Drawing.Size(132, 79);
|
||||
this.listBoxStorages.TabIndex = 2;
|
||||
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
|
||||
//
|
||||
// buttonAddObject
|
||||
//
|
||||
this.buttonAddObject.Location = new System.Drawing.Point(3, 65);
|
||||
this.buttonAddObject.Location = new System.Drawing.Point(6, 51);
|
||||
this.buttonAddObject.Name = "buttonAddObject";
|
||||
this.buttonAddObject.Size = new System.Drawing.Size(134, 25);
|
||||
this.buttonAddObject.Size = new System.Drawing.Size(131, 25);
|
||||
this.buttonAddObject.TabIndex = 1;
|
||||
this.buttonAddObject.Text = "Добавить набор";
|
||||
this.buttonAddObject.UseVisualStyleBackColor = true;
|
||||
@ -153,13 +161,51 @@
|
||||
this.ButtonAddCatamaran.UseVisualStyleBackColor = true;
|
||||
this.ButtonAddCatamaran.Click += new System.EventHandler(this.ButtonAddCatamaran_Click);
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.ToolStripMenuItem});
|
||||
this.menuStrip.Location = new System.Drawing.Point(3, 19);
|
||||
this.menuStrip.Name = "menuStrip";
|
||||
this.menuStrip.Size = new System.Drawing.Size(141, 24);
|
||||
this.menuStrip.TabIndex = 5;
|
||||
this.menuStrip.Text = "menuStrip";
|
||||
//
|
||||
// ToolStripMenuItem
|
||||
//
|
||||
this.ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.SaveToolStripMenuItem,
|
||||
this.LoadToolStripMenuItem});
|
||||
this.ToolStripMenuItem.Name = "ToolStripMenuItem";
|
||||
this.ToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
|
||||
this.ToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.SaveToolStripMenuItem.Text = "Сохранение";
|
||||
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.LoadToolStripMenuItem.Text = "Загрузка";
|
||||
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
this.openFileDialog.FileName = "openFileDialog1";
|
||||
//
|
||||
// FormCatamaranCollection
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(884, 461);
|
||||
this.ClientSize = new System.Drawing.Size(885, 461);
|
||||
this.Controls.Add(this.groupBoxTools);
|
||||
this.Controls.Add(this.pictureBoxCollection);
|
||||
this.MainMenuStrip = this.menuStrip;
|
||||
this.Name = "FormCatamaranCollection";
|
||||
this.Text = "Набор катамаранов";
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
|
||||
@ -167,6 +213,8 @@
|
||||
this.groupBoxTools.PerformLayout();
|
||||
this.groupBoxSets.ResumeLayout(false);
|
||||
this.groupBoxSets.PerformLayout();
|
||||
this.menuStrip.ResumeLayout(false);
|
||||
this.menuStrip.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
@ -184,5 +232,11 @@
|
||||
private Button buttonDelObject;
|
||||
private ListBox listBoxStorages;
|
||||
private TextBox textBoxStorageName;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem ToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
private OpenFileDialog openFileDialog;
|
||||
}
|
||||
}
|
@ -192,7 +192,49 @@ namespace Catamaran
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowCatamarans();
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Сохранение"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Загрузка"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,4 +57,16 @@
|
||||
<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>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>125, 17</value>
|
||||
</metadata>
|
||||
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>254, 17</value>
|
||||
</metadata>
|
||||
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>96</value>
|
||||
</metadata>
|
||||
</root>
|
@ -145,7 +145,7 @@
|
||||
//
|
||||
// panelYellow
|
||||
//
|
||||
this.panelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
|
||||
this.panelYellow.BackColor = System.Drawing.Color.Gold;
|
||||
this.panelYellow.Location = new System.Drawing.Point(187, 29);
|
||||
this.panelYellow.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
|
||||
this.panelYellow.Name = "panelYellow";
|
||||
|
Loading…
Reference in New Issue
Block a user