From 6ead695eac8695209ee239ccb16dd419a9e69798 Mon Sep 17 00:00:00 2001 From: YourDax Date: Mon, 27 Nov 2023 21:23:03 +0400 Subject: [PATCH 1/2] lab6 think done --- .../ExtentionDrawningAntiAirCraftGun.cs | 46 +++++++++ .../FormAntiAirCraftGunCollection.Designer.cs | 66 ++++++++++++- .../FormAntiAirCraftGunCollection.cs | 47 +++++++++ .../FormAntiAirCraftGunCollection.resx | 9 ++ .../AntiAirCraftGunGenericCollection.cs | 4 + .../Generics/AntiAirCraftGunGenericStorage.cs | 98 +++++++++++++++++++ 6 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/ExtentionDrawningAntiAirCraftGun.cs diff --git a/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/ExtentionDrawningAntiAirCraftGun.cs b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/ExtentionDrawningAntiAirCraftGun.cs new file mode 100644 index 0000000..a445aff --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/DrawingObjects/ExtentionDrawningAntiAirCraftGun.cs @@ -0,0 +1,46 @@ +using AntiAircraftGun.Enitites; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.DrawingObjects +{ + public static class ExtentionDrawningAntiAirCraftGun + { + public static BaseDrawingAntiAirCraftGun? CreateDrawningAntiAirCraftGun(this string info, char separatorForObject,int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 4) + { + return new BaseDrawingAntiAirCraftGun(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), + width, height); + } + if (strs.Length == 7) + { + return new AdvancedDrawingAntiAirCraftGun(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), + Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), width, height); + } + return null; + } + + public static string GetDataForSave(this BaseDrawingAntiAirCraftGun drawningAntiAirCraftGun, char separatorForObject) + { + var zenit = drawningAntiAirCraftGun.AntiAirСraftGun; + if (zenit == null) + { + return string.Empty; + } + var str = $"{zenit.Speed}{separatorForObject}{zenit.Weight}{separatorForObject}{zenit.BodyColor.Name}{separatorForObject}{zenit.AdditionalColor.Name}"; + if (zenit is not EntityAdvancedAntiAirCraftGun antiAirCraftGun) + { + return str; + } + return + $"{str}{separatorForObject}{antiAirCraftGun.DopColor.Name}{separatorForObject}{antiAirCraftGun.Radar}{separatorForObject}{antiAirCraftGun.Rocket}"; + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs index cd568ed..e3684cc 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs @@ -39,14 +39,21 @@ this.buttonUpdateColletion = new System.Windows.Forms.Button(); this.buttonDeleteZenit = new System.Windows.Forms.Button(); this.buttonAddZenit = new System.Windows.Forms.Button(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = 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.panelTools.SuspendLayout(); this.panel1.SuspendLayout(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(12, 12); + this.pictureBoxCollection.Location = new System.Drawing.Point(12, 50); this.pictureBoxCollection.Name = "pictureBoxCollection"; this.pictureBoxCollection.Size = new System.Drawing.Size(711, 571); this.pictureBoxCollection.TabIndex = 0; @@ -59,7 +66,7 @@ this.panelTools.Controls.Add(this.buttonUpdateColletion); this.panelTools.Controls.Add(this.buttonDeleteZenit); this.panelTools.Controls.Add(this.buttonAddZenit); - this.panelTools.Location = new System.Drawing.Point(729, 14); + this.panelTools.Location = new System.Drawing.Point(729, 50); this.panelTools.Name = "panelTools"; this.panelTools.Size = new System.Drawing.Size(234, 568); this.panelTools.TabIndex = 1; @@ -151,13 +158,57 @@ this.buttonAddZenit.UseVisualStyleBackColor = true; this.buttonAddZenit.Click += new System.EventHandler(this.ButtonAddZenit_Click); // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(971, 24); + this.menuStrip1.TabIndex = 2; + this.menuStrip1.Text = "File"; + // + // toolStripMenuItem + // + this.toolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.toolStripMenuItem.Name = "toolStripMenuItem"; + this.toolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.toolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.SaveToolStripMenuItem.Text = "Сохранить"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.LoadToolStripMenuItem.Text = "Загрузить"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog"; + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormAntiAirCraftGunCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(971, 595); + this.ClientSize = new System.Drawing.Size(971, 633); this.Controls.Add(this.panelTools); this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormAntiAirCraftGunCollection"; this.Text = "FormAntiAirCraftGunCollection"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); @@ -165,7 +216,10 @@ this.panelTools.PerformLayout(); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -182,5 +236,11 @@ private ListBox listBoxStorages; private Button buttonAddCollection; private Button buttonDelCollection; + private MenuStrip menuStrip1; + private ToolStripMenuItem toolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs index 724e5ca..066dab0 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs @@ -165,5 +165,52 @@ namespace AntiAircraftGun pictureBoxCollection.Image = obj.ShowZenits(); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + 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/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.resx b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.resx index f298a7b..ddcfdb1 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.resx +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 272, 17 + \ No newline at end of file diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs index 2df4441..0dd9a20 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs @@ -12,6 +12,10 @@ namespace AntiAircraftGun.Generics where T : BaseDrawingAntiAirCraftGun where U : IMoveableObject { + /// + /// Получение объектов коллекции + /// + public IEnumerable GetCars => _collection.GetZenits(); /// /// Ширина окна прорисовки /// diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs index 3eb2608..30b7fae 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs @@ -13,6 +13,18 @@ namespace AntiAircraftGun.Generics /// internal class AntiAirCraftGunGenericStorage { + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; /// /// Словарь (хранилище) /// @@ -91,5 +103,91 @@ namespace AntiAircraftGun.Generics } } } + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _zenitStorages) + { + StringBuilder records = new(); + foreach (BaseDrawingAntiAirCraftGun? elem in record.Value.GetCars) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + 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; + } + _zenitStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + AntiAirCraftGunGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + BaseDrawingAntiAirCraftGun? zenit = + elem?.CreateDrawningAntiAirCraftGun(_separatorForObject, _pictureWidth, _pictureHeight); + if (zenit != null) + { + if (!(collection + zenit)) + { + return false; + } + } + } + _zenitStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); + } + return true; + } } } + \ No newline at end of file -- 2.25.1 From 3914bd51bb9a30bdef71db054fb5d7c6bb2ce86c Mon Sep 17 00:00:00 2001 From: YourDax Date: Tue, 28 Nov 2023 12:06:24 +0400 Subject: [PATCH 2/2] fix with streamreader --- .../Generics/AntiAirCraftGunGenericCollection.cs | 2 +- .../Generics/AntiAirCraftGunGenericStorage.cs | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs index 0dd9a20..35d8926 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs @@ -130,7 +130,7 @@ namespace AntiAircraftGun.Generics foreach (var zenit in _collection.GetZenits()) { - // TODO: Получение объекта + T? obj = zenit; if (obj != null) diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs index 30b7fae..c6bbbec 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs @@ -129,11 +129,16 @@ namespace AntiAircraftGun.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); - return true; + 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; } /// /// Загрузка информации по автомобилям в хранилище из файла -- 2.25.1