Lab6 WarmlyShip Barsukov #6

Closed
frog24 wants to merge 1 commits from Lab6 into Lab5
6 changed files with 232 additions and 3 deletions

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectWarmlyShip.DrawingObjects;
using ProjectWarmlyShip.Entities;
namespace ProjectWarmlyShip
{
public static class ExtentionDrawingShip
{
public static DrawingShip? CreateDrawingShip(this string info,
char separatorForObjects, int width, int height)
{
string[] strs = info.Split(separatorForObjects);
if (strs.Length == 3)
{
return new DrawingShip(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawingWarmlyShip(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 DrawingShip drawingShip, char separatorForObjects)
{
var ship = drawingShip.EntityShip;
if (ship == null)
{
return string.Empty;
}
var str =
$"{ship.Speed}{separatorForObjects}{ship.Weight}{separatorForObjects}{ship.MainColor.Name}";
if (ship is not EntityWarmlyShip warmlyShip)
{
return str;
}
return
$"{str}{separatorForObjects}{warmlyShip.OptionalColor.Name}{separatorForObjects}" +
$"{warmlyShip.Pipes}{separatorForObjects}{warmlyShip.FuelCompartment}";
}
}
}

View File

@ -39,14 +39,21 @@
buttonRefresh = new Button();
buttonDeleteShip = new Button();
buttonAddShip = new Button();
StripMenu = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
saveToolStripMenuItem = new ToolStripMenuItem();
loadToolStripMenuItem = new ToolStripMenuItem();
SaveFileDialog = new SaveFileDialog();
OpenFileDialog = new OpenFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
Tools.SuspendLayout();
Sets.SuspendLayout();
StripMenu.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Location = new Point(0, 25);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(700, 460);
pictureBoxCollection.TabIndex = 0;
@ -59,7 +66,7 @@
Tools.Controls.Add(buttonRefresh);
Tools.Controls.Add(buttonDeleteShip);
Tools.Controls.Add(buttonAddShip);
Tools.Location = new Point(700, 0);
Tools.Location = new Point(700, 25);
Tools.Name = "Tools";
Tools.Size = new Size(180, 460);
Tools.TabIndex = 1;
@ -153,13 +160,54 @@
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += ButtonAddShip_Click;
//
// StripMenu
//
StripMenu.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
StripMenu.Location = new Point(0, 0);
StripMenu.Name = "StripMenu";
StripMenu.Size = new Size(884, 24);
StripMenu.TabIndex = 2;
StripMenu.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
fileToolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.Size = new Size(180, 22);
saveToolStripMenuItem.Text = "Save";
saveToolStripMenuItem.Click += SaveToolStripMenu_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.Size = new Size(180, 22);
loadToolStripMenuItem.Text = "Load";
loadToolStripMenuItem.Click += LoadToolStripMenu_Click;
//
// SaveFileDialog
//
SaveFileDialog.Filter = "txt file | *.txt";
//
// OpenFileDialog
//
OpenFileDialog.FileName = "openFileDialog1";
OpenFileDialog.Filter = "txt file | *.txt";
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
ClientSize = new Size(884, 491);
Controls.Add(Tools);
Controls.Add(pictureBoxCollection);
Controls.Add(StripMenu);
MainMenuStrip = StripMenu;
Name = "FormShipCollection";
Text = "FormShipCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -167,7 +215,10 @@
Tools.PerformLayout();
Sets.ResumeLayout(false);
Sets.PerformLayout();
StripMenu.ResumeLayout(false);
StripMenu.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -183,5 +234,11 @@
private Button DeleteSetButton;
private Button AddSetButton;
private TextBox textBoxStorageName;
private MenuStrip StripMenu;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog SaveFileDialog;
private OpenFileDialog OpenFileDialog;
}
}

View File

@ -156,5 +156,38 @@ namespace ProjectWarmlyShip
}
pictureBoxCollection.Image = obj.ShowShips();
}
private void SaveToolStripMenu_Click(object sender, EventArgs e)
{
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(SaveFileDialog.FileName))
{
MessageBox.Show("Save Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Save Not Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(OpenFileDialog.FileName))
{
MessageBox.Show("Load Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadObjects();
}
else
{
MessageBox.Show("Load Not Complete", "Result",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

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="StripMenu.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>256, 17</value>
</metadata>
</root>

View File

@ -85,5 +85,6 @@ namespace ProjectWarmlyShip.Generics
i++;
}
}
public IEnumerable<T?> GetShips => _collection.GetShips();
}
}

View File

@ -15,6 +15,9 @@ namespace ProjectWarmlyShip.Generics
public List<string> Keys => _shipStorages.Keys.ToList();
private readonly int _pictuteWidth;
private readonly int _pictuteHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObjects = ':';
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string, ShipsGenericCollection<DrawingShip, DrawingObjectShip>>();
@ -47,5 +50,82 @@ namespace ProjectWarmlyShip.Generics
return null;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> record in _shipStorages)
{
StringBuilder records = new();
foreach (DrawingShip? elem in record.Value.GetShips)
{
records.Append($"{elem?.GetDataForSave(_separatorForObjects)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"ShipStorage{Environment.NewLine}{data}");
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (StreamReader reader = new StreamReader(filename))
Review

Требовалось реализовать построчное чтение с обработкой из файла

Требовалось реализовать построчное чтение с обработкой из файла
{
string str;
while ((str = reader.ReadLine()) != null)
{
bufferTextFromFile += str + '\r' + '\n';
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs.Length == 0 || strs == null)
{
return false;
}
if (!strs[0].StartsWith("ShipStorage"))
{
return false;
}
_shipStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
ShipsGenericCollection<DrawingShip, DrawingObjectShip> collection = new(_pictuteWidth, _pictuteHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingShip? ship = elem.CreateDrawingShip(_separatorForObjects, _pictuteWidth, _pictuteHeight);
if (ship != null)
{
if (!(collection + ship))
{
return false;
}
}
}
_shipStorages.Add(record[0], collection);
}
return true;
}
}
}