diff --git a/Catamaran/Catamaran/CollectionGenericObjects/AbstractCompany.cs b/Catamaran/Catamaran/CollectionGenericObjects/AbstractCompany.cs index f8b4b77..886a0fd 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/AbstractCompany.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/AbstractCompany.cs @@ -27,7 +27,7 @@ namespace Catamaran.CollectionGenericObjects _pictureHeight = picHeight; _pictureWidth = picWidth; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } public static int operator +(AbstractCompany company, DrawingBoat boat) diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs index 9d3a914..321e0bd 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ArrayGenericObjects.cs @@ -13,8 +13,13 @@ namespace Catamaran.CollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } + set { if (value > 0) @@ -31,6 +36,8 @@ namespace Catamaran.CollectionGenericObjects } } + public CollectionType GetCollectionType => CollectionType.Array; + public ArrayGenericObjects() { _collection = Array.Empty(); @@ -94,5 +101,12 @@ namespace Catamaran.CollectionGenericObjects return obj; } + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; i++) + { + yield return _collection[i]; + } + } } } diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ICollectionGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ICollectionGenericObjects.cs index dea89d7..2079e95 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -11,7 +11,7 @@ namespace Catamaran.CollectionGenericObjects { int Count { get; } - int SetMaxCount { set; } + int MaxCount { get; set; } int Insert(T obj); @@ -20,5 +20,9 @@ namespace Catamaran.CollectionGenericObjects T? Remove(int position); T? Get(int position); + + CollectionType GetCollectionType { get; } + + IEnumerable GetItems(); } } diff --git a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs index 8a537b6..69bf080 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/ListGenericObjects.cs @@ -16,7 +16,24 @@ namespace Catamaran.CollectionGenericObjects 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; public ListGenericObjects() { @@ -62,5 +79,13 @@ namespace Catamaran.CollectionGenericObjects _collection.RemoveAt(position); return obj; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; i++) + { + yield return _collection[i]; + } + } } } diff --git a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs index 3108130..7ca5dd6 100644 --- a/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs +++ b/Catamaran/Catamaran/CollectionGenericObjects/StorageCollection.cs @@ -1,6 +1,7 @@ using Catamaran.Drawings; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,12 +9,18 @@ using System.Threading.Tasks; namespace Catamaran.CollectionGenericObjects { public class StorageCollection - where T : class + where T : DrawingBoat { readonly Dictionary> _storages; public List Keys => _storages.Keys.ToList(); + private readonly string _collectionKey = "CollectionsStorage"; + + private readonly string _separatorKeyValue = "|"; + + private readonly string _separatorItems = ";"; + public StorageCollection() { _storages = new Dictionary>(); @@ -58,5 +65,120 @@ namespace Catamaran.CollectionGenericObjects return _storages[name]; } } + + 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(_separatorKeyValue); + writer.Write(value.Value.GetCollectionType); + writer.Write(_separatorKeyValue); + writer.Write(value.Value.MaxCount); + writer.Write(_separatorKeyValue); + + 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(_separatorKeyValue, 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?.CreateDrawingBoat() is T boat) + { + if (collection.Insert(boat) < 0) + { + return false; + } + } + } + _storages.Add(record[0], collection); + + } + } + return true; + } + + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Array => new ArrayGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } + } } diff --git a/Catamaran/Catamaran/Drawings/DrawingBoat.cs b/Catamaran/Catamaran/Drawings/DrawingBoat.cs index 34f2648..4d6f634 100644 --- a/Catamaran/Catamaran/Drawings/DrawingBoat.cs +++ b/Catamaran/Catamaran/Drawings/DrawingBoat.cs @@ -47,6 +47,12 @@ namespace Catamaran.Drawings } + public DrawingBoat(EntityBoat? entityBoat) : this() + { + if (entityBoat == null) return; + EntityBoat = new EntityBoat(entityBoat.Speed, entityBoat.Weight, entityBoat.BodyColor); + } + protected DrawingBoat(int drawingCatamaranWidth, int drawingCatamaranHeight) : this() { _drawingCatamaranWidth = drawingCatamaranWidth; diff --git a/Catamaran/Catamaran/Drawings/DrawingCatamaran.cs b/Catamaran/Catamaran/Drawings/DrawingCatamaran.cs index f01a0d5..d87bf52 100644 --- a/Catamaran/Catamaran/Drawings/DrawingCatamaran.cs +++ b/Catamaran/Catamaran/Drawings/DrawingCatamaran.cs @@ -17,6 +17,12 @@ namespace Catamaran.Drawings EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, leftBobber, rightBobber, sail); } + public DrawingCatamaran(EntityCatamaran? entityCatamaran) : base(120, 90) + { + if (entityCatamaran == null) return; + EntityBoat = new EntityCatamaran(entityCatamaran.Speed, entityCatamaran.Weight, entityCatamaran.BodyColor, + entityCatamaran.AdditionalColor, entityCatamaran.LeftBobber, entityCatamaran.RightBobber, entityCatamaran.Sail); + } public override void DrawTransport(Graphics g) { diff --git a/Catamaran/Catamaran/Drawings/ExtentionDrawingBoat.cs b/Catamaran/Catamaran/Drawings/ExtentionDrawingBoat.cs new file mode 100644 index 0000000..2d0e0a9 --- /dev/null +++ b/Catamaran/Catamaran/Drawings/ExtentionDrawingBoat.cs @@ -0,0 +1,42 @@ +using Catamaran.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.Drawings +{ + public static class ExtentionDrawingBoat + { + private static readonly string _separator = ":"; + + public static DrawingBoat? CreateDrawingBoat(this string info) + { + string[] strs = info.Split(_separator); + EntityBoat? boat = EntityCatamaran.CreateEntityCatamaran(strs); + if (boat != null) + { + return new DrawingCatamaran((EntityCatamaran)boat); + } + + boat = EntityBoat.CreateEntityBoat(strs); + if (boat != null) + { + return new DrawingBoat(boat); + } + return null; + } + + public static string GetDataForSave(this DrawingBoat drawingBoat) + { + string[]? array = drawingBoat?.EntityBoat?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separator, array); + } + + } +} diff --git a/Catamaran/Catamaran/Entities/Catamaran.cs b/Catamaran/Catamaran/Entities/Catamaran.cs index 094c8aa..403db40 100644 --- a/Catamaran/Catamaran/Entities/Catamaran.cs +++ b/Catamaran/Catamaran/Entities/Catamaran.cs @@ -24,6 +24,21 @@ namespace Catamaran.Entities AdditionalColor = addColor; } + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityCatamaran), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, LeftBobber.ToString(), RightBobber.ToString(), Sail.ToString() }; + } + + public static EntityCatamaran? CreateEntityCatamaran(string[] strs) + { + if (strs.Length != 8 || strs[0] != nameof(EntityCatamaran)) + { + return null; + } + return new EntityCatamaran(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), Convert.ToBoolean(strs[7])); + } + public EntityCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; diff --git a/Catamaran/Catamaran/Entities/EntityBoat.cs b/Catamaran/Catamaran/Entities/EntityBoat.cs index 6c0dacb..9f9a769 100644 --- a/Catamaran/Catamaran/Entities/EntityBoat.cs +++ b/Catamaran/Catamaran/Entities/EntityBoat.cs @@ -21,6 +21,22 @@ namespace Catamaran.Entities BodyColor = color; } + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityBoat), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + public static EntityBoat? CreateEntityBoat(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityBoat)) + { + return null; + } + return new EntityBoat(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } + + public EntityBoat(int speed, double weight, Color bodyColor) { Speed = speed; diff --git a/Catamaran/Catamaran/FormBoatColletion.Designer.cs b/Catamaran/Catamaran/FormBoatColletion.Designer.cs index aed6486..e6754dd 100644 --- a/Catamaran/Catamaran/FormBoatColletion.Designer.cs +++ b/Catamaran/Catamaran/FormBoatColletion.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip1 = new MenuStrip(); + FileToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); groupBox1.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelCollection.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -59,9 +66,9 @@ groupBox1.Controls.Add(panelCollection); groupBox1.Controls.Add(comboBoxSelectorCompany); groupBox1.Dock = DockStyle.Right; - groupBox1.Location = new Point(809, 0); + groupBox1.Location = new Point(814, 28); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(237, 642); + groupBox1.Size = new Size(237, 646); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; @@ -75,7 +82,7 @@ panelCompanyTools.Controls.Add(DeleteButton); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 398); + panelCompanyTools.Location = new Point(3, 402); panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Size = new Size(231, 241); panelCompanyTools.TabIndex = 9; @@ -235,19 +242,63 @@ // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(809, 642); + pictureBox.Size = new Size(814, 646); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1051, 28); + menuStrip1.TabIndex = 2; + menuStrip1.Text = "menuStrip1"; + // + // FileToolStripMenuItem + // + FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + FileToolStripMenuItem.Size = new Size(59, 24); + FileToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + SaveToolStripMenuItem.Size = new Size(216, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + LoadToolStripMenuItem.Size = new Size(216, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + // // FormBoatColletion // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1046, 642); + ClientSize = new Size(1051, 674); Controls.Add(pictureBox); Controls.Add(groupBox1); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Name = "FormBoatColletion"; Text = "Коллекция лодок"; groupBox1.ResumeLayout(false); @@ -256,7 +307,10 @@ panelCollection.ResumeLayout(false); panelCollection.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -279,5 +333,11 @@ private ListBox listBoxCollection; private Button buttonCollectionAdd; private Panel panelCompanyTools; + private MenuStrip menuStrip1; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/Catamaran/Catamaran/FormBoatColletion.cs b/Catamaran/Catamaran/FormBoatColletion.cs index 32502d5..76e13ba 100644 --- a/Catamaran/Catamaran/FormBoatColletion.cs +++ b/Catamaran/Catamaran/FormBoatColletion.cs @@ -30,7 +30,7 @@ namespace Catamaran panelCompanyTools.Enabled = false; } - + private void AddBoatButton_Click(object sender, EventArgs e) { @@ -211,6 +211,37 @@ namespace Catamaran 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)) + { + MessageBox.Show("Успешно загружено", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RefreshListBoxItems(); + } + else + { + MessageBox.Show("Ошибка загрузки", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } + } } } diff --git a/Catamaran/Catamaran/FormBoatColletion.resx b/Catamaran/Catamaran/FormBoatColletion.resx index af32865..769f863 100644 --- a/Catamaran/Catamaran/FormBoatColletion.resx +++ b/Catamaran/Catamaran/FormBoatColletion.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 153, 17 + + + 318, 17 + \ No newline at end of file