diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/AbstractCompany.cs b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/AbstractCompany.cs index 2305270..2854af4 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/AbstractCompany.cs @@ -53,7 +53,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs index 1708b1a..0ebdf17 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -17,7 +17,7 @@ public interface ICollectionGenericObjects /// ///Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// ///Добавление объекта в коллекцию @@ -48,8 +48,14 @@ public interface ICollectionGenericObjects ///Объект T? Get(int position); + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } - - - + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ListGenericObjects.cs index 16f4b97..b7a565d 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/ListGenericObjects.cs @@ -19,7 +19,24 @@ public class ListGenericObjects : ICollectionGenericObjects public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount + { + get + { + if (_maxCount < _collection.Count) return _maxCount; + return _collection.Count; + } + + set + { + if (value > 0) + { + _maxCount = value; + } + } + } + + public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор @@ -82,4 +99,12 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return temp; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; i++) + { + yield return _collection[i]; + } + } } diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/MassiveGenericObjects.cs index b2014c0..d8a58f8 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -19,8 +19,13 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } + set { if (value > 0) @@ -36,6 +41,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } } + + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -117,4 +125,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects } return null; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/StorageCollection.cs b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/StorageCollection.cs index d1d69db..1842f53 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/CollectionGenericObjects/StorageCollection.cs @@ -1,11 +1,16 @@ -namespace ProjectHoistingCrane.CollectionGenericObjects; +using Microsoft.VisualBasic; +using ProjectHoistingCrane.Drawnings; +using System.Collections.ObjectModel; +using System.Text; + +namespace ProjectHoistingCrane.CollectionGenericObjects; /// /// Класс-хранилище коллекций /// /// public class StorageCollection - where T : class + where T : DrawningCrane { /// /// Словарь (хранилище) с коллекциями @@ -15,6 +20,13 @@ public class StorageCollection /// Возвращение списка названий коллекций /// public List Keys => _storages.Keys.ToList(); + + private readonly string _collectionKey = "CollectionsStorage"; + + private readonly string _separatorForKeyValue = "|"; + + private readonly string _separatorItems = ";"; + /// /// Конструктор /// @@ -74,4 +86,116 @@ public class StorageCollection } } + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + + if (File.Exists(filename)) + { + File.Delete(filename); + } + + using (StreamWriter writer = new(filename)) + { + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) + { + writer.Write(Environment.NewLine); + if (value.Value.Count == 0) + { + continue; + } + + writer.Write(value.Key); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.GetCollectionType); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.MaxCount); + writer.Write(_separatorForKeyValue); + + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + + writer.Write(data); + writer.Write(_separatorItems); + } + } + writer.Close(); + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader reader = new(filename)) + { + string line = reader.ReadLine(); + if (line == null || line.Length == 0) + { + return false; + } + + if (!line.Equals(_collectionKey)) + { + return false; + } + + _storages.Clear(); + while ((line = reader.ReadLine()) != null) + { + string[] record = line.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?.CreateDrawningCrane() is T crane) + { + if (collection.Insert(crane) < 0) + { + 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/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningCrane.cs b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningCrane.cs index 41917bc..05dc206 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningCrane.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningCrane.cs @@ -75,13 +75,17 @@ public class DrawningCrane EntityCrane = new EntityCrane(speed, weight, bodyColor); } + public DrawningCrane(EntityCrane? entityCrane) : this() + { + if (entityCrane == null) return; + EntityCrane = new EntityCrane(entityCrane.Speed, entityCrane.Weight, entityCrane.BodyColor); + } + /// /// Конструктор для наследников /// /// Ширина прорисовки крана /// Высота прорисовки крана - - protected DrawningCrane(int drawningCraneWidth, int drawningCraneHeight) : this() { _drawningCraneWidth = drawningCraneWidth; diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningHoistingCrane.cs b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningHoistingCrane.cs index 0727322..7cd7470 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningHoistingCrane.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/DrawningHoistingCrane.cs @@ -20,6 +20,13 @@ public class DrawningHoistingCrane : DrawningCrane } + public DrawningHoistingCrane(EntityHoistingCrane? entityHoistingCrane) : base(110, 56) + { + if (entityHoistingCrane == null) return; + EntityCrane = new EntityHoistingCrane(entityHoistingCrane.Speed, entityHoistingCrane.Weight, entityHoistingCrane.BodyColor, + entityHoistingCrane.AdditionalColor, entityHoistingCrane.Counterweight, entityHoistingCrane.Crane); + } + public override void DrawTransport(Graphics g) { if (EntityCrane == null || EntityCrane is not EntityHoistingCrane hoistingCrane || !_startPosX.HasValue || !_startPosY.HasValue) @@ -53,7 +60,7 @@ public class DrawningHoistingCrane : DrawningCrane } //противовес - if (hoistingCrane.Сounterweight) + if (hoistingCrane.Counterweight) { g.FillRectangle(additionalBrush, _startPosX.Value + 35, _startPosY.Value, 10, 9); g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value, 9, 8); diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/ExtentionDrawningCrane.cs b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/ExtentionDrawningCrane.cs new file mode 100644 index 0000000..d6e89d7 --- /dev/null +++ b/ProjectHoistingCrane/ProjectHoistingCrane/Drawnings/ExtentionDrawningCrane.cs @@ -0,0 +1,44 @@ +using ProjectHoistingCrane.Entities; + + +namespace ProjectHoistingCrane.Drawnings; + +/// +/// Расширение для класса EntityCrane +/// +public static class ExtentionDrawningCrane +{ + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + private static readonly string _separator = ":"; + + public static DrawningCrane? CreateDrawningCrane(this string info) + { + string[] strs = info.Split(_separator); + EntityCrane? crane = EntityHoistingCrane.CreateEntityHoistingCrane(strs); + if (crane != null) + { + return new DrawningHoistingCrane((EntityHoistingCrane)crane); + } + + crane = EntityCrane.CreateEntityCrane(strs); + if (crane != null) + { + return new DrawningCrane(crane); + } + return null; + } + + public static string GetDataForSave(this DrawningCrane drawningCrane) + { + string[]? array = drawningCrane?.EntityCrane?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separator, array); + } +} \ No newline at end of file diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityCrane.cs b/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityCrane.cs index ad743ac..e15e625 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityCrane.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityCrane.cs @@ -45,4 +45,28 @@ public class EntityCrane Weight = weight; BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityCrane), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityCrane? CreateEntityCrane(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityCrane)) + { + return null; + } + + return new EntityCrane(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityHoistingCrane.cs b/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityHoistingCrane.cs index 20f309c..f8f7f53 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityHoistingCrane.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/Entities/EntityHoistingCrane.cs @@ -10,7 +10,7 @@ public class EntityHoistingCrane : EntityCrane /// /// Признак (опция) наличия противовеса /// - public bool Сounterweight { get; private set; } + public bool Counterweight { get; private set; } /// /// Признак (опция) наличия крана /// @@ -31,10 +31,24 @@ public class EntityHoistingCrane : EntityCrane AdditionalColor = color; } + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityHoistingCrane), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Counterweight.ToString(), Crane.ToString()}; + } + + public static EntityHoistingCrane? CreateEntityHoistingCrane(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityHoistingCrane)) + { + return null; + } + return new EntityHoistingCrane(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } public EntityHoistingCrane(int speed, double weight, Color bodyColor, Color additionalColor, bool counterweight, bool crane):base(speed, weight, bodyColor) { AdditionalColor = additionalColor; - Сounterweight = counterweight; + Counterweight = counterweight; Crane = crane; } -} +} \ No newline at end of file diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.Designer.cs b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.Designer.cs index 054e211..d847329 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.Designer.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = 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 @@ -59,9 +66,9 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(771, 0); + groupBoxTools.Location = new Point(771, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(222, 690); + groupBoxTools.Size = new Size(222, 662); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -241,12 +248,54 @@ // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(771, 690); + pictureBox.Size = new Size(771, 662); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // 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(993, 28); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(227, 26); + saveToolStripMenuItem.Text = "Сохранение"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(227, 26); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file|*.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file|*.txt"; + // // FormCraneCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -254,6 +303,8 @@ ClientSize = new Size(993, 690); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormCraneCollection"; Text = "Коллекция кранов"; groupBoxTools.ResumeLayout(false); @@ -262,7 +313,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -285,5 +339,11 @@ private Button buttonCollectionAdd; private Button buttonCreateCompany; private Panel panelCompanyTools; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.cs b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.cs index 02b3b8d..b32113b 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.cs +++ b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.cs @@ -257,4 +257,37 @@ public partial class FormCraneCollection : Form } } } + + 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)) + { + MessageBox.Show("Успешно загружено", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RerfreshListBoxItems(); + } + else + { + MessageBox.Show("Ошибка загрузки", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } + } } diff --git a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.resx b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.resx index af32865..ee1748a 100644 --- a/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.resx +++ b/ProjectHoistingCrane/ProjectHoistingCrane/FormCraneCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 145, 17 + + + 310, 17 + \ No newline at end of file