From 73b9099be3b1756d8be86d1a41bd7fd28ffb154f Mon Sep 17 00:00:00 2001 From: frog24 Date: Sat, 18 Nov 2023 03:34:52 +0400 Subject: [PATCH] Lab6 WarmlyShip Barsukov --- .../ProjectWarmlyShip/ExtentionDrawingShip.cs | 49 ++++++++++++ .../FormShipCollection.Designer.cs | 63 ++++++++++++++- .../ProjectWarmlyShip/FormShipCollection.cs | 33 ++++++++ .../ProjectWarmlyShip/FormShipCollection.resx | 9 +++ .../ShipsGenericCollection.cs | 1 + .../ProjectWarmlyShip/ShipsGenericStorage.cs | 80 +++++++++++++++++++ 6 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ExtentionDrawingShip.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionDrawingShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionDrawingShip.cs new file mode 100644 index 0000000..73f3216 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ExtentionDrawingShip.cs @@ -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}"; + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs index 5d47a03..5641a7b 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index 2a03159..2fd283e 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -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); + } + } + } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx index f298a7b..9bcb1ac 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 256, 17 + \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs index ed0fcdd..aafd3c9 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -85,5 +85,6 @@ namespace ProjectWarmlyShip.Generics i++; } } + public IEnumerable GetShips => _collection.GetShips(); } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs index 992d1cd..245c43f 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs @@ -15,6 +15,9 @@ namespace ProjectWarmlyShip.Generics public List 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>(); @@ -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> 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)) + { + 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 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; + } } } -- 2.25.1