Седьмая лабораторная работа. Логер + вывод ошибок.

This commit is contained in:
ksenianeva 2022-12-13 12:26:09 +04:00
parent 4ab74bde46
commit e84985aaf5
5 changed files with 70 additions and 23 deletions

View File

@ -72,7 +72,7 @@
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(553, 33); this.groupBox1.Location = new System.Drawing.Point(553, 33);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(300, 654); this.groupBox1.Size = new System.Drawing.Size(300, 709);
this.groupBox1.TabIndex = 0; this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты"; this.groupBox1.Text = "Инструменты";
@ -154,7 +154,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow; this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(131, 536); this.buttonUp.Location = new System.Drawing.Point(131, 591);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 9; this.buttonUp.TabIndex = 9;
@ -166,7 +166,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(187, 592); this.buttonRight.Location = new System.Drawing.Point(187, 647);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 8; this.buttonRight.TabIndex = 8;
@ -178,7 +178,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow; this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(131, 592); this.buttonDown.Location = new System.Drawing.Point(131, 647);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 7; this.buttonDown.TabIndex = 7;
@ -190,7 +190,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(75, 592); this.buttonLeft.Location = new System.Drawing.Point(75, 647);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 6; this.buttonLeft.TabIndex = 6;
@ -240,7 +240,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 33); this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Name = "pictureBox"; this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(553, 654); this.pictureBox.Size = new System.Drawing.Size(553, 709);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@ -267,14 +267,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(270, 34); this.SaveToolStripMenuItem.Size = new System.Drawing.Size(212, 34);
this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(270, 34); this.LoadToolStripMenuItem.Size = new System.Drawing.Size(212, 34);
this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
@ -291,7 +291,7 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(853, 687); this.ClientSize = new System.Drawing.Size(853, 742);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip); this.Controls.Add(this.menuStrip);

View File

@ -33,12 +33,14 @@ namespace ContainerShip
/// Логер /// Логер
/// </summary> /// </summary>
private readonly ILogger _logger; private readonly ILogger _logger;
private Action<DrawingShip> addShipAction;
public FormMapWithSetShips(ILogger<FormMapWithSetShips> logger) public FormMapWithSetShips(ILogger<FormMapWithSetShips> logger)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
ComboBoxSelectorMap.Items.Clear(); ComboBoxSelectorMap.Items.Clear();
addShipAction = addShip;
foreach (var elem in _mapsDict) foreach (var elem in _mapsDict)
{ {
ComboBoxSelectorMap.Items.Add(elem.Key); ComboBoxSelectorMap.Items.Add(elem.Key);
@ -66,6 +68,30 @@ namespace ContainerShip
listBoxMaps.SelectedIndex = index; listBoxMaps.SelectedIndex = index;
} }
} }
private void addShip(DrawingShip ship)
{
try
{
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].add(new DrawingObjectShip(ship));
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Добавлен объект {ship}");
}
catch(ArgumentNullException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
catch(StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
}
/// <summary> /// <summary>
/// Добавление объекта /// Добавление объекта
/// </summary> /// </summary>
@ -78,9 +104,7 @@ namespace ContainerShip
return; return;
} }
var formShipConfig = new FormShipConfig(); var formShipConfig = new FormShipConfig();
formShipConfig.AddEvent(new Action<DrawingShip>(ship => { formShipConfig.AddEvent(addShipAction);
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].add(new DrawingObjectShip(ship));
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();}));
formShipConfig.Show(); formShipConfig.Show();
} }
/// <summary> /// <summary>
@ -105,19 +129,20 @@ namespace ContainerShip
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try try
{ {
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) IDrawingObject drawingObject = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos;
{
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} _logger.LogInformation($"Удален объект {drawingObject}");
} }
catch (ShipNotFoundException ex) catch (ShipNotFoundException ex)
{ {
MessageBox.Show($"Ошибка удаления: {ex.Message}"); MessageBox.Show($"Ошибка удаления: {ex.Message}");
_logger.LogWarning($"Ошибка: {ex.Message}");
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"Неизвестная ошибка: {ex.Message}") ; MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
_logger.LogWarning($"Ошибка: {ex.Message}");
} }
} }
/// <summary> /// <summary>
@ -202,6 +227,7 @@ namespace ContainerShip
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{ {
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Выбрана карта {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}");
} }
/// <summary> /// <summary>
/// Удаление карты /// Удаление карты
@ -214,11 +240,12 @@ namespace ContainerShip
{ {
return; return;
} }
string deletedMapName = listBoxMaps.SelectedItem?.ToString();
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps(); ReloadMaps();
_logger.LogInformation($"Удалена карта {deletedMapName}");
} }
} }
@ -230,6 +257,7 @@ namespace ContainerShip
{ {
_mapsCollection.SaveData(saveFileDialog.FileName); _mapsCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"Данные сохранены в файле {saveFileDialog.FileName}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -247,10 +275,22 @@ namespace ContainerShip
_mapsCollection.LoadData(openFileDialog.FileName); _mapsCollection.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps(); ReloadMaps();
_logger.LogInformation($"Данные загружены из файла {openFileDialog.FileName}");
}
catch (FileFormatException ex)
{
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Ошибка: {ex.Message}");
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Ошибка: {ex.Message}");
} }
} }
} }

View File

@ -66,4 +66,7 @@
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>357, 17</value> <value>357, 17</value>
</metadata> </metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root> </root>

View File

@ -129,7 +129,7 @@ namespace ContainerShip
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
throw new Exception("Файл не найден"); throw new FileNotFoundException("Файл не найден");
} }
using (FileStream fs = new(filename, FileMode.Open)) using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(fs)) using (StreamReader sr = new StreamReader(fs))
@ -139,7 +139,7 @@ namespace ContainerShip
line.Trim(); line.Trim();
if (line != "MapsCollection") if (line != "MapsCollection")
{ {
throw new Exception("Неправильный формат данных в файле"); throw new FileFormatException("Неправильный формат данных в файле");
} }
_mapStorages.Clear(); _mapStorages.Clear();
while ((line = sr.ReadLine()) != null) while ((line = sr.ReadLine()) != null)

View File

@ -44,7 +44,11 @@ namespace ContainerShip
/// <returns></returns> /// <returns></returns>
public int Insert(T ship, int position) public int Insert(T ship, int position)
{ {
if (position < 0 || position >= _maxCount || _places.Count + 1 >= _maxCount) if(position < 0 || position >= _maxCount)
{
throw new ShipNotFoundException(position);
}
if ( _places.Count >= _maxCount)
{ {
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);
} }