diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs index 6a7cd3c..eebd388 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs @@ -67,8 +67,6 @@ namespace DoubleDeckerBus.DrawningObjects g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 60)), new Point((int)(_startPosX + 10), (int)(_startPosY + 60))); } } - - } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/ExtentionDrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/ExtentionDrawningBus.cs new file mode 100644 index 0000000..7a600d8 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/ExtentionDrawningBus.cs @@ -0,0 +1,46 @@ +using DoubleDeckerBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.DrawningObjects +{ + public static class ExtentionDrawningBus + { + public static DrawningBus? CreateDrawningBus(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningBus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawningDoubleDeckerBus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), width, height); + } + return null; + } + public static string GetDataForSave(this DrawningBus drawningbus, char separatorForObject) + { + var bus = drawningbus.EntityBus; + if (bus == null) + { + return string.Empty; + } + var str = $"{bus.Speed}{separatorForObject}{bus.Weight}{separatorForObject}{bus.BodyColor.Name}"; + if (bus is not EntityDoubleDeckerBus doubleDeckerBus) + { + return str; + } + return + $"{str}{separatorForObject}{doubleDeckerBus.AdditionalColor.Name}{separatorForObject}{doubleDeckerBus.SecondFloor}" + + $"{separatorForObject}{doubleDeckerBus.Ladder}{separatorForObject}{doubleDeckerBus.LineBetweenFloor}"; + } + } +} + + + diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs index 611bee8..c1a9038 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -38,14 +38,20 @@ listBoxStorages = new ListBox(); ButtonDelObject = new Button(); ButtonAddObject = new Button(); + menuStrip = new MenuStrip(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panel.SuspendLayout(); + menuStrip.SuspendLayout(); SuspendLayout(); // // buttonAddBus // buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonAddBus.Location = new Point(13, 277); + buttonAddBus.Location = new Point(19, 277); buttonAddBus.Name = "buttonAddBus"; buttonAddBus.Size = new Size(138, 31); buttonAddBus.TabIndex = 0; @@ -56,7 +62,7 @@ // buttonRemoveBus // buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonRemoveBus.Location = new Point(13, 343); + buttonRemoveBus.Location = new Point(19, 343); buttonRemoveBus.Name = "buttonRemoveBus"; buttonRemoveBus.Size = new Size(138, 31); buttonRemoveBus.TabIndex = 1; @@ -67,7 +73,7 @@ // ButtonRefreshCollection // ButtonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right; - ButtonRefreshCollection.Location = new Point(13, 406); + ButtonRefreshCollection.Location = new Point(19, 406); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(138, 31); ButtonRefreshCollection.TabIndex = 2; @@ -78,7 +84,7 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; - maskedTextBoxNumber.Location = new Point(30, 314); + maskedTextBoxNumber.Location = new Point(36, 314); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 23); maskedTextBoxNumber.TabIndex = 3; @@ -86,7 +92,7 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(2, 2); + pictureBoxCollection.Location = new Point(12, 12); pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Size = new Size(633, 454); pictureBoxCollection.TabIndex = 4; @@ -103,17 +109,18 @@ panel.Controls.Add(maskedTextBoxNumber); panel.Controls.Add(ButtonRefreshCollection); panel.Controls.Add(buttonRemoveBus); - panel.Location = new Point(660, 2); + panel.Controls.Add(menuStrip); + panel.Location = new Point(676, 12); panel.Name = "panel"; - panel.Size = new Size(162, 454); + panel.Size = new Size(168, 454); panel.TabIndex = 5; panel.Tag = ""; // // textBoxStorageName // - textBoxStorageName.Location = new Point(13, 20); + textBoxStorageName.Location = new Point(13, 36); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(131, 23); + textBoxStorageName.Size = new Size(138, 23); textBoxStorageName.TabIndex = 9; // // listBoxStorages @@ -146,11 +153,44 @@ ButtonAddObject.UseVisualStyleBackColor = true; ButtonAddObject.Click += ButtonAddObject_Click; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { SaveToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(168, 24); + menuStrip.TabIndex = 10; + menuStrip.Text = "menuStrip1"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { LoadToolStripMenuItem }); + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(144, 20); + SaveToolStripMenuItem.Text = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(201, 22); + LoadToolStripMenuItem.Text = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog"; + openFileDialog.Filter = "«txt file | *.txt»"; + // + // saveFileDialog + // + saveFileDialog.Filter = "«txt file | *.txt»"; + // // FormBusCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(823, 468); + ClientSize = new Size(879, 461); Controls.Add(panel); Controls.Add(pictureBoxCollection); Name = "FormBusCollection"; @@ -158,6 +198,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); panel.ResumeLayout(false); panel.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); } @@ -174,5 +216,10 @@ private Button ButtonDelObject; private Button ButtonAddObject; private TextBox textBoxStorageName; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; + private MenuStrip menuStrip; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs index 852238f..3e2c112 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -9,9 +9,9 @@ using System.Threading.Tasks; using System.Windows.Forms; using DoubleDeckerBus.DrawningObjects; using DoubleDeckerBus.Generics; -using DoubleDeckerBus.Generics.ProjectSportCar.Generics; using DoubleDeckerBus.MovementStrategy; using static System.Windows.Forms.DataFormats; +using System.Windows.Forms; namespace DoubleDeckerBus { @@ -47,7 +47,7 @@ namespace DoubleDeckerBus { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses(); } - private void ButtonAddObject_Click(object sender, EventArgs e) + private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) @@ -144,5 +144,39 @@ namespace DoubleDeckerBus } pictureBoxCollection.Image = obj.ShowBuses(); } + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + foreach (var collection in _storage.Keys) + { + listBoxStorages.Items.Add(collection); + } + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } + + diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx index af32865..7ea2349 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx @@ -117,4 +117,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 11, 17 + + + 132, 17 + + + 271, 17 + + + 48 + \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs index 62aff82..4326941 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs @@ -12,6 +12,7 @@ namespace DoubleDeckerBus.Generics where T : DrawningBus where U : IMoveableObject { + public IEnumerable GetTheBuses => _collection.GetTheBuses(); private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 210; diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs index 6b9eb8e..81800ad 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs @@ -8,45 +8,126 @@ using DoubleDeckerBus.MovementStrategy; namespace DoubleDeckerBus.Generics { - namespace ProjectSportCar.Generics + internal class BusesGenericStorage { - internal class BusesGenericStorage + readonly Dictionary> _busStorages; + public List Keys => _busStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; + public BusesGenericStorage(int pictureWidth, int pictureHeight) { - readonly Dictionary> _busStorages; - public List Keys => _busStorages.Keys.ToList(); - private readonly int _pictureWidth; - private readonly int _pictureHeight; - public BusesGenericStorage(int pictureWidth, int pictureHeight) + _busStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public bool SaveData(string filename) + { + if (File.Exists(filename)) { - _busStorages = new Dictionary>(); - _pictureWidth = pictureWidth; - _pictureHeight = pictureHeight; + File.Delete(filename); } - public void AddSet(string name) + StringBuilder data = new(); + foreach (KeyValuePair> record in _busStorages) { - _busStorages.Add(name, new BusesGenericCollection(_pictureWidth, _pictureHeight)); - } - public void DelSet(string name) - { - if (!_busStorages.ContainsKey(name)) + StringBuilder records = new(); + foreach (DrawningBus? elem in record.Value.GetTheBuses) { - return; + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - - _busStorages.Remove(name); + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); } - public BusesGenericCollection? - this[string ind] + + if (data.Length == 0) { - get + return false; + } + string toWrite = $"BusStorage{Environment.NewLine}{data}"; + var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + using (StreamWriter sw = new(filename)) + { + foreach (var str in strs) { - if (_busStorages.ContainsKey(ind)) + sw.WriteLine(str); + } + } + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("BusStorage")) + { + return false; + } + _busStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - return _busStorages[ind]; + str = sr.ReadLine(); + continue; } + BusesGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningBus? bus = + elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight); + if (bus != null) + { + if (!(collection + bus)) + { + return false; + } + } + } + _busStorages.Add(record[0], collection); - return null; + str = sr.ReadLine(); + } while (str != null); + } + return true; + } + public void AddSet(string name) + { + _busStorages.Add(name, new BusesGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (!_busStorages.ContainsKey(name)) + { + return; + } + + _busStorages.Remove(name); + } + public BusesGenericCollection? + this[string ind] + { + get + { + if (_busStorages.ContainsKey(ind)) + { + return _busStorages[ind]; } + + return null; } } }