Pibd22_NikiforovaMV_lab6

This commit is contained in:
shoot 2023-12-16 11:18:38 +04:00
parent 7a90a02332
commit 515d6907cc
7 changed files with 224 additions and 3 deletions

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.Entities;
namespace ContainerShip.DrawningObjects
{
public static class ExtentionDrawningShip
{
public static DrawningShip? CreateDrawningShip(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningShip(
Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawingContainerShip(
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 DrawningShip drawningShip, char separatorForObject)
{
var ship = drawningShip.EntityShip;
if (ship == null)
{
return string.Empty;
}
var str = $"{ship.Speed}{separatorForObject}{ship.Weight}{separatorForObject}{ship.BodyColor.Name}";
if (ship is not EntityContainerShip containerShip)
{
return str;
}
return $"{str}{separatorForObject}{containerShip.AdditionalColor.Name}{separatorForObject}{containerShip.Load}{separatorForObject}{containerShip.Crane}";
}
}
}

View File

@ -39,7 +39,14 @@
buttonAddStorage = new Button(); buttonAddStorage = new Button();
buttonDeleteStorage = new Button(); buttonDeleteStorage = new Button();
buttonUpdate = new Button(); buttonUpdate = new Button();
menuStrip = new MenuStrip();
toolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// buttonAdd // buttonAdd
@ -83,10 +90,10 @@
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Location = new Point(0, 28);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(731, 609); pictureBoxCollection.Size = new Size(731, 581);
pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabIndex = 5;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
@ -150,11 +157,52 @@
buttonUpdate.UseVisualStyleBackColor = true; buttonUpdate.UseVisualStyleBackColor = true;
buttonUpdate.Click += buttonUpdate_Click; buttonUpdate.Click += buttonUpdate_Click;
// //
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(914, 28);
menuStrip.TabIndex = 13;
menuStrip.Text = "menuStrip1";
//
// toolStripMenuItem
//
toolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
toolStripMenuItem.Name = "toolStripMenuItem";
toolStripMenuItem.Size = new Size(59, 24);
toolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// FormShipCollection // FormShipCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(914, 600); ClientSize = new Size(914, 600);
Controls.Add(menuStrip);
Controls.Add(buttonDeleteStorage); Controls.Add(buttonDeleteStorage);
Controls.Add(buttonAddStorage); Controls.Add(buttonAddStorage);
Controls.Add(listBoxStorages); Controls.Add(listBoxStorages);
@ -170,6 +218,8 @@
Name = "FormShipCollection"; Name = "FormShipCollection";
Text = "Набор кораблей"; Text = "Набор кораблей";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
@ -187,5 +237,11 @@
private Button buttonAddStorage; private Button buttonAddStorage;
private Button buttonDeleteStorage; private Button buttonDeleteStorage;
private Button buttonUpdate; private Button buttonUpdate;
private MenuStrip menuStrip;
private ToolStripMenuItem toolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
} }
} }

View File

@ -66,6 +66,7 @@ namespace ContainerShip
ReloadObjects(); ReloadObjects();
} }
} }
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
var formShipConfig = new FormShipConfig(); var formShipConfig = new FormShipConfig();
@ -97,6 +98,7 @@ namespace ContainerShip
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
private void buttonDelete_Click(object sender, EventArgs e) private void buttonDelete_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1) if (listBoxStorages.SelectedIndex == -1)
@ -140,5 +142,36 @@ namespace ContainerShip
} }
pictureBoxCollection.Image = obj.ShowShips(); pictureBoxCollection.Image = obj.ShowShips();
} }
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();
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
pictureBoxCollection.Image = obj.ShowShips();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
} }
} }

View File

@ -117,4 +117,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>44, 16</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>185, 16</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>344, 16</value>
</metadata>
</root> </root>

View File

@ -61,7 +61,7 @@ namespace ContainerShip.Generic
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j) 1; ++j)
{ {//линия разметки места
g.DrawLine(pen, i * _placeSizeWidth, j * g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight); _placeSizeHeight);
@ -72,11 +72,13 @@ namespace ContainerShip.Generic
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
// координаты
int x = _pictureWidth / _placeSizeWidth - 1; int x = _pictureWidth / _placeSizeWidth - 1;
int y = _pictureHeight / _placeSizeHeight - 1; int y = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
{ {
// TODO получение объекта
DrawningShip _ship = _collection[i]; DrawningShip _ship = _collection[i];
if (_ship != null) { if (_ship != null) {
if (x < 0) if (x < 0)
@ -90,6 +92,8 @@ namespace ContainerShip.Generic
} }
} }
} }
public IEnumerable<T?> GetShip => _collection.GetShip();
} }
} }

View File

@ -10,9 +10,13 @@ namespace ContainerShip.Generic
{ {
internal class ShipsGenericStorage internal class ShipsGenericStorage
{ {
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, ShipsGenericCollection<DrawningShip, DrawingObjectShip>> _shipStorages; readonly Dictionary<string, ShipsGenericCollection<DrawningShip, DrawingObjectShip>> _shipStorages;
public List<string> Keys => _shipStorages.Keys.ToList(); public List<string> Keys => _shipStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
public ShipsGenericStorage(int pictureWidth, int pictureHeight) public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{ {
@ -33,6 +37,7 @@ namespace ContainerShip.Generic
} }
_shipStorages.Add(name, new ShipsGenericCollection<DrawningShip, DrawingObjectShip>(_pictureWidth, _pictureHeight)); _shipStorages.Add(name, new ShipsGenericCollection<DrawningShip, DrawingObjectShip>(_pictureWidth, _pictureHeight));
} }
public void DelSet(string name) public void DelSet(string name)
{ {
if (_shipStorages.ContainsKey(name)) if (_shipStorages.ContainsKey(name))
@ -41,6 +46,70 @@ namespace ContainerShip.Generic
} }
} }
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = File.CreateText(filename))
{
sw.WriteLine($"ShipStorage");
foreach (var record in _shipStorages)
{
StringBuilder records = new();
foreach (DrawningShip? elem in record.Value.GetShip)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = File.OpenText(filename))
{
string? curLine = sr.ReadLine();
if (curLine == null || !curLine.Contains("ShipStorage"))
{
return false;
}
_shipStorages.Clear();
curLine = sr.ReadLine();
while (curLine != null)
{
string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
ShipsGenericCollection<DrawningShip, DrawingObjectShip> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null)
{
if (collection + ship == -1)
{
return false;
}
}
}
_shipStorages.Add(record[0], collection);
curLine = sr.ReadLine();
}
}
return true;
}
public ShipsGenericCollection<DrawningShip, DrawingObjectShip>? this[string ind] public ShipsGenericCollection<DrawningShip, DrawingObjectShip>? this[string ind]
{ {
get get

View File

@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace ContainerShip.MovementStrategy namespace ContainerShip.MovementStrategy
{ {
/// <summary>
/// Статус выполнения операции перемещения
/// </summary>
public enum Status public enum Status
{ {
NotInit, NotInit,