Mochalov D.V. LabWork07 #7
@ -99,15 +99,26 @@ namespace Locomotive
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive) != -1)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Object added");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive) != -1)
|
||||
{
|
||||
MessageBox.Show("Object added");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
|
||||
else
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Failed to add object");
|
||||
}
|
||||
}
|
||||
catch(StorageOverflowException ex)
|
||||
{
|
||||
MessageBox.Show("Failed to add object");
|
||||
MessageBox.Show($"Storage overflow error: {ex.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Unknown error: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
@ -128,15 +139,27 @@ namespace Locomotive
|
||||
return;
|
||||
}
|
||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Object removed");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
||||
{
|
||||
MessageBox.Show("Object removed");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Failed to remove object");
|
||||
}
|
||||
}
|
||||
else
|
||||
catch(LocomotiveNotFoundException ex)
|
||||
{
|
||||
MessageBox.Show("Failed to remove object");
|
||||
MessageBox.Show($"Delete error: {ex.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Unknown error: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
/// Вывод набора
|
||||
private void buttonShowStorage_Click(object sender, EventArgs e)
|
||||
@ -190,13 +213,14 @@ namespace Locomotive
|
||||
{
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_mapsCollection.SaveData(saveFileDialog.FileName))
|
||||
try
|
||||
{
|
||||
MessageBox.Show("Saved successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
_mapsCollection.SaveData(saveFileDialog.FileName);
|
||||
MessageBox.Show("Saving success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Saving failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show($"Saving error: {ex.Message}", "Result",MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -205,16 +229,18 @@ namespace Locomotive
|
||||
{
|
||||
if (loadFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_mapsCollection.LoadData(loadFileDialog.FileName))
|
||||
|
||||
try
|
||||
{
|
||||
_mapsCollection.LoadData(loadFileDialog.FileName);
|
||||
MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
ReloadMaps();
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
else
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show("Loading failed", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show($"Loading failed + {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
ReloadMaps();
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
Locomotive/Locomotive/LocomotiveNotFoundException.cs
Normal file
19
Locomotive/Locomotive/LocomotiveNotFoundException.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Locomotive
|
||||
{
|
||||
internal class LocomotiveNotFoundException : ApplicationException
|
||||
{
|
||||
public LocomotiveNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
|
||||
public LocomotiveNotFoundException() : base() { }
|
||||
public LocomotiveNotFoundException(string message) : base(message) { }
|
||||
public LocomotiveNotFoundException(string message, Exception exception) :
|
||||
base(message, exception) { }
|
||||
protected LocomotiveNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -15,9 +15,9 @@ namespace Locomotive
|
||||
/// Высота окна отрисовки
|
||||
private readonly int _pictureHeight;
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
private readonly int _placeSizeWidth = 210;
|
||||
private readonly int _placeSizeWidth = 150;
|
||||
/// Размер занимаемого объектом места (высота)
|
||||
private readonly int _placeSizeHeight = 90;
|
||||
private readonly int _placeSizeHeight = 150;
|
||||
/// Набор объектов
|
||||
private readonly SetLocomotivesGeneric<T> _setLocomotives;
|
||||
/// Карта
|
||||
@ -138,8 +138,8 @@ namespace Locomotive
|
||||
/// Метод прорисовки объектов
|
||||
private void DrawLocomotives(Graphics g)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int height = _pictureHeight / _placeSizeHeight;
|
||||
int width = _pictureWidth / _placeSizeWidth - 1;
|
||||
int height = _pictureHeight / _placeSizeHeight - 1;
|
||||
|
||||
int curWidth = 0;
|
||||
int curHeight = 0;
|
||||
@ -147,7 +147,7 @@ namespace Locomotive
|
||||
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||
{
|
||||
// установка позиции
|
||||
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
|
||||
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 80, _pictureWidth, _pictureHeight);
|
||||
locomotive?.DrawningObject(g);
|
||||
if (curWidth < width) curWidth++;
|
||||
else
|
||||
|
@ -54,7 +54,7 @@ namespace Locomotive
|
||||
}
|
||||
|
||||
/// Сохранение информации по локомотивам в хранилище в файл
|
||||
public bool SaveData(string filename)
|
||||
public void SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
@ -72,14 +72,13 @@ namespace Locomotive
|
||||
);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// Загрузка нформации по локомотивам в депо из файла
|
||||
public bool LoadData(string filename)
|
||||
public void LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
|
||||
@ -88,7 +87,7 @@ namespace Locomotive
|
||||
|
||||
if (!curLine.Contains("MapsCollection"))
|
||||
{
|
||||
return false;
|
||||
throw new FileFormatException("Формат данных в файле неправильный");
|
||||
}
|
||||
|
||||
_mapStorages.Clear();
|
||||
@ -113,8 +112,6 @@ namespace Locomotive
|
||||
_mapStorages.Add(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elems[0]].LoadData(elems[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,7 +30,8 @@ namespace Locomotive
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
public int Insert(T locomotive, int position)
|
||||
{
|
||||
if (position >= _maxCount|| position < 0) return -1;
|
||||
if (position < 0) return -1;
|
||||
if (Count >= _maxCount) throw new StorageOverflowException(_maxCount);
|
||||
_places.Insert(position, locomotive);
|
||||
|
||||
return position;
|
||||
@ -39,8 +40,9 @@ namespace Locomotive
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= _maxCount || position < 0) return null;
|
||||
if (_places[position] is null) throw new LocomotiveNotFoundException(position);
|
||||
T result = _places[position];
|
||||
_places.RemoveAt(position);
|
||||
_places[position] = null; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! РАНЬШЕ REMOVE
|
||||
return result;
|
||||
}
|
||||
// Индексатор
|
||||
@ -61,15 +63,11 @@ namespace Locomotive
|
||||
public IEnumerable<T> GetLocomotives()
|
||||
{
|
||||
foreach (var locomotive in _places)
|
||||
{
|
||||
if (locomotive != null)
|
||||
{
|
||||
yield return locomotive;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
{
|
||||
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! РАНЬШЕ ВОЗВРАЩАЛОСЬ ТОЛЬКО ЕСЛИ НЕ НУЛЛ
|
||||
|
||||
yield return locomotive;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
20
Locomotive/Locomotive/StorageOverflowException.cs
Normal file
20
Locomotive/Locomotive/StorageOverflowException.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Locomotive
|
||||
{
|
||||
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) { }
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user