diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs index 574522b..246c017 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs @@ -21,7 +21,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } public static int? operator +(AbstractCompany company, DrawingPlane plane) diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs index a1b455c..2a6e57f 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -5,7 +5,7 @@ public interface ICollectionGenericObjects { int Count { get; } - int SetMaxCount { set; } + int MaxCount { set; get; } int Insert(T obj); @@ -14,4 +14,8 @@ public interface ICollectionGenericObjects T Remove(int position); T? Get(int position); + + CollectionType GetCollectionType { get; } + + IEnumerable GetItems(); } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs index f1cdedb..9b46b13 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ListGenericObjects.cs @@ -5,11 +5,26 @@ public class ListGenericObjects : ICollectionGenericObjects { private readonly List _collection; + public CollectionType GetCollectionType => CollectionType.List; + private int _maxCount; public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount + { + get + { + return Count; + } + set + { + if (value > 0) + { + _maxCount = value; + } + } + } public ListGenericObjects() { @@ -42,4 +57,12 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return obj; } + + public IEnumerable GetItems() + { + for (int i = 0; i < Count; ++i) + { + yield return _collection[i]; + } + } } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs index 0725008..e0d0a69 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -7,8 +7,14 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public CollectionType GetCollectionType => CollectionType.Massive; + + public int MaxCount { + get + { + return _collection.Length; + } set { if (value > 0) @@ -89,4 +95,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[position] = null; return obj; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs index 1feafd5..6b80720 100644 --- a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/StorageCollection.cs @@ -1,7 +1,10 @@ -namespace ProjectSeaplane.CollectionGenericObjects; +using ProjectSeaplane.Drawings; +using System.Text; + +namespace ProjectSeaplane.CollectionGenericObjects; public class StorageCollection - where T : class + where T : DrawingPlane { /// /// Словарь (хранилище) с коллекциями @@ -60,4 +63,133 @@ public class StorageCollection return null; } } + + private readonly string _collectionKey = "CollectionsStorage"; + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) + { + StringBuilder sb = new(); + 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); + } + writer.Write(sb); + } + + } + + return true; + } + + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader fs = File.OpenText(filename)) + { + string str = fs.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith(_collectionKey)) + { + return false; + } + _storages.Clear(); + string strs = ""; + while ((strs = fs.ReadLine()) != null) + { + string[] record = strs.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?.CreateDrawningPlane() 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/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs index 19b754d..b6fbb76 100644 --- a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs @@ -49,6 +49,11 @@ public class DrawingPlane _drawingPlaneHeight = drawingSeaplaneHeight; } + public DrawingPlane(EntityPlane ship) : this() + { + EntityPlane = new EntityPlane(ship.Speed, ship.Weight, ship.BodyColor); + } + public bool SetPictureSize(int width, int height) { if (_drawingPlaneWidth > width || _drawingPlaneHeight > height) diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingSeaplane.cs index 915b8bc..a2ea5ce 100644 --- a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingSeaplane.cs @@ -14,6 +14,11 @@ public class DrawingSeaplane : DrawingPlane EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, floats, inflatableBoat); } + public DrawingSeaplane(EntitySeaplane ship) : base(190, 85) + { + EntityPlane = new EntitySeaplane(ship.Speed, ship.Weight, ship.BodyColor, ship.AdditionalColor, ship.Floats, ship.InflatableBoat); + } + public override void DrawTransport(Graphics g) { if (EntityPlane == null || EntityPlane is not EntitySeaplane seaplane || !_startPosX.HasValue || !_startPosY.HasValue) diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/ExtentionDrawingPlane.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/ExtentionDrawingPlane.cs new file mode 100644 index 0000000..38dc76b --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/ExtentionDrawingPlane.cs @@ -0,0 +1,39 @@ +using ProjectSeaplane.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Drawings; + +public static class ExtentionDrawingPlane +{ + private static readonly string _separatorForObject = ":"; + + public static DrawingPlane? CreateDrawningPlane(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityPlane? plane = EntitySeaplane.CreateEntitySeaplane(strs); + if (plane != null) + { + return new DrawingSeaplane((EntitySeaplane)plane); + } + plane = EntityPlane.CreateEntityPlane(strs); + if (plane != null) + { + return new DrawingPlane(plane); + } + return null; + } + + public static string GetDataForSave(this DrawingPlane drawningCar) + { + string[]? array = drawningCar?.EntityPlane?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs index e96f6ae..5b6da6c 100644 --- a/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntityPlane.cs @@ -27,5 +27,19 @@ public class EntityPlane Weight = weight; BodyColor = bodyColor; } + + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityPlane), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + public static EntityPlane? CreateEntityPlane(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityPlane)) + { + return null; + } + return new EntityPlane(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } diff --git a/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs index 2051fe4..037b073 100644 --- a/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Entities/EntitySeaplane.cs @@ -23,5 +23,21 @@ public class EntitySeaplane : EntityPlane Floats = floats; InflatableBoat = inflatableBoat; } + + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntitySeaplane), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, + Floats.ToString(), InflatableBoat.ToString()}; + } + + public static EntitySeaplane? CreateEntitySeaplane(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntitySeaplane)) + { + return null; + } + return new EntitySeaplane(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), + Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } } diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs index 5c4690d..3b3f14a 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.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(764, 0); + groupBoxTools.Location = new Point(764, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 627); + groupBoxTools.Size = new Size(200, 603); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -235,12 +242,52 @@ // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(764, 627); + pictureBox.Size = new Size(764, 603); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(964, 24); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.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"; + // // FormSeaplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -248,6 +295,8 @@ ClientSize = new Size(964, 627); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormSeaplaneCollection"; Text = "FormSeaplaneCollection"; groupBoxTools.ResumeLayout(false); @@ -256,7 +305,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -279,5 +331,11 @@ private TextBox textBoxCollectionName; 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/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs index 74495cf..342e215 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs @@ -21,7 +21,7 @@ public partial class FormSeaplaneCollection : Form panelCompanyTools.Enabled = false; } - private void ButtonAddPlane_Click(object sender, EventArgs e) + private void ButtonAddPlane_Click(object sender, EventArgs e) { FormPlaneConfig form = new(); form.Show(); @@ -232,4 +232,39 @@ public partial class FormSeaplaneCollection : Form panelCompanyTools.Enabled = true; RerfreshListBoxItems(); } + + 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/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx index af32865..7dc5378 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 126, 17 + + + 255, 17 + \ No newline at end of file