Лабораторная №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> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <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> </ItemGroup>
</Project> </Project>

View File

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

View File

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

View File

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

View File

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

View File

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