From 86820b55e1e9fba5d9b93973533143ca71f94485 Mon Sep 17 00:00:00 2001 From: allllen4a Date: Mon, 20 Nov 2023 18:32:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A8=D0=B5=D1=81=D1=82=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusesGenericCollection.cs | 13 +-- .../BusesGenericStorage.cs | 93 +++++++++++++++++++ .../ExtentionDrawningBus.cs | 69 ++++++++++++++ .../FormBusCollection.Designer.cs | 63 ++++++++++++- .../FormBusCollection.cs | 37 +++++++- .../FormBusCollection.resx | 9 ++ .../projectDoubleDeckerBus/SetGeneric.cs | 35 +++---- 7 files changed, 288 insertions(+), 31 deletions(-) create mode 100644 projectDoubleDeckerBus/projectDoubleDeckerBus/ExtentionDrawningBus.cs diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs index 36183a7..1d4912c 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericCollection.cs @@ -53,17 +53,16 @@ namespace projectDoubleDeckerBus /// /// /// - //преобразовали bool в int - public static int operator +(BusesGenericCollection collect, T obj) + + public static bool operator +(BusesGenericCollection collect, T? + obj) { if (obj == null) { - return -1; + return false; } - - return collect?._collection.Insert(obj) ?? -1; + return (bool)collect?._collection.Insert(obj); } - /// Перегрузка оператора вычитания public static T? operator -(BusesGenericCollection collect, int pos) { @@ -131,5 +130,7 @@ namespace projectDoubleDeckerBus i++; } } + public IEnumerable GetBuses => _collection.GetBuses(); + } } diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericStorage.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericStorage.cs index 2687059..e534346 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericStorage.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/BusesGenericStorage.cs @@ -15,6 +15,17 @@ namespace projectDoubleDeckerBus.Generics public List Keys => _busStorages.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 BusesGenericStorage(int pictureWidth, int pictureHeight) { _busStorages = new Dictionary>(); @@ -43,5 +54,87 @@ namespace projectDoubleDeckerBus.Generics } } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _busStorages) + { + StringBuilder records = new(); + foreach (DrawningBus? elem in record.Value.GetBuses) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + + } + if (data.Length == 0) + { + return false; + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.WriteLine("BusStorage"); + writer.Write(data.ToString()); + return true; + } + + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + using (StreamReader reader = new StreamReader(filename)) + { + string checker = reader.ReadLine(); + if (checker == null) + return false; + if (!checker.StartsWith("BusStorage")) + return false; + _busStorages.Clear(); + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) + { + if (strs == null && firstinit) + return false; + if (strs == null) + break; + firstinit = false; + string name = strs.Split('|')[0]; + BusesGenericCollection collection = new(_pictureWidth, _pictureHeight); + foreach (string data in strs.Split('|')[1].Split(';')) + { + DrawningBus? bus = + data?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight); + if (bus != null) + { + if (!(collection + bus)) + { + return false; + } + } + } + _busStorages.Add(name, collection); + } + return true; + } + } } + } + + + diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/ExtentionDrawningBus.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/ExtentionDrawningBus.cs new file mode 100644 index 0000000..eac8979 --- /dev/null +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/ExtentionDrawningBus.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using projectDoubleDeckerBus.Entity; +using projectDoubleDeckerBus.Drawings; + + + +namespace projectDoubleDeckerBus +{ + public static class ExtentionDrawningBus + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawningBus? CreateDrawningBus(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningBus(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningDoubleDeckerBus(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 DrawningBus drawningBus, + char separatorForObject) + { + var bus = drawningBus.EntityBus; + if (bus == null) + { + return string.Empty; + } + var str = + $"{bus.Speed}{separatorForObject}{bus.Weight}{separatorForObject}{bus.BodyColor.Name}"; + if (bus is not EntityDoubleDeckerBus doubleDeckerBus) + { + return str; + } + return + $"{str}{separatorForObject}{doubleDeckerBus.AdditionalColor.Name}{separatorForObject}{doubleDeckerBus.Floor}{separatorForObject}{doubleDeckerBus.Tailpipe}"; + } + } +} + diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs index 35c8682..47593d9 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.Designer.cs @@ -40,8 +40,15 @@ ButtonAddCar = new Button(); labelTools = new Label(); DrawBus = new PictureBox(); + menuStrip1 = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadingToolStripMenuItem = new ToolStripMenuItem(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DrawBus).BeginInit(); + menuStrip1.SuspendLayout(); SuspendLayout(); // // panel1 @@ -58,9 +65,9 @@ panel1.Controls.Add(labelTools); panel1.Controls.Add(DrawBus); panel1.Dock = DockStyle.Fill; - panel1.Location = new Point(0, 0); + panel1.Location = new Point(0, 24); panel1.Name = "panel1"; - panel1.Size = new Size(813, 508); + panel1.Size = new Size(813, 484); panel1.TabIndex = 0; // // labelSet @@ -160,22 +167,66 @@ DrawBus.Dock = DockStyle.Fill; DrawBus.Location = new Point(0, 0); DrawBus.Name = "DrawBus"; - DrawBus.Size = new Size(813, 508); + DrawBus.Size = new Size(813, 484); DrawBus.TabIndex = 0; DrawBus.TabStop = false; // + // menuStrip1 + // + menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(813, 24); + menuStrip1.TabIndex = 1; + menuStrip1.Text = "menuStrip1"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadingToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(37, 20); + fileToolStripMenuItem.Text = "File"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.Size = new Size(180, 22); + saveToolStripMenuItem.Text = "Save"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadingToolStripMenuItem + // + loadingToolStripMenuItem.Name = "loadingToolStripMenuItem"; + loadingToolStripMenuItem.Size = new Size(180, 22); + loadingToolStripMenuItem.Text = "Load"; + loadingToolStripMenuItem.Click += LoadingToolStripMenuItem_Click; + // + // openFileDialog + // + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // // FormBusCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(813, 508); Controls.Add(panel1); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Name = "FormBusCollection"; Text = "FormBusCollection"; panel1.ResumeLayout(false); panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)DrawBus).EndInit(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -193,5 +244,11 @@ private Button ButtonRemoveBus; private TextBox maskedTextBoxNumber; private Label labelSet; + private MenuStrip menuStrip1; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadingToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs index a7c69d0..7360f4d 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.cs @@ -53,16 +53,12 @@ namespace projectDoubleDeckerBus private void AddBus(DrawningBus bus) { - if (listBoxStorages.SelectedIndex == -1) - { - return; - } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } - if ((obj + bus) != -1) + if (obj + bus) { MessageBox.Show("Объект добавлен"); DrawBus.Image = obj.ShowBuses(); @@ -192,5 +188,36 @@ namespace projectDoubleDeckerBus } } + + 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 LoadingToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog.FileName)) + { + ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx index af32865..8a67c96 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/FormBusCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 271, 17 + \ No newline at end of file diff --git a/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs b/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs index a3b1468..f635513 100644 --- a/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs +++ b/projectDoubleDeckerBus/projectDoubleDeckerBus/SetGeneric.cs @@ -35,31 +35,25 @@ namespace projectDoubleDeckerBus.Generics /// /// Добавляемый автобус /// - public int Insert(T bus) + public bool Insert(T bus) { - if (_places.Count >= _maxCount) - return -1; - _places.Insert(0, bus); - return 0; + return Insert(bus, 0); } public bool Insert(T bus, int position) { - if (_places.Count >= _maxCount) + if (position < 0 || position >= _maxCount) return false; - if (position < 0 || position > _places.Count) + if (Count >= _maxCount) return false; - - if (position == _places.Count) - _places.Add(bus); - else - _places.Insert(position, bus); - + _places.Insert(0, bus); return true; } public bool Remove(int position) { - if (position < 0 || position >= _places.Count) + if (position < 0 || position > _maxCount) + return false; + if (position >= Count) return false; _places.RemoveAt(position); return true; @@ -70,19 +64,26 @@ namespace projectDoubleDeckerBus.Generics return null; return _places[position]; } - public T this[int position] + public T? this[int position] { get { - if (position < 0 || position >= _places.Count) + if (position < 0 || position > _maxCount) + return null; + if (_places.Count <= position) return null; return _places[position]; } set { - Insert(value, position); + if (position < 0 || position > _maxCount) + return; + if (_places.Count <= position) + return; + _places[position] = value; } } + public IEnumerable GetBuses(int? maxBuses = null) { for (int i = 0; i < _places.Count; ++i)