4 лаба готова.

This commit is contained in:
Андрей Байгулов 2023-10-29 21:27:26 +04:00
parent e5619197bc
commit 275b551026
3 changed files with 41 additions and 20 deletions

View File

@ -15,7 +15,7 @@ namespace ProjectElectricLocomotive.Generics
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200; private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 90; private readonly int _placeSizeHeight = 120;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public LocomotivesGenericCollection(int picWidth, int picHeight) public LocomotivesGenericCollection(int picWidth, int picHeight)
{ {

View File

@ -15,17 +15,17 @@ namespace ProjectElectricLocomotive
/// Словарь (хранилище) /// Словарь (хранилище)
/// </summary> /// </summary>
readonly Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage; readonly Dictionary<string, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotivesStorage;
/// <summary>
/// Возвращение списка названий наборов /// Возвращение списка названий наборов
/// </summary> /// </summary>
public List<string> Keys => _locomotivesStorage.Keys.ToList(); public List<string> Keys => _locomotivesStorage.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth; private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight; private readonly int _pictureHeight;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
@ -43,16 +43,25 @@ namespace ProjectElectricLocomotive
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void AddSet(string name) public void AddSet(string name)
{ {
// TODO Прописать логику для добавления if (!_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
}
} }
/// <summary> /// <summary>
/// Удаление набора /// Удаление набора
/// </summary> /// </summary>
/// <param name="name">Название набора</param> /// <param name="name">Название набора</param>
public void DelSet(string name) public void DelSet(string name)
{ {
// TODO Прописать логику для удаления if (_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Remove(name);
}
} }
/// <summary> /// <summary>
/// Доступ к набору /// Доступ к набору
/// </summary> /// </summary>
@ -63,10 +72,12 @@ namespace ProjectElectricLocomotive
{ {
get get
{ {
// TODO Продумать логику получения набора if (_locomotivesStorage.ContainsKey(ind))
{
return _locomotivesStorage[ind];
}
return null; return null;
} }
} }
} }
} }

View File

@ -10,33 +10,40 @@ namespace ProjectElectricLocomotive.Generics
where T : class where T : class
{ {
private readonly List<T?> _places; private readonly List<T?> _places;
public int Count => _places.Count; public int Count => _places.Count;
/// Максимальное количество объектов в списке
private readonly int _maxCount; private readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_maxCount = count; _maxCount = count;
_places = new List<T?>(count); _places = new List<T?>(count);
} }
public int Insert(T locomotive)
/// Добавление объекта в набор
public int Insert(T loco)
{ {
return Insert(locomotive, 0); return Insert(loco, 0);
} }
public int Insert(T locomotive, int position)
public int Insert(T loco, int position)
{ {
if (position < 0 || position >= _maxCount) return -1; if (position < 0 || position >= _maxCount) return -1;
_places.Insert(position, locomotive); _places.Insert(position, loco);
return position; return position;
} }
public T? Remove(int position) public T? Remove(int position)
{ {
if (position >= Count || position < 0) if (position >= Count || position < 0)
{
return null; return null;
}
T? tmp = _places[position]; T? tmp = _places[position];
_places[position] = null; _places[position] = null;
return tmp; return tmp;
} }
public T? this[int position] public T? this[int position]
{ {
get get
@ -50,17 +57,20 @@ namespace ProjectElectricLocomotive.Generics
_places.Insert(position, value); _places.Insert(position, value);
} }
} }
public IEnumerable<T?> GetLocomotives(int? maxLocomotives = null) /// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetLocomotives(int? maxLocos = null)
{ {
for (int i = 0; i < _places.Count; ++i) for (int i = 0; i < _places.Count; ++i)
{ {
yield return _places[i]; yield return _places[i];
if (maxLocomotives.HasValue && i == maxLocomotives.Value) if (maxLocos.HasValue && i == maxLocos.Value)
{ {
yield break; yield break;
} }
} }
} }
} }
} }