diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs index 0ab649f..4ed36b2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/AbstractCompany.cs @@ -55,7 +55,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs index b5d541d..1e87760 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -21,7 +21,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального кол-ва элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -51,4 +51,9 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + CollectionType GetCollectionType { get; } + + IEnumerable GetItems(); + } diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs index e0aa358..3303953 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs @@ -14,7 +14,20 @@ public class ListGenericObjects : ICollectionGenericObjects private int _maxCount; public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { + + get => _maxCount; + + set + { + if (value > 0) + { + _maxCount = value; + } + } + } + + public CollectionType GetCollectionType => CollectionType.List; public ListGenericObjects() { @@ -88,4 +101,12 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return removed_object; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; i++) + { + yield return _collection[i]; + } + } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs index 887317e..4a6444e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -17,8 +17,13 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } + set { if (value > 0) @@ -33,7 +38,11 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } } + } + + public CollectionType GetCollectionType => CollectionType.Massive; + public MassiveGenericObjects() { _collection = Array.Empty(); @@ -98,4 +107,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[position] = null; return removed_object; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; i++) + { + yield return _collection[i]; + } + } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs index 47ef55d..21ce849 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ -using System; +using DoubleDeckerBus.Drawnings; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,7 +12,7 @@ namespace DoubleDeckerBus.CollectionGenericObjects; /// /// public class StorageCollection - where T : class + where T : DrawingBus { /// /// Словарь (хранилище) с коллекциями @@ -23,6 +24,13 @@ public class StorageCollection /// public List Keys => _storages.Keys.ToList(); + private readonly string _collectionKey = "CollectionsStorage"; + + private readonly string _separatorForKeyValue = "|"; + + private readonly string _separatorItems = ";"; + + public StorageCollection() { _storages = new Dictionary>(); @@ -69,4 +77,139 @@ public class StorageCollection return null; } } + + 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; + + } + + 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?.CreateDrawningCar() is T car) + { + if (collection.Insert(car) == -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/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index 92599a0..538603c 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -89,6 +89,11 @@ public class DrawingBus EntityBus = new EntityBus(speed, weight, bodyColor); } + public DrawingBus(EntityBus? entityBus) : this() + { + EntityBus = entityBus; + } + /// /// Конструктор для наследников /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index f3f2f04..b3c7a45 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -20,6 +20,11 @@ public class DrawingDoubleDeckerBus : DrawingBus EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); } + public DrawingDoubleDeckerBus(EntityBus entityDoubleDeckerBus) : base(115, 55) + { + EntityBus = entityDoubleDeckerBus; + } + public override void DrawTrasnport(Graphics g) { if (EntityBus == null || EntityBus is not EntityDoubleDeckerBus doubleDeckerBus || !_startPosX.HasValue || !_startPosY.HasValue ) diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/ExtentionDrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/ExtentionDrawingBus.cs new file mode 100644 index 0000000..e58fd56 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/ExtentionDrawingBus.cs @@ -0,0 +1,43 @@ +using DoubleDeckerBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Drawnings; + +public static class ExtentionDrawingBus +{ + private static readonly string _separatorForIbject = ":"; + + public static DrawingBus? CreateDrawningCar(this string info) + { + string[] strs = info.Split(_separatorForIbject); + EntityBus? bus = EntityDoubleDeckerBus.CreateEntityDoubleDeckerBus(strs); + if (bus != null) + { + return new DrawingDoubleDeckerBus(bus); + } + + bus = EntityBus.CreateEntityBus(strs); + if (bus != null) + { + return new DrawingBus(bus); + } + + return null; + } + + public static string GetDataForSave(this DrawingBus drawingBus) + { + string[]? array = drawingBus?.EntityBus?.GetStringRepresentation(); + + if (array == null) + { + return string.Empty; + } + + return string.Join(_separatorForIbject, array); + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs index 02fde82..79c95bc 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs @@ -47,5 +47,21 @@ public class EntityBus { BodyColor = bodyColor; } + + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityBus), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + + public static EntityBus? CreateEntityBus(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityBus)) + { + return null; + } + + return new EntityBus(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } + } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs index 1db1247..187dcae 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -36,4 +36,19 @@ public class EntityDoubleDeckerBus : EntityBus { AdditionalColor = additionalColor; } + + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityDoubleDeckerBus), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, SecondFloor.ToString(), Stripes.ToString() }; + } + + public static EntityDoubleDeckerBus? CreateEntityDoubleDeckerBus(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityDoubleDeckerBus)) + { + return null; + } + + return new EntityDoubleDeckerBus(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/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs index 46cab0e..f686b20 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip1 = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -59,9 +66,9 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(870, 0); + groupBoxTools.Location = new Point(870, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 626); + groupBoxTools.Size = new Size(200, 602); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Tools"; @@ -75,7 +82,7 @@ panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 358); + panelCompanyTools.Location = new Point(3, 334); panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Size = new Size(194, 265); panelCompanyTools.TabIndex = 16; @@ -239,12 +246,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(870, 626); + pictureBox.Size = new Size(870, 602); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip1 + // + menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1070, 24); + menuStrip1.TabIndex = 2; + menuStrip1.Text = "menuStrip"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(37, 20); + fileToolStripMenuItem.Text = "File"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(180, 22); + saveToolStripMenuItem.Text = "Save"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(180, 22); + loadToolStripMenuItem.Text = "Load"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.Filter = "txt file|*.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file|*.txt"; + // // FormBusCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -252,6 +299,8 @@ ClientSize = new Size(1070, 626); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Name = "FormBusCollection"; Text = "Bus collection"; groupBoxTools.ResumeLayout(false); @@ -260,7 +309,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -283,5 +335,11 @@ private Button buttonCreateCompany; private Button buttonRemoveCollection; private Panel panelCompanyTools; + private MenuStrip menuStrip1; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs index ae5edc4..e96be51 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -57,7 +57,7 @@ public partial class FormBusCollection : Form } } - + private void ButtonRemoveBus_Click(object sender, EventArgs e) { @@ -206,4 +206,37 @@ public partial class FormBusCollection : Form panelCompanyTools.Enabled = true; } + + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Save succeeded", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Unable to save", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + } + + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Load succeeded", "Resilt", MessageBoxButtons.OK, MessageBoxIcon.Information); + RefreshListBoxItems(); + } + else + { + MessageBox.Show("Unable to load", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx index af32865..8a67c96 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 271, 17 + \ No newline at end of file