Compare commits
2 Commits
Lab5SailBo
...
Lab7SailBo
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b736d3233 | |||
| a55d9450cf |
41
SailBoat/SailBoat/DrawningObjects/ExtentionDrawningBoat.cs
Normal file
41
SailBoat/SailBoat/DrawningObjects/ExtentionDrawningBoat.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using SailBoat.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.DrawningObjects
|
||||
{
|
||||
public static class ExtentionDrawningBoat
|
||||
{
|
||||
public static DrawningBoat? CreateDrawningBoat(this string info, char separatorForObject, int width, int height)
|
||||
{
|
||||
string[] strs = info.Split(separatorForObject);
|
||||
if(strs.Length == 3)
|
||||
{
|
||||
return new DrawningBoat(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
|
||||
}
|
||||
if(strs.Length == 6)
|
||||
{
|
||||
return new DrawningSailBoat(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 DrawningBoat drawningBoat, char separatorForObject)
|
||||
{
|
||||
var boat = drawningBoat.EntityBoat;
|
||||
if(boat == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var str = $"{boat.Speed}{separatorForObject}{boat.Weight}{separatorForObject}{boat.BodyColor.Name}";
|
||||
if(boat is not EntitySailBoat sailBoat)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{separatorForObject}{sailBoat.AdditionalColor.Name}{separatorForObject}{sailBoat.Sail}{separatorForObject}{sailBoat.RainforcedBody}";
|
||||
}
|
||||
}
|
||||
}
|
||||
23
SailBoat/SailBoat/Exceptions/BoatNotFoundException.cs
Normal file
23
SailBoat/SailBoat/Exceptions/BoatNotFoundException.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace SailBoat.Exceptions
|
||||
{
|
||||
[Serializable]
|
||||
internal class BoatNotFoundException : ApplicationException
|
||||
{
|
||||
public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
|
||||
|
||||
public BoatNotFoundException() : base() { }
|
||||
|
||||
public BoatNotFoundException(string message) : base(message) { }
|
||||
|
||||
public BoatNotFoundException(string message, Exception exception) : base(message, exception) { }
|
||||
|
||||
protected BoatNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
||||
23
SailBoat/SailBoat/Exceptions/StorageOverflowException.cs
Normal file
23
SailBoat/SailBoat/Exceptions/StorageOverflowException.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.Exceptions
|
||||
{
|
||||
[Serializable]
|
||||
internal class StorageOverflowException : ApplicationException
|
||||
{
|
||||
public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { }
|
||||
|
||||
public StorageOverflowException() : base() { }
|
||||
|
||||
public StorageOverflowException(string message) : base(message) { }
|
||||
|
||||
public StorageOverflowException(string message, Exception Exception) : base(message, Exception) { }
|
||||
|
||||
protected StorageOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
||||
79
SailBoat/SailBoat/FormBoatCollection.Designer.cs
generated
79
SailBoat/SailBoat/FormBoatCollection.Designer.cs
generated
@@ -39,9 +39,16 @@
|
||||
buttonRemoveBoat = new Button();
|
||||
buttonAddBoat = new Button();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
menuStrip = new MenuStrip();
|
||||
файлToolStripMenuItem = new ToolStripMenuItem();
|
||||
SaveToolStripMenuItem = new ToolStripMenuItem();
|
||||
LoadToolStripMenuItem = new ToolStripMenuItem();
|
||||
openFileDialog = new OpenFileDialog();
|
||||
saveFileDialog = new SaveFileDialog();
|
||||
tools.SuspendLayout();
|
||||
sets.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
menuStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// tools
|
||||
@@ -52,9 +59,9 @@
|
||||
tools.Controls.Add(buttonRemoveBoat);
|
||||
tools.Controls.Add(buttonAddBoat);
|
||||
tools.Dock = DockStyle.Right;
|
||||
tools.Location = new Point(716, 0);
|
||||
tools.Location = new Point(716, 24);
|
||||
tools.Name = "tools";
|
||||
tools.Size = new Size(200, 608);
|
||||
tools.Size = new Size(200, 613);
|
||||
tools.TabIndex = 0;
|
||||
tools.TabStop = false;
|
||||
tools.Text = "Инструменты";
|
||||
@@ -67,14 +74,14 @@
|
||||
sets.Controls.Add(textBoxStorageName);
|
||||
sets.Location = new Point(6, 22);
|
||||
sets.Name = "sets";
|
||||
sets.Size = new Size(188, 264);
|
||||
sets.Size = new Size(188, 344);
|
||||
sets.TabIndex = 5;
|
||||
sets.TabStop = false;
|
||||
sets.Text = "Наборы";
|
||||
//
|
||||
// ButtonDelObject
|
||||
//
|
||||
ButtonDelObject.Location = new Point(6, 215);
|
||||
ButtonDelObject.Location = new Point(6, 263);
|
||||
ButtonDelObject.Name = "ButtonDelObject";
|
||||
ButtonDelObject.Size = new Size(176, 41);
|
||||
ButtonDelObject.TabIndex = 3;
|
||||
@@ -86,7 +93,7 @@
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 15;
|
||||
listBoxStorages.Location = new Point(6, 100);
|
||||
listBoxStorages.Location = new Point(6, 148);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(176, 109);
|
||||
listBoxStorages.TabIndex = 2;
|
||||
@@ -94,7 +101,7 @@
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
ButtonAddObject.Location = new Point(6, 53);
|
||||
ButtonAddObject.Location = new Point(6, 39);
|
||||
ButtonAddObject.Name = "ButtonAddObject";
|
||||
ButtonAddObject.Size = new Size(176, 41);
|
||||
ButtonAddObject.TabIndex = 1;
|
||||
@@ -104,7 +111,7 @@
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(6, 23);
|
||||
textBoxStorageName.Location = new Point(6, 97);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(176, 23);
|
||||
textBoxStorageName.TabIndex = 0;
|
||||
@@ -120,7 +127,7 @@
|
||||
//
|
||||
// buttonRefreshCollection
|
||||
//
|
||||
buttonRefreshCollection.Location = new Point(6, 561);
|
||||
buttonRefreshCollection.Location = new Point(6, 545);
|
||||
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
||||
buttonRefreshCollection.Size = new Size(188, 39);
|
||||
buttonRefreshCollection.TabIndex = 3;
|
||||
@@ -150,19 +157,60 @@
|
||||
//
|
||||
// pictureBoxCollection
|
||||
//
|
||||
pictureBoxCollection.Location = new Point(0, 0);
|
||||
pictureBoxCollection.Location = new Point(0, 24);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(710, 608);
|
||||
pictureBoxCollection.Size = new Size(710, 613);
|
||||
pictureBoxCollection.TabIndex = 1;
|
||||
pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
||||
menuStrip.Location = new Point(0, 0);
|
||||
menuStrip.Name = "menuStrip";
|
||||
menuStrip.RenderMode = ToolStripRenderMode.System;
|
||||
menuStrip.Size = new Size(916, 24);
|
||||
menuStrip.TabIndex = 2;
|
||||
menuStrip.Text = "menuStrip";
|
||||
//
|
||||
// файлToolStripMenuItem
|
||||
//
|
||||
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
|
||||
файлToolStripMenuItem.Name = "файлToolStripMenuItem";
|
||||
файлToolStripMenuItem.Size = new Size(48, 20);
|
||||
файлToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
SaveToolStripMenuItem.Size = new Size(180, 22);
|
||||
SaveToolStripMenuItem.Text = "Сохранение";
|
||||
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
LoadToolStripMenuItem.Size = new Size(180, 22);
|
||||
LoadToolStripMenuItem.Text = "Загрузка";
|
||||
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.FileName = "openFileDialog";
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormBoatCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(916, 608);
|
||||
ClientSize = new Size(916, 637);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Controls.Add(tools);
|
||||
Controls.Add(menuStrip);
|
||||
Name = "FormBoatCollection";
|
||||
Text = "Набор лодок";
|
||||
tools.ResumeLayout(false);
|
||||
@@ -170,7 +218,10 @@
|
||||
sets.ResumeLayout(false);
|
||||
sets.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
menuStrip.ResumeLayout(false);
|
||||
menuStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -186,5 +237,11 @@
|
||||
private ListBox listBoxStorages;
|
||||
private Button ButtonAddObject;
|
||||
private TextBox textBoxStorageName;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem файлToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using SailBoat.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SailBoat
|
||||
{
|
||||
@@ -17,10 +20,13 @@ namespace SailBoat
|
||||
{
|
||||
private readonly BoatsGenericStorage _storage;
|
||||
|
||||
public FormBoatCollection()
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public FormBoatCollection(ILogger<FormBoatCollection> logger)
|
||||
{
|
||||
InitializeComponent();
|
||||
_storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void ReloadObjects()
|
||||
@@ -48,10 +54,12 @@ namespace SailBoat
|
||||
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
||||
{
|
||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogWarning($"!!Ошибка!! Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
_storage.AddSet(textBoxStorageName.Text);
|
||||
ReloadObjects();
|
||||
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
||||
}
|
||||
|
||||
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||||
@@ -61,14 +69,17 @@ namespace SailBoat
|
||||
|
||||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(listBoxStorages.SelectedIndex == -1)
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
_logger.LogWarning($"Удаление не выбранного набора");
|
||||
return;
|
||||
}
|
||||
if(MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||||
if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||||
{
|
||||
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
|
||||
ReloadObjects();
|
||||
_logger.LogInformation($"Удален набор: {name}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,16 +89,27 @@ namespace SailBoat
|
||||
boat._pictureHeight = pictureBoxCollection.Height;
|
||||
if (listBoxStorages.SelectedIndex == -1) return;
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||||
if (obj == null) return;
|
||||
|
||||
if (obj + boat)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
|
||||
if (obj + boat)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
_logger.LogInformation($"В набор {name} добавлен объект");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
_logger.LogWarning("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
catch(StorageOverflowException ex) {
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +128,7 @@ namespace SailBoat
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1) return;
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||||
if (obj == null) return;
|
||||
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
@@ -114,14 +137,23 @@ namespace SailBoat
|
||||
}
|
||||
if (maskedTextBoxNumber.Text == "") { return; }
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
if (obj - pos)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
}
|
||||
else
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
_logger.LogInformation($"Из набора {name} удален объект");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
_logger.LogWarning("Не удалось удалить объект");
|
||||
}
|
||||
} catch(BoatNotFoundException ex)
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"{ex.Message}. Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +165,41 @@ namespace SailBoat
|
||||
pictureBoxCollection.Image = obj.ShowBoats();
|
||||
}
|
||||
|
||||
|
||||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
_storage.SaveData(saveFileDialog.FileName);
|
||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_logger.LogInformation("Сохранение");
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogWarning("Ошибка сохранения");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
_storage.LoadData(openFileDialog.FileName);
|
||||
ReloadObjects();
|
||||
MessageBox.Show("Загрузка произошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_logger.LogInformation("Загрузка");
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_logger.LogWarning("Ошибка загрузки");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,4 +117,13 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>132, 17</value>
|
||||
</metadata>
|
||||
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>247, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>387, 17</value>
|
||||
</metadata>
|
||||
</root>
|
||||
2
SailBoat/SailBoat/FormBoatConfig.Designer.cs
generated
2
SailBoat/SailBoat/FormBoatConfig.Designer.cs
generated
@@ -168,7 +168,7 @@
|
||||
//
|
||||
// panelGreen
|
||||
//
|
||||
panelGreen.BackColor = Color.FromArgb(0, 192, 0);
|
||||
panelGreen.BackColor = Color.LimeGreen;
|
||||
panelGreen.Location = new Point(71, 22);
|
||||
panelGreen.Name = "panelGreen";
|
||||
panelGreen.Size = new Size(50, 50);
|
||||
|
||||
@@ -38,16 +38,13 @@ namespace SailBoat.Generics
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
|
||||
public static bool operator -(BoatsGenericCollection<T,U> collect, int pos)
|
||||
public static T? operator -(BoatsGenericCollection<T,U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection[pos];
|
||||
if(obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
collect._collection.Remove(pos);
|
||||
|
||||
return true;
|
||||
return obj;
|
||||
}
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
@@ -91,5 +88,7 @@ namespace SailBoat.Generics
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<T?> GetBoats => _collection.GetBoats();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +45,93 @@ namespace SailBoat.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
|
||||
if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly char _separatorForKeyValue = '|';
|
||||
private readonly char _separatorRecords = ';';
|
||||
private static readonly char _separatorForObject = ':';
|
||||
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
StringBuilder data = new();
|
||||
|
||||
foreach (KeyValuePair < string, BoatsGenericCollection < DrawningBoat, DrawningObjectBoat>> record in _boatStorages)
|
||||
{
|
||||
StringBuilder records = new();
|
||||
foreach (DrawningBoat? elem in record.Value.GetBoats)
|
||||
{
|
||||
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
||||
}
|
||||
data.Append($"{record.Key}{_separatorForKeyValue}{records}\n");
|
||||
}
|
||||
|
||||
if (data.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения");
|
||||
}
|
||||
|
||||
using StreamWriter fs = new StreamWriter(filename);
|
||||
string info = $"BoatStorage{Environment.NewLine}{data}";
|
||||
fs.Write(info, 0, info.Length);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
|
||||
using (StreamReader fs = new StreamReader(filename))
|
||||
{
|
||||
string str = fs.ReadLine();
|
||||
|
||||
if(str == null || str.Length == 0)
|
||||
{
|
||||
throw new NullReferenceException("Нет данных для загрузки");
|
||||
}
|
||||
|
||||
if (!str.StartsWith("BoatStorage"))
|
||||
{
|
||||
throw new FormatException("Неверный формат данных");
|
||||
}
|
||||
_boatStorages.Clear();
|
||||
while ((str = fs.ReadLine()) != null)
|
||||
{
|
||||
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
BoatsGenericCollection<DrawningBoat, DrawningObjectBoat> collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawningBoat? boat = elem?.CreateDrawningBoat(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (boat != null)
|
||||
{
|
||||
if (!(collection + boat))
|
||||
{
|
||||
throw new ApplicationException("Ошибка добавления в коллекцию");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_boatStorages.Add(record[0], collection);
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SailBoat.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -30,11 +31,11 @@ namespace SailBoat.Generics
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
return false;
|
||||
throw new BoatNotFoundException(position);
|
||||
}
|
||||
if (Count >= _maxCount)
|
||||
{
|
||||
return false;
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
}
|
||||
_places.Insert(position, boat);
|
||||
return true;
|
||||
@@ -46,11 +47,12 @@ namespace SailBoat.Generics
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (Count >= _maxCount)
|
||||
if (_places[position] == null)
|
||||
{
|
||||
return false;
|
||||
throw new BoatNotFoundException(position);
|
||||
}
|
||||
_places.RemoveAt(position);
|
||||
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -58,12 +60,12 @@ namespace SailBoat.Generics
|
||||
{
|
||||
get
|
||||
{
|
||||
if (position < 0 || position > _maxCount) { return null; }
|
||||
if (position < 0 || position >= Count) { return null; }
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position > _maxCount) { return; }
|
||||
if (position < 0 || position >= Count) { return; }
|
||||
_places[position] = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SailBoat
|
||||
{
|
||||
internal static class Program
|
||||
@@ -11,7 +19,29 @@ namespace SailBoat
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new FormBoatCollection());
|
||||
var services = new ServiceCollection();
|
||||
ConfigureServices(services);
|
||||
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
|
||||
{
|
||||
Application.Run(serviceProvider.GetRequiredService<FormBoatCollection>());
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FormBoatCollection>().AddLogging(option =>
|
||||
{
|
||||
string[] path = Directory.GetCurrentDirectory().Split('\\');
|
||||
string appPath = "";
|
||||
for (int i = 0; i < path.Length - 3; i++)
|
||||
{
|
||||
appPath += path[i] + "\\";
|
||||
}
|
||||
var configuration = new ConfigurationBuilder().AddJsonFile($"{appPath}appsettings.json").Build();
|
||||
var logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
|
||||
option.SetMinimumLevel(LogLevel.Information);
|
||||
option.AddSerilog(logger);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,34 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="C:\Users\shotb\.nuget\packages\nlog.config\4.7.15\contentFiles\any\any\NLog.config" />
|
||||
<None Remove="C:\Users\shotb\.nuget\packages\nlog.schema\5.2.7\contentFiles\any\any\NLog.xsd" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0" />
|
||||
<PackageReference Include="NLog" Version="5.2.7" />
|
||||
<PackageReference Include="NLog.Config" Version="4.7.15" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
|
||||
<PackageReference Include="NLog.Schema" Version="5.2.7" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.5" />
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
|
||||
20
SailBoat/SailBoat/appsettings.json
Normal file
20
SailBoat/SailBoat/appsettings.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Information",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "logs/boatlog-.log",
|
||||
"rollingInterval": "Day",
|
||||
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
|
||||
"Properties": {
|
||||
"Application": "Loco"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user