From 85bb53b06279423af67e051e9b217232b3d07e4f Mon Sep 17 00:00:00 2001 From: Aidar Date: Sun, 5 May 2024 16:20:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=966?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../ICollectionGenericObjects.cs | 13 +- .../ListGenericObjects.cs | 25 ++- .../MassiveGenericObjects.cs | 17 +- .../StorageCollection.cs | 191 ++++++++++++++++-- .../ProjectLiner/Drawnings/DrawningLiner.cs | 11 +- .../ProjectLiner/Drawnings/DrawningShip.cs | 11 +- .../Drawnings/ExtentionDrawningShip.cs | 50 +++++ .../ProjectLiner/Entities/EntityLiner.cs | 28 ++- .../ProjectLiner/Entities/EntityShip.cs | 24 +++ .../FormShipCollection.Designer.cs | 76 ++++++- .../ProjectLiner/FormShipCollection.cs | 44 +++- .../ProjectLiner/FormShipCollection.resx | 9 + 13 files changed, 471 insertions(+), 30 deletions(-) create mode 100644 ProjectLiner/ProjectLiner/Drawnings/ExtentionDrawningShip.cs diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs index 9dbf734..5809d5a 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs @@ -48,7 +48,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs index 01a29a4..0bc81b3 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -15,7 +15,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -45,4 +45,15 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs index be59236..deb83e4 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -namespace ProjectLiner.CollectionGenericObjects; + +namespace ProjectLiner.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -19,7 +20,19 @@ public class ListGenericObjects : ICollectionGenericObjects public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { + get + { + return _maxCount; + } + set { + if (value > 0) { + _maxCount = value; + } + } + } + + public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор @@ -63,4 +76,12 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return obj; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; ++i) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs index f7d2cf5..817befc 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@  + namespace ProjectLiner.CollectionGenericObjects; /// @@ -15,8 +16,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } set { if (value > 0) @@ -33,6 +38,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -98,4 +105,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects } return null; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs index 7df3d68..1acf277 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,7 @@ -namespace ProjectLiner.CollectionGenericObjects; +using ProjectLiner.Drawnings; +using System.Text; + +namespace ProjectLiner.CollectionGenericObjects; /// @@ -6,24 +9,39 @@ /// /// public class StorageCollection - where T : class + where T : DrawningShip { + /// + /// Ключевое слово, с которого должен начинаться файл + /// + private readonly string _collectionKey = "CollectionsStorage"; + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storage; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storage.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// public StorageCollection() { - _storage = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -33,7 +51,7 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (string.IsNullOrEmpty(name) || _storage.ContainsKey(name)) + if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name)) { return; } @@ -43,11 +61,11 @@ public class StorageCollection } if (collectionType == CollectionType.Massive) { - _storage[name] = new MassiveGenericObjects(); + _storages[name] = new MassiveGenericObjects(); } else if (collectionType == CollectionType.List) { - _storage[name] = new ListGenericObjects(); + _storages[name] = new ListGenericObjects(); } } @@ -57,11 +75,11 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (name == null || !_storage.ContainsKey(name)) + if (name == null || !_storages.ContainsKey(name)) { return; } - _storage.Remove(name); + _storages.Remove(name); } /// @@ -73,9 +91,9 @@ public class StorageCollection { get { - if (_storage.ContainsKey(name)) + if (_storages.ContainsKey(name)) { - return _storage[name]; + return _storages[name]; } else { @@ -83,4 +101,153 @@ public class StorageCollection } } } + + /// + /// Сохранение информации по кораблям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + + if (File.Exists(filename)) + { + File.Delete(filename); + } + + StringBuilder sb = new(); + sb.Append(_collectionKey); + + foreach (KeyValuePair> value in _storages) + { + sb.Append(Environment.NewLine); + + // не сохраняем пустые коллекции + if (value.Value.Count == 0) + { + continue; + } + + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + + if (string.IsNullOrEmpty(data)) + { + continue; + } + + sb.Append(data); + sb.Append(_separatorItems); + } + } + + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); + fs.Write(info, 0, info.Length); + return true; + } + + /// + /// Загрузка информации по кораблям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + + string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + + if (strs == null || strs.Length == 0) + { + return false; + } + + if (!strs[0].Equals(_collectionKey)) + { + //если нет такой записи, то это не те данные + return false; + } + + _storages.Clear(); + + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + + if (record.Length != 4) + { + continue; + } + + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + + if (collection == null) + { + return false; + } + + collection.MaxCount = Convert.ToInt32(record[2]); + + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawningShip() is T ship) + { + if (collection.Insert(ship) == -1) + { + return false; + } + } + } + + _storages.Add(record[0], collection); + } + + return true; + } + + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Massive => new MassiveGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } } diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs index 8c79154..d6614bc 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs @@ -21,6 +21,16 @@ public class DrawningLiner : DrawningShip EntityShip = new EntityLiner(speed, weight, bodyColor, additionalColor, secondDeck, pool); } + + /// + /// Конструктор принимающий сущность + /// + /// Объект класса-сущность + public DrawningLiner(EntityShip entityShip) : base() + { + EntityShip = entityShip; + } + public override void DrawTransport(Graphics g) { if (EntityShip == null || EntityShip is not EntityLiner Liner || !_startPosX.HasValue || !_startPosY.HasValue) @@ -29,7 +39,6 @@ public class DrawningLiner : DrawningShip } Pen pen = new(Color.Black, 2); - Brush br = new SolidBrush(Liner.BodyColor); Brush additionalBrush = new SolidBrush(Liner.AdditionalColor); // Вторая палуба diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningShip.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningShip.cs index 8181a2b..f45fe7d 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawningShip.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningShip.cs @@ -65,7 +65,7 @@ public class DrawningShip /// /// Пустой конструктор /// - private DrawningShip() + protected DrawningShip() { _pictureWidth = null; _pictureHeight = null; @@ -84,6 +84,15 @@ public class DrawningShip EntityShip = new EntityShip(speed, weight, bodyColor); } + /// + /// Конструктор принимающий сущность + /// + /// Объект класса-сущность + public DrawningShip(EntityShip entityShip) : base() + { + EntityShip = entityShip; + } + /// /// Конструктор для наследников /// diff --git a/ProjectLiner/ProjectLiner/Drawnings/ExtentionDrawningShip.cs b/ProjectLiner/ProjectLiner/Drawnings/ExtentionDrawningShip.cs new file mode 100644 index 0000000..9a50412 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Drawnings/ExtentionDrawningShip.cs @@ -0,0 +1,50 @@ +using ProjectLiner.Entities; + +namespace ProjectLiner.Drawnings; + +/// +/// Расширение для класса EntityShip +/// +public static class ExtentionDrawningShip +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningShip? CreateDrawningShip(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityShip? ship = EntityLiner.CreateEntityLiner(strs); + if (ship != null) + { + return new DrawningLiner(ship); + } + ship = EntityShip.CreateEntityShip(strs); + if (ship != null) + { + return new DrawningShip(ship); + } + return null; + } + + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningShip drawningShip) + { + string[]? array = drawningShip?.EntityShip?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} diff --git a/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs index 5b7698f..060a93a 100644 --- a/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs +++ b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs @@ -1,4 +1,6 @@ -namespace ProjectLiner.Entities; +using System.ComponentModel; + +namespace ProjectLiner.Entities; /// /// Класс-сущность "Лайнер" @@ -39,4 +41,28 @@ public class EntityLiner : EntityShip { AdditionalColor = color; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityLiner), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, SecondDeck.ToString(), Pool.ToString() }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityLiner? CreateEntityLiner(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityLiner)) + { + return null; + } + return new EntityLiner(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), + Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/Entities/EntityShip.cs b/ProjectLiner/ProjectLiner/Entities/EntityShip.cs index ba30327..f514924 100644 --- a/ProjectLiner/ProjectLiner/Entities/EntityShip.cs +++ b/ProjectLiner/ProjectLiner/Entities/EntityShip.cs @@ -42,4 +42,28 @@ public class EntityShip { BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityShip), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityShip? CreateEntityShip(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityShip)) + { + return null; + } + return new EntityShip(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs b/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs index b6c1351..a275372 100644 --- a/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs @@ -50,10 +50,17 @@ namespace ProjectLiner labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + FileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -63,9 +70,9 @@ namespace ProjectLiner groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(793, 0); + groupBoxTools.Location = new Point(793, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(144, 571); + groupBoxTools.Size = new Size(144, 547); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -81,7 +88,7 @@ namespace ProjectLiner panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(3, 321); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(138, 247); + panelCompanyTools.Size = new Size(138, 223); panelCompanyTools.TabIndex = 10; // // buttonAddShip @@ -96,7 +103,7 @@ namespace ProjectLiner // // buttonRefresh // - buttonRefresh.Location = new Point(3, 202); + buttonRefresh.Location = new Point(4, 172); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(126, 42); buttonRefresh.TabIndex = 6; @@ -106,7 +113,7 @@ namespace ProjectLiner // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(4, 101); + maskedTextBoxPosition.Location = new Point(4, 71); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(126, 23); @@ -115,7 +122,7 @@ namespace ProjectLiner // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(4, 166); + buttonGoToCheck.Location = new Point(4, 136); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(126, 30); buttonGoToCheck.TabIndex = 5; @@ -125,7 +132,7 @@ namespace ProjectLiner // // buttonRemoveShip // - buttonRemoveShip.Location = new Point(4, 130); + buttonRemoveShip.Location = new Point(4, 100); buttonRemoveShip.Name = "buttonRemoveShip"; buttonRemoveShip.Size = new Size(126, 30); buttonRemoveShip.TabIndex = 4; @@ -242,12 +249,52 @@ namespace ProjectLiner // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(793, 571); + pictureBox.Size = new Size(793, 547); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(937, 24); + menuStrip.TabIndex = 0; + menuStrip.Text = "menuStrip"; + // + // FileToolStripMenuItem + // + FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + FileToolStripMenuItem.Size = new Size(48, 20); + FileToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(181, 22); + saveToolStripMenuItem.Text = "Сохранение"; + saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(181, 22); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // // FormShipCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -255,6 +302,8 @@ namespace ProjectLiner ClientSize = new Size(937, 571); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormShipCollection"; Text = "Коллекция кораблей"; groupBoxTools.ResumeLayout(false); @@ -263,7 +312,10 @@ namespace ProjectLiner panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -286,5 +338,11 @@ namespace ProjectLiner private Button buttonCollectionDel; private Button buttonCreateCompany; private Panel panelCompanyTools; + private MenuStrip menuStrip; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.cs b/ProjectLiner/ProjectLiner/FormShipCollection.cs index 3ac25c9..e27c942 100644 --- a/ProjectLiner/ProjectLiner/FormShipCollection.cs +++ b/ProjectLiner/ProjectLiner/FormShipCollection.cs @@ -1,5 +1,6 @@ using ProjectLiner.CollectionGenericObjects; using ProjectLiner.Drawnings; +using System.Windows.Forms; namespace ProjectLiner; @@ -77,7 +78,7 @@ public partial class FormShipCollection : Form /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) { return; } @@ -235,4 +236,45 @@ public partial class FormShipCollection : Form panelCompanyTools.Enabled = true; RefreshListBoxItems(); } + + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.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 (_storageCollection.LoadData(openFileDialog.FileName)) + { + RefreshListBoxItems(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.resx b/ProjectLiner/ProjectLiner/FormShipCollection.resx index af32865..8b1dfa1 100644 --- a/ProjectLiner/ProjectLiner/FormShipCollection.resx +++ b/ProjectLiner/ProjectLiner/FormShipCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 126, 17 + + + 261, 17 + \ No newline at end of file