Лабораторная №7

This commit is contained in:
yuliya.mavrina@internet.ru 2023-12-10 21:55:01 +03:00
parent dc45fbd65d
commit 28b59aba44
8 changed files with 96 additions and 86 deletions

View File

@ -10,7 +10,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.7" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@ -10,7 +10,7 @@ namespace DoubleDeckerBus.Exceptions
[Serializable]
internal class BusNotFoundException : ApplicationException
{
public BusNotFoundException(int i) : base($"Не найден объект по позиции { i}") { }
public BusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
public BusNotFoundException() : base() { }
public BusNotFoundException(string message) : base(message) { }
public BusNotFoundException(string message, Exception exception) : base(message, exception){ }

View File

@ -10,11 +10,10 @@ namespace DoubleDeckerBus.Exceptions
[Serializable]
internal class StorageOverflowException : ApplicationException
{
public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: { count}") { }
public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { }
public StorageOverflowException() : base() { }
public StorageOverflowException(string message) : base(message) { }
public StorageOverflowException(string message, Exception exception)
: base(message, exception) { }
public StorageOverflowException(string message, Exception exception): base(message, exception) { }
protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -39,10 +39,11 @@
ButtonDelObject = new Button();
ButtonAddObject = new Button();
menuStrip = new MenuStrip();
SaveToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
SaveToolStripMenuItem = new ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panel.SuspendLayout();
menuStrip.SuspendLayout();
@ -155,26 +156,25 @@
//
// menuStrip
//
menuStrip.Items.AddRange(new ToolStripItem[] { SaveToolStripMenuItem });
menuStrip.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(168, 24);
menuStrip.TabIndex = 10;
menuStrip.Text = "menuStrip1";
//
// SaveToolStripMenuItem
// ToolStripMenuItem
//
SaveToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { LoadToolStripMenuItem });
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(144, 20);
SaveToolStripMenuItem.Text = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
ToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { LoadToolStripMenuItem, SaveToolStripMenuItem });
ToolStripMenuItem.Name = "ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(48, 20);
ToolStripMenuItem.Text = "файл";
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(201, 22);
LoadToolStripMenuItem.Text = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
@ -186,6 +186,13 @@
//
saveFileDialog.Filter = "«txt file | *.txt»";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -219,7 +226,8 @@
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private MenuStrip menuStrip;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem ToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
}
}

View File

@ -14,29 +14,30 @@ using static System.Windows.Forms.DataFormats;
using System.Windows.Forms;
using DoubleDeckerBus.Exceptions;
using Microsoft.Extensions.Logging;
using System.Xml.Linq;
using Serilog;
using Log = Serilog.Log;
namespace DoubleDeckerBus
{
public partial class FormBusCollection : Form
{
private readonly BusesGenericStorage _storage;
private readonly ILogger _logger;
public FormBusCollection(ILogger<FormBusCollection> logger)
{
InitializeComponent();
_storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
_logger = logger;
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
@ -52,15 +53,14 @@ namespace DoubleDeckerBus
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
_logger.LogInformation($"Добавлен набор:{ textBoxStorageName.Text}");
Log.Information($"Добавлен набор:{textBoxStorageName.Text}");
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
@ -68,47 +68,16 @@ namespace DoubleDeckerBus
{
return;
}
string name = listBoxStorages.SelectedItem.ToString() ??
string.Empty;
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"Удалить объект {name}?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(name);
ReloadObjects();
_logger.LogInformation($"Удален набор: {name}");
Log.Information($"Удален набор: {name}");
}
}
private void buttonAddBus_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormBusConfig form = new FormBusConfig();
form.AddEvent(AddBus);
form.Show();
}
private void AddBus(DrawningBus boat)
{
boat._pictureWidth = pictureBoxCollection.Width;
boat._pictureHeight = pictureBoxCollection.Height; if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) return;
if (obj + boat)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowBuses();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
private void buttonRemoveBus_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
@ -120,29 +89,64 @@ namespace DoubleDeckerBus
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
FormBusConfig form = new();
form.Show();
Action<DrawningBus>? busDelegate = new((m) =>
{
try
{
bool q = obj + m;
MessageBox.Show("Объект добавлен");
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
pictureBoxCollection.Image = obj.ShowBuses();
}
catch (StorageOverflowException ex)
{
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
});
form.AddEvent(busDelegate);
}
private void buttonRemoveBus_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удалить", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
try
{
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowBuses();
Log.Information($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Не удалось удалить объект");
MessageBox.Show("Объект не удален");
Log.Warning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}");
}
}
catch (BusNotFoundException ex)
{
MessageBox.Show(ex.Message);
Log.Warning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show("Объект не добавлен"); Log.Warning("Not input");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
@ -165,12 +169,16 @@ namespace DoubleDeckerBus
{
_storage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
}
catch (Exception ex)
{
Log.Warning("Не удалось сохранить");
MessageBox.Show($"Не сохранилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
@ -180,14 +188,18 @@ namespace DoubleDeckerBus
{
_storage.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Файл {openFileDialog.FileName} успешно загружен");
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
ReloadObjects();
}
catch (Exception ex)
{
MessageBox.Show($"Не открылось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Warning("Не удалось загрузить");
MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

View File

@ -120,6 +120,9 @@
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>11, 17</value>
</metadata>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>11, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>132, 17</value>
</metadata>

View File

@ -36,8 +36,7 @@ namespace DoubleDeckerBus.Generics
_busStorages.Remove(name);
}
public BusesGenericCollection<DrawningBus, DrawningObjectBus>?
this[string ind]
public BusesGenericCollection<DrawningBus, DrawningObjectBus>?this[string ind]
{
get
{

View File

@ -1,4 +1,5 @@
using System;
using DoubleDeckerBus.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
@ -21,30 +22,23 @@ namespace DoubleDeckerBus.Generics
public bool Insert(T bus)
{
if (_places.Count == _maxCount)
{
return false;
}
throw new StorageOverflowException(_maxCount);
Insert(bus, 0);
return true;
}
public bool Insert(T bus, int position)
{
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
return false;
}
_places.Insert(position, bus);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
return false;
}
if (!(position >= 0 && position < Count))
throw new BusNotFoundException(position);
_places.RemoveAt(position);
return true;
}
@ -52,20 +46,14 @@ namespace DoubleDeckerBus.Generics
{
get
{
if (position < 0 || position >= _maxCount)
{
if (!(position >= 0 && position < Count))
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
}