лаба 7, я так полагаю, возможно, кто знает, не уверена, готовая лаба
This commit is contained in:
parent
e016aa1b8e
commit
d177de49ad
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||
using AirplaneWithRadar.DrawningObjects;
|
||||
using AirplaneWithRadar.MovementStrategy;
|
||||
using AirplaneWithRadar.Exceptoins;
|
||||
using System.Numerics;
|
||||
|
||||
namespace AirplaneWithRadar.Generics
|
||||
{
|
||||
@ -115,7 +116,7 @@ namespace AirplaneWithRadar.Generics
|
||||
}
|
||||
if (data.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("Невалиданя операция, нет данных для сохранения");
|
||||
throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения");
|
||||
|
||||
}
|
||||
using (StreamWriter writer = new StreamWriter(filename))
|
||||
@ -132,66 +133,52 @@ namespace AirplaneWithRadar.Generics
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
throw new FileNotFoundException($"Файл {filename} не найден");
|
||||
}
|
||||
string bufferTextFromFile = "";
|
||||
using (StreamReader reader = new StreamReader(filename))
|
||||
{
|
||||
string str;
|
||||
while ((str = reader.ReadLine()) != null)
|
||||
{
|
||||
bufferTextFromFile += str + '\r' + '\n';
|
||||
}
|
||||
}
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
if (strs == null || strs.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("Нет данных для загрузки");
|
||||
|
||||
}
|
||||
if (!strs[0].StartsWith("AirplaneStorage"))
|
||||
using (StreamReader fs = File.OpenText(filename))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
throw new InvalidDataException("Неверный формат данных");
|
||||
}
|
||||
_airplaneStorages.Clear();
|
||||
foreach (string data in strs)
|
||||
{
|
||||
string[] record = data.Split(_separatorForKeyValue,
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
string str = fs.ReadLine();
|
||||
if (str == null || str.Length == 0)
|
||||
{
|
||||
continue;
|
||||
throw new NullReferenceException("Нет данных для загрузки");
|
||||
}
|
||||
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>
|
||||
collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords,
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
if (!str.StartsWith("AirplaneStorage"))
|
||||
{
|
||||
DrawningAirplane? airplane =
|
||||
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (airplane != null)
|
||||
//если нет такой записи, то это не те данные
|
||||
throw new FormatException("Неверный формат данных");
|
||||
}
|
||||
|
||||
_airplaneStorages.Clear();
|
||||
string strs = "";
|
||||
|
||||
while ((strs = fs.ReadLine()) != null)
|
||||
{
|
||||
if (strs == null)
|
||||
{
|
||||
if (!(collection + airplane))
|
||||
throw new NullReferenceException("Нет данных для загрузки");
|
||||
}
|
||||
|
||||
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (record.Length != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane> collection = new(_pictureWidth, _pictureHeight);
|
||||
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string elem in set)
|
||||
{
|
||||
DrawningAirplane? airplane = elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (airplane != null)
|
||||
{
|
||||
try
|
||||
if (!(collection + airplane))
|
||||
{
|
||||
_ = collection + airplane;
|
||||
}
|
||||
catch (AirplaneNotFoundException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
catch (StorageOverflowException e)
|
||||
{
|
||||
throw e;
|
||||
throw new InvalidOperationException("Ошибка добавления в коллекцию");
|
||||
}
|
||||
}
|
||||
}
|
||||
_airplaneStorages.Add(record[0], collection);
|
||||
}
|
||||
_airplaneStorages.Add(record[0], collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -185,14 +185,14 @@
|
||||
// сохранениеToolStripMenuItem
|
||||
//
|
||||
сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem";
|
||||
сохранениеToolStripMenuItem.Size = new Size(180, 22);
|
||||
сохранениеToolStripMenuItem.Size = new Size(141, 22);
|
||||
сохранениеToolStripMenuItem.Text = "Сохранение";
|
||||
сохранениеToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
|
||||
//
|
||||
// загрузкаToolStripMenuItem
|
||||
//
|
||||
загрузкаToolStripMenuItem.Name = "загрузкаToolStripMenuItem";
|
||||
загрузкаToolStripMenuItem.Size = new Size(180, 22);
|
||||
загрузкаToolStripMenuItem.Size = new Size(141, 22);
|
||||
загрузкаToolStripMenuItem.Text = "Загрузка";
|
||||
загрузкаToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
|
||||
//
|
||||
@ -210,6 +210,11 @@
|
||||
// openFileDialog
|
||||
//
|
||||
openFileDialog.FileName = "openFileDialog1";
|
||||
openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormAirplaneCollection
|
||||
//
|
||||
|
@ -84,6 +84,7 @@ namespace AirplaneWithRadar
|
||||
_logger.LogWarning($"Загрузка из файла {openFileDialog.FileName} не прошла");
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -144,6 +145,7 @@ namespace AirplaneWithRadar
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
_logger.LogWarning("Коллекция не выбрана");
|
||||
return;
|
||||
}
|
||||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||||
@ -164,6 +166,7 @@ namespace AirplaneWithRadar
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
_logger.LogWarning("Коллекция не выбрана");
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
@ -175,31 +178,23 @@ namespace AirplaneWithRadar
|
||||
|
||||
var formAirplaneConfig = new FormAirplaneConfig();
|
||||
formAirplaneConfig.Show();
|
||||
formAirplaneConfig.AddEvent(AddAirplane);
|
||||
}
|
||||
private void AddAirplane(DrawningAirplane airplane)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
Action<DrawningAirplane>? airplaneDelegate = new((m) =>
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
_ = obj + airplane;
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||||
_logger.LogInformation($"Самолет добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"Самолет не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
bool isAddSuccessful = (obj + m);
|
||||
if (isAddSuccessful)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||||
_logger.LogInformation($"Самолет добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Набор переполнен! Не удалось добавить объект");
|
||||
_logger.LogWarning($"Самолет не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
});
|
||||
formAirplaneConfig.AddEvent(airplaneDelegate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -222,21 +217,22 @@ namespace AirplaneWithRadar
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
{
|
||||
_logger.LogWarning("Отмена удаления объекта");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||||
_logger.LogInformation($"Удален объект с позиции {pos} из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
pictureBoxCollection.Image = obj.ShowAirplanes();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
_logger.LogInformation($"Объект не удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogWarning($"Объект не удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
}
|
||||
catch (AirplaneNotFoundException ex)
|
||||
@ -244,6 +240,11 @@ namespace AirplaneWithRadar
|
||||
MessageBox.Show(ex.Message);
|
||||
_logger.LogWarning($"Самолет не найден: {ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
_logger.LogWarning($"Было введено не число");
|
||||
MessageBox.Show("Введите число");
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -120,6 +120,9 @@
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 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>
|
||||
|
@ -75,7 +75,7 @@ namespace AirplaneWithRadar.Generics
|
||||
/// <returns></returns>
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
if (position >= Count || position < 0 || position > _maxCount)
|
||||
{
|
||||
throw new AirplaneNotFoundException("Невалидная операция");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user