Compare commits
No commits in common. "507118762322d6568cadd14529075ecac9de956e" and "f575f977b02a8b26ddb693ad4bd896c45764e8e7" have entirely different histories.
5071187623
...
f575f977b0
@ -17,8 +17,8 @@ public abstract class AbstractCompany
|
|||||||
|
|
||||||
// Коллекция автомобилей
|
// Коллекция автомобилей
|
||||||
protected ICollectionGenObj<DrawningBase>? _collection = null;
|
protected ICollectionGenObj<DrawningBase>? _collection = null;
|
||||||
private int GetMaxCount => (_pictureWidth / (_placeSizeWidth + 20))
|
private int GetMaxCount => _pictureWidth * _pictureHeight /
|
||||||
* ( _pictureHeight / (_placeSizeHeight + 4));
|
(_placeSizeWidth * _placeSizeHeight);
|
||||||
|
|
||||||
public AbstractCompany(int picWidth, int picHeight,
|
public AbstractCompany(int picWidth, int picHeight,
|
||||||
ICollectionGenObj<DrawningBase>? collection)
|
ICollectionGenObj<DrawningBase>? collection)
|
||||||
@ -51,22 +51,20 @@ public abstract class AbstractCompany
|
|||||||
Bitmap bitmap = new(_pictureWidth, _pictureHeight);
|
Bitmap bitmap = new(_pictureWidth, _pictureHeight);
|
||||||
Graphics graphics = Graphics.FromImage(bitmap);
|
Graphics graphics = Graphics.FromImage(bitmap);
|
||||||
DrawBackground(graphics);
|
DrawBackground(graphics);
|
||||||
|
SetObjectsPosition();
|
||||||
SetObjectsPosition(_collection.Count - 1);
|
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
|
||||||
|
|
||||||
for (int i = 0; i < (_collection?.Count ?? 0); i++)
|
|
||||||
{
|
{
|
||||||
DrawningBase? obj = _collection?.GetItem(i);
|
DrawningBase? obj = _collection?.GetItem(i);
|
||||||
obj?.DrawTransport(graphics);
|
obj?.DrawTransport(graphics);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Вывод заднего фона
|
// Вывод заднего фона
|
||||||
protected abstract void DrawBackground(Graphics g);
|
protected abstract void DrawBackground(Graphics g);
|
||||||
|
|
||||||
// Расстановка объектов
|
// Расстановка объектов
|
||||||
protected abstract void SetObjectsPosition(int border);
|
protected abstract void SetObjectsPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using ProjectCruiser.Exceptions;
|
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
public class ArrayGenObj<T> : ICollectionGenObj<T>
|
public class ArrayGenObj<T> : ICollectionGenObj<T>
|
||||||
@ -10,7 +9,7 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
// Максимально допустимое число объектов в массиве
|
// Максимально допустимое число объектов в массиве
|
||||||
private int _maxCount;
|
private int _maxCount;
|
||||||
public int Count => _collection.Count(s => (s != null));
|
public int Count => _collection.Count(s => s != null);
|
||||||
|
|
||||||
public int MaxCount
|
public int MaxCount
|
||||||
{
|
{
|
||||||
@ -19,10 +18,10 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
{
|
{
|
||||||
if (value > 0)
|
if (value > 0)
|
||||||
{
|
{
|
||||||
_maxCount = value;
|
|
||||||
|
|
||||||
if (_collection.Length == 0) _collection = new T?[value];
|
if (_collection.Length == 0) _collection = new T?[value];
|
||||||
else Array.Resize(ref _collection, value);
|
else Array.Resize(ref _collection, value);
|
||||||
|
|
||||||
|
_maxCount = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,31 +36,22 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
// methods :
|
// methods :
|
||||||
public T? GetItem(int index)
|
public T? GetItem(int index)
|
||||||
{
|
{
|
||||||
if (index > _maxCount)
|
if (index > Count || index < 0)
|
||||||
throw new CollectionOverflowException(index);
|
{
|
||||||
if (index < 0)
|
return null;
|
||||||
throw new PositionOutOfCollectionException(index);
|
}
|
||||||
|
|
||||||
if (_collection[index] == null)
|
|
||||||
throw new ObjectNotFoundException(index);
|
|
||||||
|
|
||||||
return _collection[index];
|
return _collection[index];
|
||||||
|
|
||||||
// CollectionOverflowException
|
|
||||||
// PositionOutOfCollectionException
|
|
||||||
// ObjectNotFoundException
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? item)
|
public int Insert(T? item)
|
||||||
{
|
{
|
||||||
if (item == null) throw
|
if (Count >= _maxCount || item == null)
|
||||||
new NullReferenceException("> Inserting item is null");
|
{
|
||||||
|
return -1;
|
||||||
// выход за границы, курируется CollectionOverflowException
|
}
|
||||||
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
|
||||||
|
|
||||||
// any empty place -> fill immediately
|
// any empty place -> fill immediately
|
||||||
for (int i = Count; i < _maxCount; i++)
|
for (int i = 0; i < _collection.Length; i++)
|
||||||
{
|
{
|
||||||
if (_collection[i] == null)
|
if (_collection[i] == null)
|
||||||
{
|
{
|
||||||
@ -70,63 +60,50 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Count;
|
return Count;
|
||||||
|
|
||||||
// NullReferenceException
|
|
||||||
// CollectionOverflowException
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? item, int index)
|
public int Insert(T? item, int index)
|
||||||
{
|
{
|
||||||
if (index < 0 || index >= _maxCount) throw new PositionOutOfCollectionException(index);
|
if (index >= _maxCount || Count >= _maxCount ||
|
||||||
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
index < 0 || _collection[index] != null
|
||||||
|
|| item == null)
|
||||||
if (item == null) throw
|
{
|
||||||
new NullReferenceException("> Inserting item (at position) is null");
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (_collection[index] == null)
|
if (_collection[index] == null)
|
||||||
{
|
{
|
||||||
_collection[index] = item;
|
_collection[index] = item;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
int min_diff = 100, firstNullIndex = 100;
|
||||||
|
|
||||||
|
for (int i = 0; i < Count; i++)
|
||||||
{
|
{
|
||||||
int min_diff = 100, firstNullIndex = 100;
|
if (_collection[i] == null
|
||||||
|
&& min_diff > Math.Abs(index - i))
|
||||||
for (int i = 0; i < Count; i++)
|
|
||||||
{
|
{
|
||||||
if (_collection[i] == null && min_diff > Math.Abs(index - i))
|
min_diff = Math.Abs(index - i);
|
||||||
{
|
firstNullIndex = i;
|
||||||
min_diff = Math.Abs(index - i);
|
|
||||||
firstNullIndex = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_collection[firstNullIndex] = item;
|
|
||||||
return firstNullIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PositionOutOfCollectionException
|
_collection[firstNullIndex] = item;
|
||||||
// CollectionOverflowException
|
return firstNullIndex;
|
||||||
// NullReferenceException
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int index)
|
public T? Remove(int index)
|
||||||
{
|
{
|
||||||
if (index >= _maxCount || index < 0)
|
if (index >= Count || index < 0)
|
||||||
// on the other positions items don't exist
|
// on the other positions items don't exist
|
||||||
{
|
{
|
||||||
throw new PositionOutOfCollectionException(index);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
T? item = _collection[index];
|
T? item = _collection[index];
|
||||||
_collection[index] = null;
|
_collection[index] = null;
|
||||||
|
|
||||||
if (item == null) throw new ObjectNotFoundException(index);
|
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
|
|
||||||
// PositionOutOfCollectionException
|
|
||||||
// ObjectNotFoundException
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
using ProjectCruiser.Exceptions;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
@ -37,24 +40,20 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public T? GetItem(int position)
|
public T? GetItem(int position)
|
||||||
{
|
{
|
||||||
if (position > _maxCount)
|
if (position >= Count || position < 0)
|
||||||
throw new CollectionOverflowException(position);
|
{
|
||||||
if (position < 0)
|
return null;
|
||||||
throw new PositionOutOfCollectionException(position);
|
}
|
||||||
|
|
||||||
if (_collection[position] == null)
|
|
||||||
throw new ObjectNotFoundException(position);
|
|
||||||
|
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? obj)
|
public int Insert(T? obj)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
if (Count >= _maxCount || obj == null)
|
||||||
throw new NullReferenceException("> Inserting object is null");
|
{
|
||||||
|
return -1;
|
||||||
// выход за границы, курируется CollectionOverflowException
|
}
|
||||||
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
|
||||||
|
|
||||||
_collection.Add(obj);
|
_collection.Add(obj);
|
||||||
return Count;
|
return Count;
|
||||||
@ -62,12 +61,12 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public int Insert(T? obj, int position)
|
public int Insert(T? obj, int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _maxCount)
|
if (position >= _maxCount || Count >= _maxCount ||
|
||||||
throw new PositionOutOfCollectionException(position);
|
position < 0 || _collection[position] != null
|
||||||
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
|| obj == null)
|
||||||
|
{
|
||||||
if (obj == null)
|
return -1;
|
||||||
throw new NullReferenceException("> Inserting object (at position) is null");
|
}
|
||||||
|
|
||||||
_collection.Insert(position, obj);
|
_collection.Insert(position, obj);
|
||||||
return position;
|
return position;
|
||||||
@ -75,17 +74,14 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public T? Remove(int position)
|
public T? Remove(int position)
|
||||||
{
|
{
|
||||||
if (position >= _maxCount || position < 0)
|
if (position >= Count || position < 0)
|
||||||
// on the other positions items don't exist
|
// on the other positions items don't exist
|
||||||
{
|
{
|
||||||
throw new PositionOutOfCollectionException(position);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
T? item = _collection[position];
|
T? item = _collection[position];
|
||||||
_collection.RemoveAt(position);
|
_collection.RemoveAt(position);
|
||||||
|
|
||||||
if (item == null) throw new ObjectNotFoundException(position);
|
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,33 +41,27 @@ public class ShipSharingService : AbstractCompany
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void SetObjectsPosition(int border)
|
protected override void SetObjectsPosition()
|
||||||
{
|
{
|
||||||
int index_collection = 0;
|
int index_collection = 0;
|
||||||
int newY = fromCeiling + 4;
|
int newX = fromBorder + 6, newY = fromCeiling + 6;
|
||||||
|
|
||||||
if (_collection != null)
|
if (_collection != null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MaxInColon; i++)
|
for (int i = 0; i < MaxInColon; ++i)
|
||||||
{
|
{
|
||||||
int newX = fromBorder + 2;
|
newX = fromBorder + 2;
|
||||||
for (int j = 0; j < MaxInRow; j++)
|
for (int j = 0; j < MaxInRow; ++j)
|
||||||
{
|
{
|
||||||
// TRY / CATCH [?]
|
if (_collection.GetItem(index_collection) != null)
|
||||||
_collection.GetItem(index_collection).SetPictureSize(
|
|
||||||
_pictureWidth, _pictureHeight);
|
|
||||||
|
|
||||||
_collection.GetItem(index_collection).SetPosition(newX, newY);
|
|
||||||
|
|
||||||
newX += _placeSizeWidth + between + 2;
|
|
||||||
|
|
||||||
if (index_collection < border)
|
|
||||||
{
|
{
|
||||||
|
_collection.GetItem(index_collection).SetPictureSize(_pictureWidth, _pictureHeight);
|
||||||
|
_collection.GetItem(index_collection).SetPosition(newX, newY);
|
||||||
|
newX += _placeSizeWidth + between + 2;
|
||||||
index_collection++;
|
index_collection++;
|
||||||
}
|
}
|
||||||
else return;
|
|
||||||
}
|
}
|
||||||
newY += _placeSizeHeight + 1;
|
newY += _placeSizeHeight + 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
using System.Security.Cryptography;
|
using System.Text;
|
||||||
using System.Text;
|
|
||||||
using ProjectCruiser.DrawningSamples;
|
using ProjectCruiser.DrawningSamples;
|
||||||
using ProjectCruiser.Exceptions;
|
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
@ -33,7 +31,7 @@ public class StorageCollection<T>
|
|||||||
if (name == null || _storages.ContainsKey(name)
|
if (name == null || _storages.ContainsKey(name)
|
||||||
|| collType == CollectionType.None)
|
|| collType == CollectionType.None)
|
||||||
{
|
{
|
||||||
throw new NullReferenceException("> Not enough information to save");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ICollectionGenObj<T> collection = CreateCollection(collType);
|
ICollectionGenObj<T> collection = CreateCollection(collType);
|
||||||
@ -44,7 +42,7 @@ public class StorageCollection<T>
|
|||||||
public void DelCollection(string name)
|
public void DelCollection(string name)
|
||||||
{
|
{
|
||||||
if (_storages.ContainsKey(name)) _storages.Remove(name);
|
if (_storages.ContainsKey(name)) _storages.Remove(name);
|
||||||
else throw new NullReferenceException("> No such key in the list");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Доступ к коллекции ( по ключу-строке - её имени ) - индексатор [!!!]
|
// Доступ к коллекции ( по ключу-строке - её имени ) - индексатор [!!!]
|
||||||
@ -57,11 +55,9 @@ public class StorageCollection<T>
|
|||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - сохранение прошло успешно,
|
/// <returns>true - сохранение прошло успешно,
|
||||||
/// false - ошибка при сохранении данных</returns>
|
/// false - ошибка при сохранении данных</returns>
|
||||||
public void SaveData(string filename)
|
public bool SaveData(string filename)
|
||||||
{
|
{
|
||||||
if (_storages.Count == 0)
|
if (_storages.Count == 0) { return false; }
|
||||||
throw new NullReferenceException("> No existing collections to save");
|
|
||||||
|
|
||||||
if (File.Exists(filename)) { File.Delete(filename); }
|
if (File.Exists(filename)) { File.Delete(filename); }
|
||||||
|
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
@ -83,6 +79,21 @@ public class StorageCollection<T>
|
|||||||
foreach (T? item in pair.Value.GetItems())
|
foreach (T? item in pair.Value.GetItems())
|
||||||
{
|
{
|
||||||
string data = item?.GetDataForSave() ?? string.Empty;
|
string data = item?.GetDataForSave() ?? string.Empty;
|
||||||
|
|
||||||
|
/*
|
||||||
|
string n = item.GetType().Name;
|
||||||
|
string data = null;
|
||||||
|
|
||||||
|
if (n != null && n == "DrawningCruiser")
|
||||||
|
{
|
||||||
|
data = ExtentionDrShip.GetDataForSave(item);
|
||||||
|
}
|
||||||
|
else if (n != null && n == "DrawningBase")
|
||||||
|
{
|
||||||
|
data = ExtentionDrShip.GetDataForSave(item);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(data))
|
if (string.IsNullOrEmpty(data))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@ -96,6 +107,7 @@ public class StorageCollection<T>
|
|||||||
using FileStream fs = new(filename, FileMode.Create);
|
using FileStream fs = new(filename, FileMode.Create);
|
||||||
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
||||||
fs.Write(info, 0, info.Length);
|
fs.Write(info, 0, info.Length);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Создание коллекции по типу
|
// Создание коллекции по типу
|
||||||
@ -110,9 +122,12 @@ public class StorageCollection<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Загрузка информации по кораблям в хранилище из файла
|
// Загрузка информации по кораблям в хранилище из файла
|
||||||
public void LoadData(string filename)
|
public bool LoadData(string filename)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename)) throw new FileNotFoundException("> No such file");
|
if (!File.Exists(filename))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
string bufferTextFromFile = "";
|
string bufferTextFromFile = "";
|
||||||
|
|
||||||
@ -130,9 +145,14 @@ public class StorageCollection<T>
|
|||||||
new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
if (strs == null || strs.Length == 0)
|
if (strs == null || strs.Length == 0)
|
||||||
throw new NullReferenceException("> No data to decode");
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!strs[0].Equals(_collectionKey))
|
if (!strs[0].Equals(_collectionKey))
|
||||||
throw new InvalidDataException("> Incorrect data");
|
{
|
||||||
|
//если нет такой записи, то это не те данные
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
string[] companies = new string[strs.Length - 1];
|
string[] companies = new string[strs.Length - 1];
|
||||||
for (int k = 1; k < strs.Length; k++)
|
for (int k = 1; k < strs.Length; k++)
|
||||||
@ -146,13 +166,15 @@ public class StorageCollection<T>
|
|||||||
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (record.Length != 4) // >
|
if (record.Length != 4) // >
|
||||||
// key | collType | maxcount | all next inf > 4
|
// key | collType | maxcount | all next inf > 4
|
||||||
{ continue; }
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||||
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
throw new NullReferenceException("[!] Failed to create collection");
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
string[] set = record[3].Split(_separatorItems,
|
string[] set = record[3].Split(_separatorItems,
|
||||||
@ -162,22 +184,15 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (elem?.CreateDrawningCar() is T ship)
|
if (elem?.CreateDrawningCar() is T ship)
|
||||||
{
|
{
|
||||||
try
|
if (collection.Insert(ship) == -1)
|
||||||
{
|
{
|
||||||
collection.Insert(ship);
|
return false;
|
||||||
|
|
||||||
// throw new IndexOutOfRangeException IF IT WAS Insert(item, pos)
|
|
||||||
// NullReferenceException >
|
|
||||||
// CollectionOverflowException >
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new Exception(e.Message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
_storages.Add(record[0], collection);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
using System.Runtime.Serialization;
|
|
||||||
namespace ProjectCruiser.Exceptions;
|
|
||||||
|
|
||||||
// Класс, описывающий ошибку переполнения коллекции
|
|
||||||
[Serializable]
|
|
||||||
internal class CollectionOverflowException : ApplicationException
|
|
||||||
{
|
|
||||||
public CollectionOverflowException(int count)
|
|
||||||
: base("<> Possible accsess\nof collection is over : " + count) { }
|
|
||||||
public CollectionOverflowException() : base() { }
|
|
||||||
public CollectionOverflowException(string message) : base(message) { }
|
|
||||||
public CollectionOverflowException(string message, Exception exception) :
|
|
||||||
base(message, exception)
|
|
||||||
{ }
|
|
||||||
protected CollectionOverflowException(SerializationInfo info,
|
|
||||||
StreamingContext contex) : base(info, contex) { }
|
|
||||||
}
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
|||||||
using System.Runtime.Serialization;
|
|
||||||
namespace ProjectCruiser.Exceptions;
|
|
||||||
|
|
||||||
// Класс, описывающий ошибку, что по указанной позиции нет элемента
|
|
||||||
[Serializable]
|
|
||||||
internal class ObjectNotFoundException : ApplicationException
|
|
||||||
{
|
|
||||||
public ObjectNotFoundException(int i)
|
|
||||||
: base("<> Didn't find obj\non this position : " + i) { }
|
|
||||||
public ObjectNotFoundException() : base() { }
|
|
||||||
public ObjectNotFoundException(string message) : base(message) { }
|
|
||||||
public ObjectNotFoundException(string message, Exception exception)
|
|
||||||
: base(message, exception) { }
|
|
||||||
protected ObjectNotFoundException(SerializationInfo info,
|
|
||||||
StreamingContext contex) : base(info, contex) { }
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
using System.Runtime.Serialization;
|
|
||||||
namespace ProjectCruiser.Exceptions;
|
|
||||||
|
|
||||||
// Класс, описывающий ошибку выхода за границы коллекции
|
|
||||||
[Serializable]
|
|
||||||
internal class PositionOutOfCollectionException : ApplicationException
|
|
||||||
{
|
|
||||||
public PositionOutOfCollectionException(int i)
|
|
||||||
: base("<> Out of collection\nboarder. Position : " + i) { }
|
|
||||||
public PositionOutOfCollectionException() : base() { }
|
|
||||||
public PositionOutOfCollectionException(string message) : base(message) { }
|
|
||||||
public PositionOutOfCollectionException(string message,
|
|
||||||
Exception exception) : base(message, exception) { }
|
|
||||||
protected PositionOutOfCollectionException(SerializationInfo info,
|
|
||||||
StreamingContext contex) : base(info, contex) { }
|
|
||||||
}
|
|
@ -1,45 +1,17 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace ProjectCruiser
|
namespace ProjectCruiser
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main entry point for the application.
|
/// The main entry point for the application.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
|
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
|
Application.Run(new ServiceForm2());
|
||||||
ServiceCollection services = new();
|
|
||||||
ConfigureServices(services);
|
|
||||||
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
||||||
Application.Run(serviceProvider.GetRequiredService<ServiceForm2>());
|
|
||||||
|
|
||||||
}
|
|
||||||
private static void ConfigureServices(ServiceCollection services)
|
|
||||||
{
|
|
||||||
string[] path = Directory.GetCurrentDirectory().Split('\\');
|
|
||||||
string pathNeed = "";
|
|
||||||
for (int i = 0; i < path.Length - 3; i++)
|
|
||||||
{
|
|
||||||
pathNeed += path[i] + "\\";
|
|
||||||
}
|
|
||||||
|
|
||||||
services.AddSingleton<ServiceForm2>().AddLogging(option =>
|
|
||||||
{
|
|
||||||
option.SetMinimumLevel(LogLevel.Information);
|
|
||||||
option.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(
|
|
||||||
new ConfigurationBuilder().AddJsonFile(
|
|
||||||
$"{pathNeed}serilog.json").Build()).CreateLogger());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,16 +8,4 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
|
||||||
<PackageReference Include="Serilog" Version="4.0.0" />
|
|
||||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
2
ProjectCruiser/ServiceForm2.Designer.cs
generated
2
ProjectCruiser/ServiceForm2.Designer.cs
generated
@ -221,7 +221,7 @@
|
|||||||
btnDelete.TabIndex = 4;
|
btnDelete.TabIndex = 4;
|
||||||
btnDelete.Text = "Delete";
|
btnDelete.Text = "Delete";
|
||||||
btnDelete.UseVisualStyleBackColor = true;
|
btnDelete.UseVisualStyleBackColor = true;
|
||||||
btnDelete.Click += btnRemoveShip_Click;
|
btnDelete.Click += btnRemoveCar_Click;
|
||||||
//
|
//
|
||||||
// btnAddCruiser
|
// btnAddCruiser
|
||||||
//
|
//
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
using ProjectCruiser.CollectionGenericObj;
|
using ProjectCruiser.CollectionGenericObj;
|
||||||
using ProjectCruiser.DrawningSamples;
|
using ProjectCruiser.DrawningSamples;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using ProjectCruiser.Exceptions;
|
|
||||||
// using NLog.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace ProjectCruiser;
|
namespace ProjectCruiser;
|
||||||
|
|
||||||
public partial class ServiceForm2 : Form
|
public partial class ServiceForm2 : Form
|
||||||
@ -13,16 +9,10 @@ public partial class ServiceForm2 : Form
|
|||||||
|
|
||||||
private readonly StorageCollection<DrawningBase> _storageCollection;
|
private readonly StorageCollection<DrawningBase> _storageCollection;
|
||||||
|
|
||||||
// Логер
|
public ServiceForm2()
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
// Конструктор > logger
|
|
||||||
public ServiceForm2(ILogger<ServiceForm2> logger)
|
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_storageCollection = new();
|
_storageCollection = new();
|
||||||
_logger = logger;
|
|
||||||
_logger.LogInformation("> Form is loaded successfully");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Выбор компании
|
// Выбор компании
|
||||||
@ -31,10 +21,13 @@ public partial class ServiceForm2 : Form
|
|||||||
toolPanel.Enabled = false;
|
toolPanel.Enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Color picker (default : random) <...>
|
||||||
|
|
||||||
// Добавление корабля
|
// Добавление корабля
|
||||||
private void btnAddTransport_Click(object sender, EventArgs e)
|
private void btnAddTransport_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
EditorForm3 form3 = new();
|
EditorForm3 form3 = new();
|
||||||
|
// TODO передать метод :
|
||||||
form3.AddEvent(CreateObject);
|
form3.AddEvent(CreateObject);
|
||||||
form3.Show();
|
form3.Show();
|
||||||
}
|
}
|
||||||
@ -42,53 +35,37 @@ public partial class ServiceForm2 : Form
|
|||||||
// Создание объекта класса-перемещения
|
// Создание объекта класса-перемещения
|
||||||
private void CreateObject(DrawningBase? ship)
|
private void CreateObject(DrawningBase? ship)
|
||||||
{
|
{
|
||||||
try
|
if (_company == null || ship == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_company + ship != -1)
|
||||||
{
|
{
|
||||||
if (_company == null || ship == null)
|
|
||||||
{
|
|
||||||
throw new NullReferenceException(" > No existing collections to save");
|
|
||||||
}
|
|
||||||
|
|
||||||
int count = _company + ship;
|
|
||||||
|
|
||||||
MessageBox.Show("> Object was added");
|
MessageBox.Show("> Object was added");
|
||||||
pictureBox.Image = _company.Show();
|
pictureBox.Image = _company.Show();
|
||||||
|
|
||||||
_logger.LogInformation("> Adding object succeed {ship} at {count} position", ship, count);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("[!] Failed to add object\n" + ex.Message);
|
MessageBox.Show("[!] Failed to add object");
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Удаление объекта
|
// Удаление объекта
|
||||||
private void btnRemoveShip_Click(object sender, EventArgs e)
|
private void btnRemoveCar_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)
|
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)
|
||||||
|| _company == null) return;
|
|| _company == null) return;
|
||||||
|
|
||||||
if (MessageBox.Show("[*] Remove object: Are you sure?", "Remove",
|
if (MessageBox.Show("[*] Remove object: Are you sure?", "Remove",
|
||||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
|
||||||
|
|
||||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||||
|
|
||||||
try
|
if (_company - Convert.ToInt32(maskedTextBoxPosition.Text) != null)
|
||||||
{
|
{
|
||||||
if (_company - pos != null)
|
MessageBox.Show("> Object was removed");
|
||||||
{
|
pictureBox.Image = _company.Show();
|
||||||
MessageBox.Show("> Object was removed");
|
|
||||||
pictureBox.Image = _company.Show();
|
|
||||||
_logger.LogInformation("Object at " +
|
|
||||||
pos + "position was deleted successfully");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
MessageBox.Show("[!] Failed to remove object");
|
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
||||||
}
|
}
|
||||||
|
else MessageBox.Show("[!] Failed to remove object");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Передача объекта в другую форму
|
// Передача объекта в другую форму
|
||||||
@ -98,37 +75,27 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DrawningBase? ship = null;
|
DrawningBase? car = null;
|
||||||
int counter = 100;
|
int counter = 100;
|
||||||
while (ship == null)
|
while (car == null)
|
||||||
{
|
{
|
||||||
try
|
car = _company.GetRandomObject();
|
||||||
|
counter--;
|
||||||
|
if (counter <= 0)
|
||||||
{
|
{
|
||||||
ship = _company.GetRandomObject();
|
break;
|
||||||
counter--;
|
|
||||||
|
|
||||||
if (counter <= 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if (car == null)
|
||||||
if (ship == null)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
OceanForm1 form = new() { SetShip = ship };
|
OceanForm1 form = new() { SetShip = car };
|
||||||
form.ShowDialog();
|
form.ShowDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Перерисовка коллекции
|
||||||
private void btnRefresh_Click(object sender, EventArgs e)
|
private void btnRefresh_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (_company == null)
|
if (_company == null)
|
||||||
@ -157,17 +124,7 @@ public partial class ServiceForm2 : Form
|
|||||||
collType = CollectionType.List;
|
collType = CollectionType.List;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
_storageCollection.AddCollection(maskedTxtBoxCName.Text, collType);
|
||||||
{
|
|
||||||
_storageCollection.AddCollection(maskedTxtBoxCName.Text, collType);
|
|
||||||
_logger.LogInformation("Adding collection succeed : {Name}, {Type}", maskedTxtBoxCName.Text, collType);
|
|
||||||
}
|
|
||||||
catch (NullReferenceException ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,21 +134,13 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
MessageBox.Show("Collection was not choosed");
|
MessageBox.Show("Collection was not choosed");
|
||||||
return;
|
return;
|
||||||
} if (MessageBox.Show("Are you sure?", "Removing",
|
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Question)
|
|
||||||
!= DialogResult.OK) return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_storageCollection.DelCollection(listBox.SelectedItem.ToString());
|
|
||||||
RefreshListBoxItems();
|
|
||||||
_logger.LogInformation("Removing collection succeed : {Name}", listBox.SelectedItem.ToString);
|
|
||||||
}
|
}
|
||||||
catch (NullReferenceException ex)
|
if (MessageBox.Show("Are you sure?", "Removing", MessageBoxButtons.OK, MessageBoxIcon.Question) != DialogResult.OK)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.Message);
|
return;
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
||||||
}
|
}
|
||||||
|
_storageCollection.DelCollection(listBox.SelectedItem.ToString());
|
||||||
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshListBoxItems()
|
private void RefreshListBoxItems()
|
||||||
@ -228,7 +177,7 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
case "Storage":
|
case "Storage":
|
||||||
_company = new ShipSharingService(pictureBox.Width,
|
_company = new ShipSharingService(pictureBox.Width,
|
||||||
pictureBox.Height, collection);
|
pictureBox.Height, collection);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,17 +190,15 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
try
|
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
||||||
{
|
{
|
||||||
_storageCollection.SaveData(saveFileDialog.FileName);
|
|
||||||
MessageBox.Show(" < Saved succesfully >",
|
MessageBox.Show(" < Saved succesfully >",
|
||||||
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
_logger.LogInformation("Saving to file : {filename}", saveFileDialog.FileName);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show(ex.Message, "Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("< Failed to save >", "Result :",
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,27 +208,16 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
try
|
if (_storageCollection.LoadData(openFileDialog.FileName))
|
||||||
{
|
{
|
||||||
_storageCollection.LoadData(openFileDialog.FileName);
|
|
||||||
// LoadData() : Exceptions
|
|
||||||
// FileNotFoundException
|
|
||||||
// NullReferenceException
|
|
||||||
// InvalidDataException
|
|
||||||
// IndexOutOfRangeException
|
|
||||||
// CollectionOverflowException
|
|
||||||
|
|
||||||
MessageBox.Show(" < Loaded succesfully >",
|
MessageBox.Show(" < Loaded succesfully >",
|
||||||
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
_logger.LogInformation("Loading from file : {Filename}", openFileDialog.FileName);
|
|
||||||
|
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
{
|
{
|
||||||
MessageBox.Show("< Failed to load >" + ex.Message,
|
MessageBox.Show("< Failed to load >", "Result :",
|
||||||
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"Serilog": {
|
|
||||||
"Using": [ "Serilog.Sinks.File" ],
|
|
||||||
"MinimumLevel": "Debug",
|
|
||||||
"WriteTo": [
|
|
||||||
{
|
|
||||||
"Name": "File",
|
|
||||||
"Args": { "path": "log.log" }
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"Properties": {
|
|
||||||
"Application": "Sample"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user