6 лабораторная

This commit is contained in:
sardq 2023-12-02 23:16:34 +04:00
parent 0db70e3020
commit 5ad06ae239
6 changed files with 299 additions and 53 deletions

View File

@ -18,6 +18,7 @@ namespace HoistingCrane.Generics
private readonly int _placeSizeHeight = 150;
/// Набор объектов
private readonly SetGeneric<T> _collection;
public IEnumerable<T?> GetCranes => _collection.GetCranes();
/// Конструктор
public CranesGenericCollection(int picWidth, int picHeight)
{

View File

@ -1,6 +1,7 @@
using HoistingCrane.DrawningObjects;
using HoistingCrane.Generics;
using HoistingCrane.MovementStrategy;
using System.Text;
namespace HoistingCrane
{
@ -14,6 +15,94 @@ namespace HoistingCrane
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
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,
CranesGenericCollection<DrawingCrane, DrawningObjectsCrane>> record in _craneStorages)
{
StringBuilder records = new();
foreach (DrawingCrane? elem in record.Value.GetCranes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter sw = new StreamWriter(filename))
{
sw.WriteLine("CarStorage");
sw.Write(data.ToString());
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader reader = new StreamReader(filename))
{
string cheker = reader.ReadLine();
if (cheker == null)
{
return false;
}
if (!cheker.StartsWith("CarStorage"))
{
return false;
}
_craneStorages.Clear();
string strs;
bool firstinit = true;
while ((strs = reader.ReadLine()) != null)
{
if (strs == null && firstinit)
{
return false;
}
if (strs == null)
{
return false;
}
firstinit = false;
string name = strs.Split(_separatorForKeyValue)[0];
CranesGenericCollection<DrawingCrane, DrawningObjectsCrane> collection = new(_pictureWidth, _pictureHeight);
foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords))
{
DrawingCrane? crane =
data?.CreateDrawningCrane(_separatorForObject, _pictureWidth, _pictureHeight);
if (crane != null)
{
int? result = collection + crane;
if (result == null || result.Value == -1)
{
return false;
}
}
}
_craneStorages.Add(name, collection);
}
return true;
}
}
/// Конструктор
public CranesGenericStorage(int pictureWidth, int pictureHeight)
{

View File

@ -0,0 +1,50 @@
using HoistingCrane.Entities;
namespace HoistingCrane.DrawningObjects
{
public static class ExtentionDrawningCrane
{
public static DrawingCrane? CreateDrawningCrane(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawingCrane(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new AdditionalDrawingCrane(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="drawningCar">Сохраняемый объект</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <returns>Строка с данными по объекту</returns>
public static string GetDataForSave(this DrawingCrane drawningCrane,
char separatorForObject)
{
var car = drawningCrane.EntityCrane;
if (car == null)
{
return string.Empty;
}
var str = $"{car.Speed}{separatorForObject}{car.Weight}{separatorForObject}{car.BodyColor.Name}";
if (car is not AdditionEntityCrane hoistingCrane)
{
return str;
}
return $"{str}{separatorForObject}{hoistingCrane.AdditionalColor.Name}{separatorForObject}{hoistingCrane.Crane}{separatorForObject}{hoistingCrane.CounterWeight}";
}
}
}

View File

@ -29,19 +29,26 @@
private void InitializeComponent()
{
this.groupBoxInstruments = new System.Windows.Forms.GroupBox();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonReload = new System.Windows.Forms.Button();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxSet = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonAddSet = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonRemoveSet = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonReload = new System.Windows.Forms.Button();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBoxInstruments.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.groupBoxSet.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// groupBoxInstruments
@ -58,52 +65,6 @@
this.groupBoxInstruments.TabStop = false;
this.groupBoxInstruments.Text = "Инструменты";
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31);
this.maskedTextBoxNumber.TabIndex = 9;
//
// buttonReload
//
this.buttonReload.Location = new System.Drawing.Point(12, 397);
this.buttonReload.Name = "buttonReload";
this.buttonReload.Size = new System.Drawing.Size(167, 35);
this.buttonReload.TabIndex = 8;
this.buttonReload.Text = "Обновить экран";
this.buttonReload.UseVisualStyleBackColor = true;
this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(12, 356);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(167, 35);
this.buttonDelete.TabIndex = 7;
this.buttonDelete.Text = "Удалить кран";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click);
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(12, 278);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(167, 35);
this.buttonAdd.TabIndex = 6;
this.buttonAdd.Text = "Добавить кран";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(996, 434);
this.pictureBoxCollection.TabIndex = 5;
this.pictureBoxCollection.TabStop = false;
//
// groupBoxSet
//
this.groupBoxSet.Controls.Add(this.textBoxStorageName);
@ -154,6 +115,98 @@
this.buttonRemoveSet.UseVisualStyleBackColor = true;
this.buttonRemoveSet.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 319);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31);
this.maskedTextBoxNumber.TabIndex = 9;
//
// buttonReload
//
this.buttonReload.Location = new System.Drawing.Point(12, 397);
this.buttonReload.Name = "buttonReload";
this.buttonReload.Size = new System.Drawing.Size(167, 35);
this.buttonReload.TabIndex = 8;
this.buttonReload.Text = "Обновить экран";
this.buttonReload.UseVisualStyleBackColor = true;
this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(12, 356);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(167, 35);
this.buttonDelete.TabIndex = 7;
this.buttonDelete.Text = "Удалить кран";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click);
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(12, 278);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(167, 35);
this.buttonAdd.TabIndex = 6;
this.buttonAdd.Text = "Добавить кран";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(996, 401);
this.pictureBoxCollection.TabIndex = 5;
this.pictureBoxCollection.TabStop = false;
//
// menuStrip1
//
this.menuStrip1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.menuStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Visible;
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 401);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
this.menuStrip1.Size = new System.Drawing.Size(996, 33);
this.menuStrip1.TabIndex = 12;
this.menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItemLoad,
this.ToolStripMenuItemSave});
this.файлToolStripMenuItem.Name = айлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(68, 29);
this.файлToolStripMenuItem.Text = "файл";
//
// ToolStripMenuItemLoad
//
this.ToolStripMenuItemLoad.Name = "ToolStripMenuItemLoad";
this.ToolStripMenuItemLoad.Size = new System.Drawing.Size(197, 34);
this.ToolStripMenuItemLoad.Text = "загрузить";
this.ToolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// ToolStripMenuItemSave
//
this.ToolStripMenuItemSave.Name = "ToolStripMenuItemSave";
this.ToolStripMenuItemSave.Size = new System.Drawing.Size(197, 34);
this.ToolStripMenuItemSave.Text = "сохранить";
this.ToolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog1";
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormCraneCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
@ -161,14 +214,18 @@
this.ClientSize = new System.Drawing.Size(996, 434);
this.Controls.Add(this.groupBoxInstruments);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.menuStrip1);
this.Name = "FormCraneCollection";
this.Text = "Набор кранов";
this.groupBoxInstruments.ResumeLayout(false);
this.groupBoxInstruments.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.groupBoxSet.ResumeLayout(false);
this.groupBoxSet.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -185,5 +242,11 @@
private Button buttonAddSet;
private ListBox listBoxStorages;
private Button buttonRemoveSet;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem ToolStripMenuItemLoad;
private ToolStripMenuItem ToolStripMenuItemSave;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -1,6 +1,7 @@
using HoistingCrane.DrawningObjects;
using HoistingCrane.Generics;
using HoistingCrane.MovementStrategy;
using System.Windows.Forms;
namespace HoistingCrane
{
@ -29,6 +30,39 @@ namespace HoistingCrane
listBoxStorages.SelectedIndex = index;
}
}
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);
ReloadObjects();
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))

View File

@ -57,4 +57,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>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>173, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>367, 17</value>
</metadata>
</root>