From b3513987ac504dd95640410c8814a48520e59b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=95=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Sun, 6 Oct 2024 22:19:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericCollection.cs | 6 +- Sailboat/Sailboat/BoatsGenericStorage.cs | 51 ++++++----- .../Sailboat/FormBoatCollection.Designer.cs | 89 ++++++++++--------- Sailboat/Sailboat/FormBoatCollection.cs | 22 +++-- Sailboat/Sailboat/FormSailboat.Designer.cs | 1 + Sailboat/Sailboat/SetGeneric.cs | 18 ++-- 6 files changed, 102 insertions(+), 85 deletions(-) diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 8ba60f6..1589da5 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -62,13 +62,13 @@ namespace Sailboat.Generics /// /// /// - public static int operator +(BoatsGenericCollection collect, T? obj) + public static bool operator +(BoatsGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj, new DrawingBoatEqutables()); + return (bool)collect?._collection.Insert(obj, new DrawingBoatEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index b939182..b0bbf36 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -129,39 +129,50 @@ namespace Sailboat.Generics { throw new FileNotFoundException("Файл не найден"); } - using (StreamReader sr = new(filename)) + + using (StreamReader reader = new StreamReader(filename)) { - string str = sr.ReadLine(); - if (str == null || str.Length == 0) - { + string checker = reader.ReadLine(); + if (checker == null) throw new NullReferenceException("Нет данных для загрузки"); - } - if (!str.StartsWith("BoatStorage")) + if (!checker.StartsWith("BoatStorage")) { - throw new InvalidDataException("Неверный формат данных"); + //если нет такой записи, то это не те данные + throw new FormatException("Неверный формат данных"); } + _boatStorages.Clear(); - while ((str = sr.ReadLine()) != null) + string strs; + bool firstinit = true; + while ((strs = reader.ReadLine()) != null) { - string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } + if (strs == null && firstinit) + throw new NullReferenceException("Нет данных для загрузки"); + if (strs == null) + break; + firstinit = false; + string name = strs.Split('|')[0]; BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set.Reverse()) + foreach (string data in strs.Split('|')[1].Split(';')) { - DrawingBoat? truck = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); - if (truck != null) + DrawingBoat? vehicle = data?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (vehicle != null) { - if (collection + truck == -1) + try { - throw new ApplicationException("Ошибка добавления в коллекцию"); + _ = collection + vehicle; + } + catch (BoatNotFoundException e) + { + throw e; + } + catch (StorageOverflowException e) + { + throw e; } } } - _boatStorages.Add(new BoatsCollectionInfo(record[0], string.Empty), collection); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), collection); } } } diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index 07c1e08..d82d2d6 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -29,29 +29,31 @@ namespace Sailboat /// private void InitializeComponent() { - pictureBoxCollection = new PictureBox(); - panelTools = new Panel(); - panelCollection = new Panel(); - buttonDelObject = new Button(); - listBoxStorages = new ListBox(); - buttonAddObject = new Button(); - textBoxStorageName = new TextBox(); - maskedTextBoxNumber = new MaskedTextBox(); - buttonRefreshCollection = new Button(); - buttonRemoveBoat = new Button(); - buttonAddBoat = new Button(); - menuStrip = new MenuStrip(); - toolStripMenuItem1 = new ToolStripMenuItem(); - ToolStripMenuItem = new ToolStripMenuItem(); - SaveToolStripMenuItem = new ToolStripMenuItem(); - LoadToolStripMenuItem = new ToolStripMenuItem(); - openFileDialog = new OpenFileDialog(); - saveFileDialog = new SaveFileDialog(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); - panelTools.SuspendLayout(); - panelCollection.SuspendLayout(); - menuStrip.SuspendLayout(); - SuspendLayout(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.panelTools = new System.Windows.Forms.Panel(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.panelCollection = new System.Windows.Forms.Panel(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddObject = new System.Windows.Forms.Button(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + this.buttonRemoveBoat = new System.Windows.Forms.Button(); + this.buttonAddBoat = new System.Windows.Forms.Button(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + 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.panelCollection.SuspendLayout(); + this.menuStrip.SuspendLayout(); + this.SuspendLayout(); // // pictureBoxCollection // @@ -77,7 +79,26 @@ namespace Sailboat panelTools.Name = "panelTools"; panelTools.Size = new Size(183, 542); panelTools.TabIndex = 1; - this.panelTools.TabIndex = 1; + // + // buttonSortByColor + // + buttonSortByColor.Location = new System.Drawing.Point(20, 479); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new System.Drawing.Size(180, 34); + buttonSortByColor.TabIndex = 7; + buttonSortByColor.Text = "Сортировать по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new System.Drawing.Point(20, 439); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new System.Drawing.Size(180, 34); + buttonSortByType.TabIndex = 6; + buttonSortByType.Text = "Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; // // panelCollection // @@ -220,26 +241,6 @@ namespace Sailboat // saveFileDialog.Filter = "txt file | *.txt"; // - // buttonSortByType - // - buttonSortByType.Location = new System.Drawing.Point(20, 348); - buttonSortByType.Name = "buttonSortByType"; - buttonSortByType.Size = new System.Drawing.Size(180, 34); - buttonSortByType.TabIndex = 6; - buttonSortByType.Text = "Сортировать по типу"; - buttonSortByType.UseVisualStyleBackColor = true; - buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); - // - // buttonSortByColor - // - buttonSortByColor.Location = new System.Drawing.Point(20, 488); - buttonSortByColor.Name = "buttonSortByColor"; - buttonSortByColor.Size = new System.Drawing.Size(180, 34); - buttonSortByColor.TabIndex = 7; - buttonSortByColor.Text = "Сортировать по цвету"; - buttonSortByColor.UseVisualStyleBackColor = true; - buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); - // // FormBoatCollection // AutoScaleDimensions = new SizeF(7F, 15F); diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 00050b0..807465b 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -77,16 +77,23 @@ namespace Sailboat _logger.LogWarning("Добавление пустого объекта"); return; } - if (obj + drawingBoat != -1) + try { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = obj.ShowBoats(); - _logger.LogInformation($"Объект {obj.GetType()} добавлен"); + if (obj + drawingBoat) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowBoats(); + _logger.LogInformation($"Объект {obj.GetType()} добавлен"); + } } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); - _logger.LogInformation($"Не удалось добавить объект"); + MessageBox.Show(ex.Message); + _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); + } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); } } @@ -128,7 +135,6 @@ namespace Sailboat _logger.LogWarning($"{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}"); } - } private void buttonRefreshCollection_Click(object sender, EventArgs e) diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index d5f33da..698bae1 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -162,6 +162,7 @@ buttonSelectBoat.TabIndex = 9; buttonSelectBoat.Text = "Выбрать лодку"; buttonSelectBoat.UseVisualStyleBackColor = true; + buttonSelectBoat.Click += buttonSelectBoat_Click; // // FormSailboat // diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index 72cf481..21d843e 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -23,7 +23,6 @@ namespace Sailboat.Generics /// private readonly int _maxCount; public void SortSet(IComparer comparer) => _places.Sort(comparer); - /// /// Конструктор /// @@ -38,7 +37,7 @@ namespace Sailboat.Generics /// /// Добавляемая лодка /// - public int Insert(T boat, IEqualityComparer? equal = null) + public bool Insert(T boat, IEqualityComparer? equal = null) { return Insert(boat, 0, equal); } @@ -48,19 +47,18 @@ namespace Sailboat.Generics /// Добавляемая лодка /// Позиция /// - public int Insert(T boat, int position, IEqualityComparer? equal = null) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { - if (position < 0 || position > Count) + if (position < 0 || position >= _maxCount) throw new BoatNotFoundException(position); + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null && _places.Contains(boat, equal)) - { - - return -1; - } - _places.Insert(position, boat); - return position; + throw new ArgumentException("Данный объект уже есть в коллекции"); + _places.Insert(0, boat); + return true; } /// /// Удаление объекта из набора с конкретной позиции