diff --git a/Bulldoser/Bulldoser/Drawings/ExtentionDrawningTractor.cs b/Bulldoser/Bulldoser/Drawings/ExtentionDrawningTractor.cs new file mode 100644 index 0000000..3ed3502 --- /dev/null +++ b/Bulldoser/Bulldoser/Drawings/ExtentionDrawningTractor.cs @@ -0,0 +1,52 @@ +using Bulldoser.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldoser.Drawings +{ + internal static class ExtentionDrawningTractor + { + public static DrawingTractor? CreateDrawingTractor(this string info, + char separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingTractor(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), width, height); + } + if (strs.Length == 7) + { + return new DrawingBulldoser(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 DrawingTractor drawingTractor, + char separatorForObject) + { + var tractor = drawingTractor.EntityTractor; + if (tractor == null) + { + return string.Empty; + } + var str = + $"{tractor.Speed}{separatorForObject}{tractor.Weight}{separatorForObject}{tractor.BodyColor.Name}"; + if (tractor is not EntityBulldoser bulldozer) + { + return str; + } + return + $"{str}{separatorForObject}{bulldozer.AdditionalColor.Name}{separatorForObject}" + + $"{bulldozer.SeifBatteries}{separatorForObject}{bulldozer.Otval}{separatorForObject}"; + } + } +} diff --git a/Bulldoser/Bulldoser/Form2.Designer.cs b/Bulldoser/Bulldoser/Form2.Designer.cs index dd7d460..8adccd4 100644 --- a/Bulldoser/Bulldoser/Form2.Designer.cs +++ b/Bulldoser/Bulldoser/Form2.Designer.cs @@ -39,17 +39,24 @@ buttonUpdateCollection = new Button(); buttonRemoveTransport = new Button(); buttonAddTransport = new Button(); + menuStrip = new MenuStrip(); + toolStripMenuItem = new ToolStripMenuItem(); + SavetoolStripMenuItem = new ToolStripMenuItem(); + LoadtoolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).BeginInit(); groupBoxTools.SuspendLayout(); groupBox1.SuspendLayout(); + menuStrip.SuspendLayout(); SuspendLayout(); // // pictureBoxCollections // pictureBoxCollections.Dock = DockStyle.Left; - pictureBoxCollections.Location = new Point(0, 0); + pictureBoxCollections.Location = new Point(0, 24); pictureBoxCollections.Name = "pictureBoxCollections"; - pictureBoxCollections.Size = new Size(588, 450); + pictureBoxCollections.Size = new Size(588, 426); pictureBoxCollections.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxCollections.TabIndex = 1; pictureBoxCollections.TabStop = false; @@ -61,9 +68,9 @@ groupBoxTools.Controls.Add(buttonUpdateCollection); groupBoxTools.Controls.Add(buttonRemoveTransport); groupBoxTools.Controls.Add(buttonAddTransport); - groupBoxTools.Location = new Point(594, 6); + groupBoxTools.Location = new Point(594, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 432); + groupBoxTools.Size = new Size(200, 414); groupBoxTools.TabIndex = 2; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -76,7 +83,7 @@ groupBox1.Controls.Add(InputNabor); groupBox1.Location = new Point(6, 22); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(188, 223); + groupBox1.Size = new Size(188, 198); groupBox1.TabIndex = 6; groupBox1.TabStop = false; groupBox1.Text = "Наборы"; @@ -87,13 +94,13 @@ listBoxStorage.ItemHeight = 15; listBoxStorage.Location = new Point(6, 85); listBoxStorage.Name = "listBoxStorage"; - listBoxStorage.Size = new Size(176, 94); + listBoxStorage.Size = new Size(176, 64); listBoxStorage.TabIndex = 10; listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; // // DelNabor // - DelNabor.Location = new Point(6, 187); + DelNabor.Location = new Point(6, 155); DelNabor.Name = "DelNabor"; DelNabor.Size = new Size(176, 30); DelNabor.TabIndex = 9; @@ -122,7 +129,7 @@ // // maskedTextBoxNumbers // - maskedTextBoxNumbers.Location = new Point(6, 312); + maskedTextBoxNumbers.Location = new Point(6, 278); maskedTextBoxNumbers.Margin = new Padding(3, 2, 3, 2); maskedTextBoxNumbers.Mask = "0"; maskedTextBoxNumbers.Name = "maskedTextBoxNumbers"; @@ -131,7 +138,7 @@ // // buttonUpdateCollection // - buttonUpdateCollection.Location = new Point(6, 386); + buttonUpdateCollection.Location = new Point(6, 352); buttonUpdateCollection.Name = "buttonUpdateCollection"; buttonUpdateCollection.Size = new Size(188, 40); buttonUpdateCollection.TabIndex = 3; @@ -141,7 +148,7 @@ // // buttonRemoveTransport // - buttonRemoveTransport.Location = new Point(6, 340); + buttonRemoveTransport.Location = new Point(6, 306); buttonRemoveTransport.Name = "buttonRemoveTransport"; buttonRemoveTransport.Size = new Size(188, 40); buttonRemoveTransport.TabIndex = 2; @@ -151,7 +158,7 @@ // // buttonAddTransport // - buttonAddTransport.Location = new Point(6, 260); + buttonAddTransport.Location = new Point(6, 226); buttonAddTransport.Name = "buttonAddTransport"; buttonAddTransport.Size = new Size(188, 47); buttonAddTransport.TabIndex = 0; @@ -159,6 +166,46 @@ buttonAddTransport.UseVisualStyleBackColor = true; buttonAddTransport.Click += ButtonAddTractor_Click; // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(800, 24); + menuStrip.TabIndex = 3; + menuStrip.Text = "menuStrip1"; + // + // toolStripMenuItem + // + toolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SavetoolStripMenuItem, LoadtoolStripMenuItem }); + toolStripMenuItem.Name = "toolStripMenuItem"; + toolStripMenuItem.Size = new Size(48, 20); + toolStripMenuItem.Text = "Файл"; + // + // SavetoolStripMenuItem + // + SavetoolStripMenuItem.Name = "SavetoolStripMenuItem"; + SavetoolStripMenuItem.Size = new Size(180, 22); + SavetoolStripMenuItem.Text = "Сохранить"; + SavetoolStripMenuItem.Click += SavetoolStripMenuItem_Click; + // + // LoadtoolStripMenuItem + // + LoadtoolStripMenuItem.Name = "LoadtoolStripMenuItem"; + LoadtoolStripMenuItem.Size = new Size(180, 22); + LoadtoolStripMenuItem.Text = "Загрузить"; + LoadtoolStripMenuItem.Click += LoadtoolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog"; + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.FileName = "saveFileDialog"; + saveFileDialog.Filter = "txt file | *.txt"; + // // FormTractorCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -166,6 +213,8 @@ ClientSize = new Size(800, 450); Controls.Add(groupBoxTools); Controls.Add(pictureBoxCollections); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormTractorCollection"; Text = "Набор автомобилей"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).EndInit(); @@ -173,6 +222,8 @@ groupBoxTools.PerformLayout(); groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -190,5 +241,11 @@ private Button DelNabor; private Button AddNabor; private MaskedTextBox InputNabor; + private MenuStrip menuStrip; + private ToolStripMenuItem toolStripMenuItem; + private ToolStripMenuItem SavetoolStripMenuItem; + private ToolStripMenuItem LoadtoolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Bulldoser/Bulldoser/Form2.cs b/Bulldoser/Bulldoser/Form2.cs index bcd0af9..1d4543c 100644 --- a/Bulldoser/Bulldoser/Form2.cs +++ b/Bulldoser/Bulldoser/Form2.cs @@ -83,8 +83,8 @@ namespace Bulldoser form.Show(); Action? TractorDelegate = new((m) => { - bool q = (obj + m); - if (q) + int q = (obj + m); + if (q != -1 && q < 10) { MessageBox.Show("Объект добавлен"); pictureBoxCollections.Image = obj.ShowTractors(); @@ -131,5 +131,41 @@ namespace Bulldoser } pictureBoxCollections.Image = obj.ShowTractors(); } + + 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); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + ReloadObjects(); + } + } } + } diff --git a/Bulldoser/Bulldoser/Form2.resx b/Bulldoser/Bulldoser/Form2.resx index af32865..3940e8d 100644 --- a/Bulldoser/Bulldoser/Form2.resx +++ b/Bulldoser/Bulldoser/Form2.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 126, 17 + + + 265, 17 + \ No newline at end of file diff --git a/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs b/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs index 8dd0fb4..1775edf 100644 --- a/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs +++ b/Bulldoser/Bulldoser/Generic/BulldoserGenericCollection.cs @@ -19,6 +19,7 @@ namespace Bulldoser.Generic private readonly int _placeSizeHeight = 130; /// Набор объектов private readonly SetGeneric _collection; + public IEnumerable GetTractors => _collection.GetTractors(); public TractorGenericCollection(int picWidth, int picHeight) { // высчитываем размер массива для setgeneric @@ -29,13 +30,13 @@ namespace Bulldoser.Generic _collection = new SetGeneric(width * height); } /// Перегрузка оператора сложения - public static bool operator +(TractorGenericCollection collect, T tract) + public static int operator +(TractorGenericCollection collect, T tract) { if (tract == null) { - return false; + return -1; } - return collect?._collection.Insert(tract)??false; + return collect._collection.Insert(tract); } public static T? operator -(TractorGenericCollection collect, int pos) diff --git a/Bulldoser/Bulldoser/Generic/SetGeneric.cs b/Bulldoser/Bulldoser/Generic/SetGeneric.cs index 565f5c1..0de977f 100644 --- a/Bulldoser/Bulldoser/Generic/SetGeneric.cs +++ b/Bulldoser/Bulldoser/Generic/SetGeneric.cs @@ -16,17 +16,15 @@ namespace Bulldoser.Generic _places = new List(count); _maxCount = count; } - public bool Insert(T tract) + public int Insert(T tract) { - if (_places.Count == _maxCount) return false; - Insert(tract, 0 ); - return true; + return Insert(tract, 0 ); } - public bool Insert(T tract, int position) + public int Insert(T tract, int position) { - if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return -1; _places.Insert(position, tract); - return true; + return position; } public T? Remove(int position) diff --git a/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs b/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs index 396bd72..2b0b283 100644 --- a/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs +++ b/Bulldoser/Bulldoser/Generic/TractorGenericStorage.cs @@ -2,6 +2,7 @@ using Bulldoser.Drawings; using Bulldoser.Generic; using Bulldoser.MovementStrategy; +using System.Text; namespace Bulldozer.Generics { @@ -11,6 +12,10 @@ namespace Bulldozer.Generics public List Keys => _TractorsStorage.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 TractorGenericStorage(int pictureWidth, int pictureHeight) { _TractorsStorage = new Dictionary>(); @@ -31,8 +36,7 @@ namespace Bulldozer.Generics _TractorsStorage.Remove(name); } } - public TractorGenericCollection? - this[string ind] + public TractorGenericCollection? this[string ind] { get { @@ -43,5 +47,84 @@ namespace Bulldozer.Generics return null; } } - } + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + + foreach (KeyValuePair> + record in _TractorsStorage) + { + StringBuilder records = new(); + foreach (DrawingTractor? elem in record.Value.GetTractors) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using StreamWriter sw = new(filename); + sw.Write($"TractorsStorage{Environment.NewLine}{data}"); + return true; + } + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader sr = File.OpenText(filename)) + { + string str = sr.ReadLine(); + if (str == null || str.Length == 0) + { + return false; + } + if (!str.StartsWith("TractorsStorage")) + { + return false; + } + + _TractorsStorage.Clear(); + string strs = ""; + + while ((strs = sr.ReadLine()) != null) + { + if (strs == null) + { + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + TractorGenericCollection collection = + new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingTractor? tractor = elem?.CreateDrawingTractor(_separatorForObject, + _pictureWidth, _pictureHeight); + if (tractor != null) + { + if ((collection + tractor) == -1) + { + return false; + } + } + } + _TractorsStorage.Add(record[0], collection); + } + return true; + } + } } + } \ No newline at end of file