diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs index b7600f7..a1fba9a 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs @@ -21,6 +21,7 @@ namespace ProjectAirplaneWithRadar.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } + public IEnumerable GetAirplanes => _collection.GetAirplanes(); public static bool operator +(AirplanesGenericCollection collect, T? obj) { if (obj == null || collect == null) diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs index b9c169d..91c2fe2 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs @@ -15,30 +15,114 @@ namespace ProjectAirplaneWithRadar.Generics public List Keys => _airplanesStorages.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 AirplanesGenericStorage(int pictureWidth, int pictureHeight) { _airplanesStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } - public void AddSet(string name) + public bool SaveData(string filename) { - _airplanesStorages.Add(name, new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); - } - public void DelSet(string name) - { - if (!_airplanesStorages.ContainsKey(name)) - return; - _airplanesStorages.Remove(name); - } - public AirplanesGenericCollection?this[string ind] - { - get + if (File.Exists(filename)) { - if (_airplanesStorages.ContainsKey(ind)) - return _airplanesStorages[ind]; - return null; + File.Delete(filename); } + StringBuilder data = new(); + foreach (KeyValuePair> record in _airplanesStorages) + { + StringBuilder records = new(); + foreach (DrawningAirplane? elem in record.Value.GetAirplanes) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + string toWrite = $"AirplanesStorage{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("AirplanesStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _airplanesStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + AirplanesGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningAirplane? airplane = + elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection + airplane)) + { + return false; + } + } + } + _airplanesStorages.Add(record[0], collection); + str = sr.ReadLine(); + } while (str != null); + } + return true; + } + public void AddSet(string name) + { + _airplanesStorages.Add(name, new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (!_airplanesStorages.ContainsKey(name)) + return; + _airplanesStorages.Remove(name); + } + public AirplanesGenericCollection?this[string ind] + { + get + { + if (_airplanesStorages.ContainsKey(ind)) + return _airplanesStorages[ind]; + return null; + } + } } } diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs new file mode 100644 index 0000000..1b014fc --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/ExtentionDrawningAirplane.cs @@ -0,0 +1,50 @@ +using ProjectAirplaneWithRadar.Entities; +using ProjectAirplaneWithRadar.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar +{ + public static class ExtentionDrawningAirplane + { + public static DrawningAirplane? CreateDrawningAirplane(this string info, char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningAirplane(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningAirplaneWithRadar(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 DrawningAirplane drawningAirplane,char separatorForObject) + { + var airplane = drawningAirplane.EntityAirplane; + if (airplane == null) + { + return string.Empty; + } + var str = $"{airplane.Speed}{separatorForObject}{airplane.Weight}{separatorForObject}{airplane.BodyColor.Name}"; + if (airplane is not EntityAirplaneWithRadar airplaneWithRadar) + { + return str; + } + return + $"{str}{separatorForObject}{airplaneWithRadar.AdditionalColor.Name}" + + $"{separatorForObject}{airplaneWithRadar.Radar}" + + $"{separatorForObject}{airplaneWithRadar.DopBak}"; + } + } +} + diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs index 21b5bdb..4496e0a 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs @@ -39,14 +39,21 @@ buttonDeleteAirplane = new Button(); buttonAddAirplane = new Button(); maskedTextBoxNumber = new MaskedTextBox(); + menuStrip1 = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).BeginInit(); groupBoxAirplaneWithRadar.SuspendLayout(); groupBoxCollection.SuspendLayout(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // pictureBoxAirplanesCollection // - pictureBoxAirplanesCollection.Location = new Point(-9, 0); + pictureBoxAirplanesCollection.Location = new Point(0, 38); pictureBoxAirplanesCollection.Name = "pictureBoxAirplanesCollection"; pictureBoxAirplanesCollection.Size = new Size(650, 454); pictureBoxAirplanesCollection.SizeMode = PictureBoxSizeMode.AutoSize; @@ -62,9 +69,9 @@ groupBoxAirplaneWithRadar.Controls.Add(buttonAddAirplane); groupBoxAirplaneWithRadar.Controls.Add(maskedTextBoxNumber); groupBoxAirplaneWithRadar.Dock = DockStyle.Right; - groupBoxAirplaneWithRadar.Location = new Point(640, 0); + groupBoxAirplaneWithRadar.Location = new Point(650, 28); groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar"; - groupBoxAirplaneWithRadar.Size = new Size(250, 453); + groupBoxAirplaneWithRadar.Size = new Size(251, 464); groupBoxAirplaneWithRadar.TabIndex = 1; groupBoxAirplaneWithRadar.TabStop = false; groupBoxAirplaneWithRadar.Text = "Инструменты"; @@ -156,13 +163,49 @@ maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 0; // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(901, 28); + menuStrip1.TabIndex = 2; + menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(224, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(224, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + // // FormAirplanesCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(890, 453); + ClientSize = new Size(901, 492); Controls.Add(groupBoxAirplaneWithRadar); Controls.Add(pictureBoxAirplanesCollection); + Controls.Add(menuStrip1); Name = "FormAirplanesCollection"; Text = "FormAirplaneWithRadar"; ((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).EndInit(); @@ -170,6 +213,8 @@ groupBoxAirplaneWithRadar.PerformLayout(); groupBoxCollection.ResumeLayout(false); groupBoxCollection.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -187,5 +232,11 @@ private ListBox listBoxStorages; private Button buttonAddObject; private TextBox textBoxStorageName; + 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/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs index a3ea417..f6b043d 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs @@ -2,6 +2,7 @@ using ProjectAirplaneWithRadar.DrawningObjects; using ProjectAirplaneWithRadar.MovementStrategy; using ProjectAirplaneWithRadar.Generics; using System.Diagnostics.Metrics; +using System.Windows.Forms; namespace ProjectAirplaneWithRadar { @@ -47,7 +48,8 @@ namespace ProjectAirplaneWithRadar } FormAirplaneConfig form = new(pictureBoxAirplanesCollection.Width, pictureBoxAirplanesCollection.Height); form.Show(); - Action < DrawningAirplane >? airplaneDelegate = new((m) => { + Action? airplaneDelegate = new((m) => + { bool q = (obj + m); if (q) { @@ -138,5 +140,41 @@ namespace ProjectAirplaneWithRadar { } + private void SaveToolStripMenuItem_Click_1(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); + } + } + } } } \ No newline at end of file diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.resx b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.resx index af32865..b9db4d6 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.resx +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 153, 17 + + + 323, 17 + \ No newline at end of file