diff --git a/DumpTruck/DumpTruck/CarsGenericCollection.cs b/DumpTruck/DumpTruck/CarsGenericCollection.cs index 880078c..3d74ec6 100644 --- a/DumpTruck/DumpTruck/CarsGenericCollection.cs +++ b/DumpTruck/DumpTruck/CarsGenericCollection.cs @@ -13,6 +13,7 @@ namespace DumpTruck.Generics where T : DrawningCar where U : IMoveableObject { + public IEnumerable GetCars => _collection.GetCars(); /// /// Ширина окна прорисовки /// diff --git a/DumpTruck/DumpTruck/CarsGenericStorage.cs b/DumpTruck/DumpTruck/CarsGenericStorage.cs index 4409f1d..93e569b 100644 --- a/DumpTruck/DumpTruck/CarsGenericStorage.cs +++ b/DumpTruck/DumpTruck/CarsGenericStorage.cs @@ -10,6 +10,18 @@ namespace DumpTruck.Generics { internal class CarsGenericStorage { + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; /// /// Словарь (хранилище) /// @@ -77,6 +89,99 @@ namespace DumpTruck.Generics return null; } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _carStorages) + { + StringBuilder records = new(); + foreach (DrawningCar? elem in record.Value.GetCars) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + + if (data.Length == 0) + { + return false; + } + string toWrite = $"CarStorage{Environment.NewLine}{data}"; + var strs = toWrite.Split(new char[] { '\n', '\r' }, +StringSplitOptions.RemoveEmptyEntries); + + using (StreamWriter sw = new(filename)) + { + foreach (var str in strs) + { + 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("CarStorage")) + { + return false; + } + _carStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + CarsGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningCar? car = elem?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight); + if (car != null) + { + if (collection + car == -1) + { + return false; + } + } + } + _carStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); + } + + return true; + } + } + + } } diff --git a/DumpTruck/DumpTruck/ExtentionDrawningCar.cs b/DumpTruck/DumpTruck/ExtentionDrawningCar.cs new file mode 100644 index 0000000..ef7ef13 --- /dev/null +++ b/DumpTruck/DumpTruck/ExtentionDrawningCar.cs @@ -0,0 +1,55 @@ +using DumpTruck.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck +{ + public static class ExtentionDrawningCar + { + public static DrawningCar? CreateDrawningCar(this string info, char +separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningCar(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + + if (strs.Length == 6) + { + return new DrawningDumpTruck(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), width, height); + } + return null; + } + + public static string GetDataForSave(this DrawningCar drawningCar, +char separatorForObject) + { + var car = drawningCar.EntityCar; + if (car == null) + { + return string.Empty; + } + var str = $"{car.Speed}{separatorForObject}{car.Weight}{separatorForObject}{car.BodyColor.Name}"; + if (car is not EntityDumpTruck dumpTruck) + { + return str; + } + return + $"{str}{separatorForObject}{dumpTruck.AdditionalColor.Name}" + + $"{separatorForObject}{dumpTruck.BodyKit}{separatorForObject}{dumpTruck.Tent}"; + + } + + } +} diff --git a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs index 9d28df0..2bfc252 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.Designer.cs @@ -40,17 +40,24 @@ this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); this.ButtonAddObject = new System.Windows.Forms.Button(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelCollection.SuspendLayout(); this.panel1.SuspendLayout(); this.panelObjects.SuspendLayout(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCollection.Location = new System.Drawing.Point(0, 27); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(630, 482); + this.pictureBoxCollection.Size = new System.Drawing.Size(630, 455); this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // @@ -161,6 +168,48 @@ this.ButtonAddObject.UseVisualStyleBackColor = true; this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(848, 24); + this.menuStrip.TabIndex = 2; + this.menuStrip.Text = "Файл"; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.LoadToolStripMenuItem, + this.SaveToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormCarCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -168,6 +217,8 @@ this.ClientSize = new System.Drawing.Size(848, 482); this.Controls.Add(this.panelCollection); this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormCarCollection"; this.Text = "Набор грузовиков"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -176,7 +227,10 @@ this.panel1.PerformLayout(); this.panelObjects.ResumeLayout(false); this.panelObjects.PerformLayout(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -194,5 +248,11 @@ private Panel panel1; private Button ButtonDelObject_; private TextBox textBoxStorageName; + private MenuStrip menuStrip; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormCarCollection.cs b/DumpTruck/DumpTruck/FormCarCollection.cs index 345f7b2..e6f283a 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.cs +++ b/DumpTruck/DumpTruck/FormCarCollection.cs @@ -181,6 +181,46 @@ MessageBoxIcon.Question) == DialogResult.Yes) { } + + 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/DumpTruck/DumpTruck/FormCarCollection.resx b/DumpTruck/DumpTruck/FormCarCollection.resx index f298a7b..934ed35 100644 --- a/DumpTruck/DumpTruck/FormCarCollection.resx +++ b/DumpTruck/DumpTruck/FormCarCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormCarConfig.Designer.cs b/DumpTruck/DumpTruck/FormCarConfig.Designer.cs index 97e2163..f949a84 100644 --- a/DumpTruck/DumpTruck/FormCarConfig.Designer.cs +++ b/DumpTruck/DumpTruck/FormCarConfig.Designer.cs @@ -130,7 +130,7 @@ // panelPurple // this.panelPurple.AllowDrop = true; - this.panelPurple.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); + this.panelPurple.BackColor = System.Drawing.Color.Fuchsia; this.panelPurple.Location = new System.Drawing.Point(156, 106); this.panelPurple.Name = "panelPurple"; this.panelPurple.Size = new System.Drawing.Size(57, 53); @@ -139,7 +139,7 @@ // panelLightBlue // this.panelLightBlue.AllowDrop = true; - this.panelLightBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.panelLightBlue.BackColor = System.Drawing.Color.Cyan; this.panelLightBlue.Location = new System.Drawing.Point(13, 106); this.panelLightBlue.Name = "panelLightBlue"; this.panelLightBlue.Size = new System.Drawing.Size(57, 53); @@ -148,7 +148,7 @@ // panelYellow // this.panelYellow.AllowDrop = true; - this.panelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); + this.panelYellow.BackColor = System.Drawing.Color.Yellow; this.panelYellow.Location = new System.Drawing.Point(156, 35); this.panelYellow.Name = "panelYellow"; this.panelYellow.Size = new System.Drawing.Size(57, 53); @@ -157,7 +157,7 @@ // panelBlue // this.panelBlue.AllowDrop = true; - this.panelBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); + this.panelBlue.BackColor = System.Drawing.Color.Blue; this.panelBlue.Location = new System.Drawing.Point(86, 106); this.panelBlue.Name = "panelBlue"; this.panelBlue.Size = new System.Drawing.Size(57, 53); @@ -175,7 +175,7 @@ // panelOrange // this.panelOrange.AllowDrop = true; - this.panelOrange.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); + this.panelOrange.BackColor = System.Drawing.Color.Orange; this.panelOrange.Location = new System.Drawing.Point(86, 35); this.panelOrange.Name = "panelOrange"; this.panelOrange.Size = new System.Drawing.Size(57, 53); @@ -184,7 +184,7 @@ // panelRed // this.panelRed.AllowDrop = true; - this.panelRed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))); + this.panelRed.BackColor = System.Drawing.Color.Red; this.panelRed.Location = new System.Drawing.Point(13, 35); this.panelRed.Name = "panelRed"; this.panelRed.Size = new System.Drawing.Size(57, 53);