This commit is contained in:
KirillFilippow 2023-12-22 13:51:04 +04:00
parent 4d7de3bca3
commit dc6c31f382
13 changed files with 267 additions and 59 deletions

View File

@ -7,7 +7,8 @@ using static ProjectContainerShip.Direction;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectContainerShip.MovementStrategy
{/// <summary>
{
/// <summary>
/// Класс-стратегия перемещения объекта
/// </summary>
public abstract class AbstractStrategy

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.Generics;
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip.MovementStrategy;
@ -46,6 +47,7 @@ namespace ProjectContainerShip.Generics
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public ContainerGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -60,14 +62,14 @@ namespace ProjectContainerShip.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int? operator +(ContainerGenericCollection<T, U> collect, T?
public static bool operator +(ContainerGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect?._collection.Insert(obj) ?? -1;
return collect?._collection.Insert(obj, new DrawningContainerShipEqutables()) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания

View File

@ -1,4 +1,5 @@
using ContainerShip.Exceptions;
using ContainerShip.Generics;
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip.Generics;
using ProjectContainerShip.MovementStrategy;
@ -18,12 +19,12 @@ namespace ProjectContainerShip
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, ContainerGenericCollection<DrawningShip,
readonly Dictionary<ContainerShipCollectionInfo, ContainerGenericCollection<DrawningShip,
DrawningObjectShip>> _shipStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _shipStorages.Keys.ToList();
public List<ContainerShipCollectionInfo> Keys => _shipStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -51,8 +52,8 @@ namespace ProjectContainerShip
/// <param name="pictureHeight"></param>
public ContainerGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string,
ContainerGenericCollection<DrawningShip, DrawningObjectShip>>();
_shipStorages = new Dictionary<ContainerShipCollectionInfo, ContainerGenericCollection<DrawningShip,
DrawningObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -62,10 +63,10 @@ namespace ProjectContainerShip
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_shipStorages.ContainsKey(name))
if (!_shipStorages.ContainsKey(new ContainerShipCollectionInfo(name, string.Empty)))
{
var shipCollection = new ContainerGenericCollection<DrawningShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
_shipStorages.Add(name, shipCollection);
var lincornCollection = new ContainerGenericCollection<DrawningShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
_shipStorages.Add(new ContainerShipCollectionInfo(name, string.Empty), lincornCollection);
}
}
/// <summary>
@ -74,9 +75,9 @@ namespace ProjectContainerShip
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_shipStorages.ContainsKey(name))
if (_shipStorages.ContainsKey(new ContainerShipCollectionInfo(name, string.Empty)))
{
_shipStorages.Remove(name);
_shipStorages.Remove(new ContainerShipCollectionInfo(name, string.Empty));
}
}
/// <summary>
@ -88,9 +89,10 @@ namespace ProjectContainerShip
{
get
{
if (_shipStorages.ContainsKey(ind))
ContainerShipCollectionInfo indObj = new ContainerShipCollectionInfo(ind, string.Empty);
if (_shipStorages.ContainsKey(indObj))
{
return _shipStorages[ind];
return _shipStorages[indObj];
}
return null;
}
@ -107,7 +109,7 @@ namespace ProjectContainerShip
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, ContainerGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
foreach (KeyValuePair<ContainerShipCollectionInfo, ContainerGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
{
StringBuilder records = new();
foreach (DrawningShip? elem in record.Value.GetShip)
@ -120,7 +122,6 @@ namespace ProjectContainerShip
{
throw new Exception("Невалидная операция, нет данных для сохранения");
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"shipStorage{Environment.NewLine}{data}");
@ -181,7 +182,7 @@ namespace ProjectContainerShip
}
}
}
_shipStorages.Add(name, collection);
_shipStorages.Add(new ContainerShipCollectionInfo(name, string.Empty), collection);
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip.Generics
{
internal class ContainerShipCollectionInfo : IEquatable<ContainerShipCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public ContainerShipCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(ContainerShipCollectionInfo? other)
{
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,47 @@
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectContainerShip.Entities;
namespace ContainerShip.Generics
{
internal class ContainerShipCompareByColor : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityShip == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityShip.BodyColor.Name != y.EntityShip.BodyColor.Name)
{
return x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name);
}
if (x.GetType().Name != y.GetType().Name)
{
if (x is EntityShip)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is EntityContainerShip)
{
EntityContainerShip EntityX = (EntityContainerShip)x.EntityShip;
EntityContainerShip EntityY = (EntityContainerShip)y.EntityShip;
if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name)
{
return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name);
}
}
var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight);
}
}
}

View File

@ -0,0 +1,28 @@
using ProjectContainerShip.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip.Generics
{
internal class ContainerShipCompareByType : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityShip == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight);
}
}
}

View File

@ -37,7 +37,7 @@ namespace ProjectContainerShip.DrawningObjects
{
return;
}
Brush additionalBrush = new SolidBrush(containerShip.AdditionalColor);
Brush additionalBrush = new SolidBrush(containerShip.AdditionalColor);
if (containerShip.Container)
{
g.FillRectangle(additionalBrush, _startPosX + 55, _startPosY + 7, 35, 5);

View File

@ -0,0 +1,59 @@
using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectContainerShip.Entities;
namespace ContainerShip.Generics
{
internal class DrawningContainerShipEqutables : IEqualityComparer<DrawningShip?>
{
public bool Equals(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityShip == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityShip.Speed != y.EntityShip.Speed)
{
return false;
}
if (x.EntityShip.Weight != y.EntityShip.Weight)
{
return false;
}
if (x.EntityShip.BodyColor != y.EntityShip.BodyColor)
{
return false;
}
if (x is EntityContainerShip && y is EntityContainerShip)
{
EntityContainerShip EntityX = (EntityContainerShip)x.EntityShip;
EntityContainerShip EntityY = (EntityContainerShip)y.EntityShip;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
if (EntityX.Crane != EntityY.Crane)
return false;
if (EntityX.Container != EntityY.Container)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningShip obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -22,7 +22,7 @@ namespace ProjectContainerShip.Entities
/// </summary>
public bool Container { get; private set; }
/// <summary>
/// Инициализация полей объекта-класса контейнеровоз
/// Инициализация полей объекта-класса контейнеровоза
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес </param>

View File

@ -34,6 +34,8 @@
buttonRemoveEx = new Button();
buttonRefreshCollection = new Button();
groupBox1 = new GroupBox();
ButtonSortByColor = new Button();
ButtonSortByType = new Button();
menuStrip = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
@ -99,6 +101,8 @@
//
// groupBox1
//
groupBox1.Controls.Add(ButtonSortByColor);
groupBox1.Controls.Add(ButtonSortByType);
groupBox1.Controls.Add(menuStrip);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(buttonAddEx);
@ -112,6 +116,28 @@
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// ButtonSortByColor
//
ButtonSortByColor.Location = new Point(6, 116);
ButtonSortByColor.Margin = new Padding(3, 4, 3, 4);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(110, 51);
ButtonSortByColor.TabIndex = 11;
ButtonSortByColor.Text = "Сортировка по цвету";
ButtonSortByColor.UseVisualStyleBackColor = true;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// ButtonSortByType
//
ButtonSortByType.Location = new Point(6, 57);
ButtonSortByType.Margin = new Padding(3, 4, 3, 4);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(108, 51);
ButtonSortByType.TabIndex = 10;
ButtonSortByType.Text = "Сортировка по типу";
ButtonSortByType.UseVisualStyleBackColor = true;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
@ -133,14 +159,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Size = new Size(177, 26);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click_1;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Size = new Size(177, 26);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -150,9 +176,9 @@
groupBox2.Controls.Add(buttonDelObject);
groupBox2.Controls.Add(listBoxStorages);
groupBox2.Controls.Add(buttonAddObject);
groupBox2.Location = new Point(6, 151);
groupBox2.Location = new Point(6, 174);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(116, 214);
groupBox2.Size = new Size(116, 191);
groupBox2.TabIndex = 5;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
@ -166,7 +192,7 @@
//
// buttonDelObject
//
buttonDelObject.Location = new Point(13, 162);
buttonDelObject.Location = new Point(13, 158);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(95, 27);
buttonDelObject.TabIndex = 8;
@ -240,5 +266,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -3,6 +3,8 @@ using ProjectContainerShip.DrawningObjects;
using ProjectContainerShip.Generics;
using ProjectContainerShip.MovementStrategy;
using Microsoft.Extensions.Logging;
using ContainerShip.Exceptions;
using ContainerShip.Generics;
namespace ProjectContainerShip
{
@ -37,7 +39,7 @@ namespace ProjectContainerShip
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
@ -159,15 +161,24 @@ namespace ProjectContainerShip
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
try
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowContainer();
_logger.LogInformation($"Удален объект из набора {listBoxStorages.SelectedItem.ToString()}");
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowContainer();
_logger.LogInformation($"Удален объект из набора {listBoxStorages.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Не удалось удалить объект");
_logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}");
}
}
else
catch (ContainerShipNotFoundException ex)
{
MessageBox.Show("Не удалось удалить объект"); _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}");
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}");
}
}
/// <summary>
@ -234,6 +245,24 @@ namespace ProjectContainerShip
}
}
}
private void CompareContainerShip(IComparer<DrawningShip?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowContainer();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareContainerShip(new ContainerShipCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareContainerShip(new ContainerShipCompareByColor());
}
}

View File

@ -31,5 +31,7 @@ namespace ProjectContainerShip.MovementStrategy
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
void SetPosition(int x, int y);
void Draw(Graphics g);
}
}

View File

@ -35,45 +35,29 @@ namespace ProjectContainerShip.Generics
_maxCount = count;
_places = new List<T?>(count);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="ship">Добавляемый контейнеровоз</param>
/// <returns></returns>
public int Insert(T ship)
public bool Insert(T ship, IEqualityComparer<T>? equal = null)
{
if (_places.Count == 0)
{
_places.Add(ship);
return 0;
}
else
{
if (_places.Count < _maxCount)
{
_places.Add(ship);
for (int i = 0; i < _places.Count; i++)
{
T temp = _places[i];
_places[i] = _places[_places.Count - 1];
_places[_places.Count - 1] = temp;
}
return 0;
}
else
{
throw new StorageOverflowException(_places.Count);
}
}
Insert(ship, 0, equal);
return true;
}
public bool Insert(T ship, int position)
public bool Insert(T ship, int position, IEqualityComparer<T>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new ContainerShipNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(position);
_places.Insert(0, ship);
if (equal != null)
{
if (_places.Contains(ship, equal))
throw new ArgumentException(nameof(ship));
}
_places.Insert(position, ship);
return true;
}
/// <summary>