From cb38f8505d656bb140d0f15e7a15d38ab487806c Mon Sep 17 00:00:00 2001 From: kirin Date: Tue, 28 Nov 2023 12:26:51 +0400 Subject: [PATCH] all done --- .../DrawingElectricLocomotiv.cs | 8 +- .../ExtentionDrawingLocomotiv.cs | 53 ++++++++++ lab1/Entities/EntityElectricLocomotiv.cs | 6 +- lab1/FormLocoConfig.Designer.cs | 2 +- lab1/FormLocoConfig.cs | 5 +- lab1/FormLocomotiv.cs | 2 +- lab1/FormLocomotivCollection.Designer.cs | 62 ++++++++++- lab1/FormLocomotivCollection.cs | 39 ++++++- lab1/FormLocomotivCollection.resx | 9 ++ lab1/Generics/LocosGenericCollection.cs | 1 + lab1/Generics/LocosGenericStorage.cs | 100 +++++++++++++++++- 11 files changed, 268 insertions(+), 19 deletions(-) create mode 100644 lab1/DrawingObjects/ExtentionDrawingLocomotiv.cs diff --git a/lab1/DrawingObjects/DrawingElectricLocomotiv.cs b/lab1/DrawingObjects/DrawingElectricLocomotiv.cs index e0b5200..fe5d7fc 100644 --- a/lab1/DrawingObjects/DrawingElectricLocomotiv.cs +++ b/lab1/DrawingObjects/DrawingElectricLocomotiv.cs @@ -4,16 +4,16 @@ namespace ElectricLocomotive; public class DrawingElectricLocomotiv : DrawingLocomotiv { - private bool isBattery; - private bool isRoga; - public DrawingElectricLocomotiv(bool isBattery, bool isRoga, int speed, double weight, int width, int height, Color mainColor, Color dopColor, Color batteryColor, Color rogaColor) : base(speed, weight, width, + public bool isBattery; + public bool isRoga; + public DrawingElectricLocomotiv(int speed, double weight, int width, int height, Color mainColor, Color dopColor, Color batteryColor, Color rogaColor,bool isBattery, bool isRoga) : base(speed, weight, width, height, mainColor, dopColor) { this.isBattery = isBattery; this.isRoga = isRoga; if (EntityLocomotiv != null) { - EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, batteryColor, rogaColor, mainColor, dopColor); + EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, batteryColor, rogaColor, mainColor, dopColor, isRoga, isBattery); } } diff --git a/lab1/DrawingObjects/ExtentionDrawingLocomotiv.cs b/lab1/DrawingObjects/ExtentionDrawingLocomotiv.cs new file mode 100644 index 0000000..0a2bf63 --- /dev/null +++ b/lab1/DrawingObjects/ExtentionDrawingLocomotiv.cs @@ -0,0 +1,53 @@ +namespace ElectricLocomotive; + +public static class ExtentionDrawingLocomotiv +{ + public static DrawingLocomotiv? CreateDrawingLoco(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 4) + { + return new DrawingLocomotiv(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), width, height,Color.FromName(strs[2]), Color.FromName(strs[3])); + } + if (strs.Length == 8) + { + return new DrawingElectricLocomotiv( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + width, height, + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Color.FromName(strs[4]), + Color.FromName(strs[5]), + Convert.ToBoolean(strs[6]), + Convert.ToBoolean(strs[7]) + ); + } + return null; + } + + public static string GetDataForSave(this DrawingLocomotiv drawingLoco, + char separatorForObject) + { + var loco = drawingLoco.EntityLocomotiv; + if (loco == null) + { + return string.Empty; + } + var str = + $"{loco.Speed}{separatorForObject}{loco.Weight}" + + $"{separatorForObject}{loco.ColorBody.Name}" + + $"{separatorForObject}{loco.ColorWindow.Name}"; + if (loco is not EntityElectricLocomotiv electricLocomotiv) + { + return str; + } + return + $"{str}{separatorForObject}{electricLocomotiv.BatteryColor.Name}{separatorForObject}" + + $"{electricLocomotiv.RogaColor.Name}{separatorForObject}" + + $"{electricLocomotiv.isBattery}{separatorForObject}{electricLocomotiv.isRoga}"; + } + +} \ No newline at end of file diff --git a/lab1/Entities/EntityElectricLocomotiv.cs b/lab1/Entities/EntityElectricLocomotiv.cs index 28d0d89..0006009 100644 --- a/lab1/Entities/EntityElectricLocomotiv.cs +++ b/lab1/Entities/EntityElectricLocomotiv.cs @@ -4,9 +4,13 @@ public class EntityElectricLocomotiv : EntityLocomotiv { public Color BatteryColor { get; set; } public Color RogaColor { get; set; } - public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor, Color mainColor, Color dopColor) : base(speed, weight, mainColor, dopColor) + public bool isRoga; + public bool isBattery; + public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor, Color mainColor, Color dopColor, bool isRoga, bool isBattery) : base(speed, weight, mainColor, dopColor) { BatteryColor = batteryColor; RogaColor = rogaColor; + this.isRoga = isRoga; + this.isBattery = isBattery; } } \ No newline at end of file diff --git a/lab1/FormLocoConfig.Designer.cs b/lab1/FormLocoConfig.Designer.cs index 7f8bd81..34f58e5 100644 --- a/lab1/FormLocoConfig.Designer.cs +++ b/lab1/FormLocoConfig.Designer.cs @@ -189,7 +189,7 @@ // // panelRed // - panelRed.BackColor = Color.FromArgb(192, 0, 0); + panelRed.BackColor = Color.Red; panelRed.Location = new Point(6, 38); panelRed.Name = "panelRed"; panelRed.Size = new Size(60, 56); diff --git a/lab1/FormLocoConfig.cs b/lab1/FormLocoConfig.cs index 2439371..f4760e2 100644 --- a/lab1/FormLocoConfig.cs +++ b/lab1/FormLocoConfig.cs @@ -73,9 +73,8 @@ DragDropEffects.Move | DragDropEffects.Copy); pictureBox.Height, Color.Black, Color.Yellow); break; case "labelAdvanced": - _loco = new DrawingElectricLocomotiv(checkBoxRoga.Checked, checkBoxBattery.Checked, - (int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, pictureBox.Width, - pictureBox.Height, Color.Black, Color.Yellow, Color.Aqua, Color.Black); + _loco = new DrawingElectricLocomotiv((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value, pictureBox.Width, + pictureBox.Height, Color.Black, Color.Yellow, Color.Aqua, Color.Black,checkBoxRoga.Checked, checkBoxBattery.Checked); break; } labelColor.BackColor = Color.Empty; diff --git a/lab1/FormLocomotiv.cs b/lab1/FormLocomotiv.cs index bcbf3bf..25403a0 100644 --- a/lab1/FormLocomotiv.cs +++ b/lab1/FormLocomotiv.cs @@ -43,7 +43,7 @@ namespace ElectricLocomotive { if (dialog.ShowDialog() == DialogResult.OK) dopColor = dialog.Color; - _drawingLocomotiv = new DrawingElectricLocomotiv(true, true, random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor); + _drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor,true, true); _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } diff --git a/lab1/FormLocomotivCollection.Designer.cs b/lab1/FormLocomotivCollection.Designer.cs index 6487b8b..bd6df2a 100644 --- a/lab1/FormLocomotivCollection.Designer.cs +++ b/lab1/FormLocomotivCollection.Designer.cs @@ -38,9 +38,16 @@ partial class FormLocomotivCollection { locoIndexInput = new TextBox(); addLocomotiv = new Button(); collectionPictureBox = new PictureBox(); + menuStrip1 = new MenuStrip(); + toolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + loadFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); toolsBox.SuspendLayout(); collectionGroupBoxes.SuspendLayout(); ((ISupportInitialize)collectionPictureBox).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // toolsBox @@ -146,12 +153,52 @@ partial class FormLocomotivCollection { // // collectionPictureBox // - collectionPictureBox.Location = new Point(12, 12); + collectionPictureBox.Location = new Point(12, 38); collectionPictureBox.Name = "collectionPictureBox"; - collectionPictureBox.Size = new Size(933, 707); + collectionPictureBox.Size = new Size(933, 681); collectionPictureBox.TabIndex = 1; collectionPictureBox.TabStop = false; // + // 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(1265, 28); + menuStrip1.TabIndex = 3; + 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(166, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // LoadToolStripMenuItem + // + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(166, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // loadFileDialog + // + loadFileDialog.FileName = "openFileDialog4"; + loadFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // // FormLocomotivCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -159,6 +206,8 @@ partial class FormLocomotivCollection { ClientSize = new Size(1265, 731); Controls.Add(collectionPictureBox); Controls.Add(toolsBox); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Name = "FormLocomotivCollection"; Text = "Набор локомотивов"; toolsBox.ResumeLayout(false); @@ -166,7 +215,10 @@ partial class FormLocomotivCollection { collectionGroupBoxes.ResumeLayout(false); collectionGroupBoxes.PerformLayout(); ((ISupportInitialize)collectionPictureBox).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -182,4 +234,10 @@ partial class FormLocomotivCollection { private Button addStorageButton; private TextBox storageIndexInput; private ListBox storageListBox; + private MenuStrip menuStrip1; + private ToolStripMenuItem toolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog loadFileDialog; + private SaveFileDialog saveFileDialog; } \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.cs b/lab1/FormLocomotivCollection.cs index 1020bd1..e231097 100644 --- a/lab1/FormLocomotivCollection.cs +++ b/lab1/FormLocomotivCollection.cs @@ -1,4 +1,6 @@ -namespace ElectricLocomotive; +using System.Windows.Forms; + +namespace ElectricLocomotive; public partial class FormLocomotivCollection : Form { private readonly LocosGenericStorage _storage; @@ -34,14 +36,12 @@ public partial class FormLocomotivCollection : Form { form.Show(); Action? monorailDelegate = new((m) => { bool q = (obj + m); - if (q) - { + if (q) { MessageBox.Show("Объект добавлен"); m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height); collectionPictureBox.Image = obj.ShowLocos(); } - else - { + else { MessageBox.Show("Не удалось добавить объект"); } }); @@ -108,4 +108,33 @@ public partial class FormLocomotivCollection : Form { _storage.AddSet(storageIndexInput.Text); ReloadObjects(); } + + 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 (loadFileDialog.ShowDialog() == DialogResult.OK) { + if (_storage.LoadData(loadFileDialog.FileName)) { + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + foreach (var collection in _storage.Keys) { + storageListBox.Items.Add(collection); + } + } + else { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.resx b/lab1/FormLocomotivCollection.resx index af32865..7953710 100644 --- a/lab1/FormLocomotivCollection.resx +++ b/lab1/FormLocomotivCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 323, 17 + + + 481, 17 + \ No newline at end of file diff --git a/lab1/Generics/LocosGenericCollection.cs b/lab1/Generics/LocosGenericCollection.cs index b98f55a..a0b349c 100644 --- a/lab1/Generics/LocosGenericCollection.cs +++ b/lab1/Generics/LocosGenericCollection.cs @@ -7,6 +7,7 @@ public class LocosGenericCollection where T : DrawingLocomotiv where U : private readonly int _placeSizeWidth = 170; private readonly int _placeSizeHeight = 110; private readonly SetGeneric _collection; + public IEnumerable GetLocos => _collection.GetElectricLocos(); public LocosGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; diff --git a/lab1/Generics/LocosGenericStorage.cs b/lab1/Generics/LocosGenericStorage.cs index cf669f5..5d175ec 100644 --- a/lab1/Generics/LocosGenericStorage.cs +++ b/lab1/Generics/LocosGenericStorage.cs @@ -1,6 +1,8 @@ -namespace ElectricLocomotive; +using System.Text; -public class LocosGenericStorage +namespace ElectricLocomotive; + +public class LocosGenericStorage { readonly Dictionary> _electricLocoStorages; public List Keys => _electricLocoStorages.Keys.ToList(); @@ -8,6 +10,11 @@ public class LocosGenericStorage private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + + private readonly char _separatorRecords = ';'; + + private static readonly char _separatorForObject = ':'; public LocosGenericStorage(int pictureWidth, int pictureHeight) { @@ -38,4 +45,93 @@ public class LocosGenericStorage return null; } } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _electricLocoStorages) + { + StringBuilder records = new(); + foreach (DrawingLocomotiv? elem in record.Value.GetLocos) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + + if (data.Length == 0) + { + return false; + } + string toWrite = $"LocoStorage{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("LocoStorage")) + { + return false; + } + _electricLocoStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + LocosGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotiv? monorail = + elem?.CreateDrawingLoco(_separatorForObject, _pictureWidth, _pictureHeight); + if (monorail != null) + { + if (!(collection + monorail)) + { + return false; + } + } + } + _electricLocoStorages.Add(record[0], collection); + + str = sr.ReadLine(); + } while (str != null); + + } + return true; + } + } \ No newline at end of file -- 2.25.1