From 515d6907cc7c7f6743d633e3d76cae6e5e34e968 Mon Sep 17 00:00:00 2001 From: shoot Date: Sat, 16 Dec 2023 11:18:38 +0400 Subject: [PATCH] Pibd22_NikiforovaMV_lab6 --- ContainerShip/ExtentionDrawningShip.cs | 47 +++++++++++++ ContainerShip/FormShipCollection.Designer.cs | 60 ++++++++++++++++- ContainerShip/FormShipCollection.cs | 33 ++++++++++ ContainerShip/FormShipCollection.resx | 9 +++ ContainerShip/ShipsGenericCollection.cs | 6 +- ContainerShip/ShipsGenericStorage.cs | 69 ++++++++++++++++++++ ContainerShip/Status.cs | 3 + 7 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 ContainerShip/ExtentionDrawningShip.cs diff --git a/ContainerShip/ExtentionDrawningShip.cs b/ContainerShip/ExtentionDrawningShip.cs new file mode 100644 index 0000000..86f8195 --- /dev/null +++ b/ContainerShip/ExtentionDrawningShip.cs @@ -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}"; + } + } +} diff --git a/ContainerShip/FormShipCollection.Designer.cs b/ContainerShip/FormShipCollection.Designer.cs index eb6698f..a03add4 100644 --- a/ContainerShip/FormShipCollection.Designer.cs +++ b/ContainerShip/FormShipCollection.Designer.cs @@ -39,7 +39,14 @@ buttonAddStorage = new Button(); buttonDeleteStorage = 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(); + menuStrip.SuspendLayout(); SuspendLayout(); // // buttonAdd @@ -83,10 +90,10 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Location = new Point(0, 28); pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(731, 609); + pictureBoxCollection.Size = new Size(731, 581); pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabStop = false; // @@ -150,11 +157,52 @@ buttonUpdate.UseVisualStyleBackColor = true; 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 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(914, 600); + Controls.Add(menuStrip); Controls.Add(buttonDeleteStorage); Controls.Add(buttonAddStorage); Controls.Add(listBoxStorages); @@ -170,6 +218,8 @@ Name = "FormShipCollection"; Text = "Набор кораблей"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -187,5 +237,11 @@ private Button buttonAddStorage; private Button buttonDeleteStorage; private Button buttonUpdate; + private MenuStrip menuStrip; + private ToolStripMenuItem toolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/ContainerShip/FormShipCollection.cs b/ContainerShip/FormShipCollection.cs index b8b3af1..eaf7727 100644 --- a/ContainerShip/FormShipCollection.cs +++ b/ContainerShip/FormShipCollection.cs @@ -66,6 +66,7 @@ namespace ContainerShip ReloadObjects(); } } + private void buttonAdd_Click(object sender, EventArgs e) { var formShipConfig = new FormShipConfig(); @@ -97,6 +98,7 @@ namespace ContainerShip MessageBox.Show("Не удалось добавить объект"); } } + private void buttonDelete_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -140,5 +142,36 @@ namespace ContainerShip } 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); + } + } + } } } diff --git a/ContainerShip/FormShipCollection.resx b/ContainerShip/FormShipCollection.resx index a395bff..82b990f 100644 --- a/ContainerShip/FormShipCollection.resx +++ b/ContainerShip/FormShipCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 44, 16 + + + 185, 16 + + + 344, 16 + \ No newline at end of file diff --git a/ContainerShip/ShipsGenericCollection.cs b/ContainerShip/ShipsGenericCollection.cs index 910696e..9070132 100644 --- a/ContainerShip/ShipsGenericCollection.cs +++ b/ContainerShip/ShipsGenericCollection.cs @@ -61,7 +61,7 @@ namespace ContainerShip.Generic { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - { + {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); @@ -72,11 +72,13 @@ namespace ContainerShip.Generic } private void DrawObjects(Graphics g) { + // координаты int x = _pictureWidth / _placeSizeWidth - 1; int y = _pictureHeight / _placeSizeHeight - 1; for (int i = 0; i < _collection.Count; i++) { + // TODO получение объекта DrawningShip _ship = _collection[i]; if (_ship != null) { if (x < 0) @@ -90,6 +92,8 @@ namespace ContainerShip.Generic } } } + + public IEnumerable GetShip => _collection.GetShip(); } } diff --git a/ContainerShip/ShipsGenericStorage.cs b/ContainerShip/ShipsGenericStorage.cs index 12b28a2..8fb99b5 100644 --- a/ContainerShip/ShipsGenericStorage.cs +++ b/ContainerShip/ShipsGenericStorage.cs @@ -10,9 +10,13 @@ namespace ContainerShip.Generic { internal class ShipsGenericStorage { + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; readonly Dictionary> _shipStorages; public List Keys => _shipStorages.Keys.ToList(); private readonly int _pictureWidth; + private readonly int _pictureHeight; public ShipsGenericStorage(int pictureWidth, int pictureHeight) { @@ -33,6 +37,7 @@ namespace ContainerShip.Generic } _shipStorages.Add(name, new ShipsGenericCollection(_pictureWidth, _pictureHeight)); } + public void DelSet(string 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 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? this[string ind] { get diff --git a/ContainerShip/Status.cs b/ContainerShip/Status.cs index de4c75b..e5f8210 100644 --- a/ContainerShip/Status.cs +++ b/ContainerShip/Status.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace ContainerShip.MovementStrategy { + /// + /// Статус выполнения операции перемещения + /// public enum Status { NotInit,