Merge branch 'lab8' into lab7

This commit is contained in:
rinat 2024-05-21 11:14:32 +04:00
commit 0320c06148
13 changed files with 600 additions and 345 deletions

View File

@ -1,5 +1,6 @@
using WarmlyShip.CollectionGenericObjects; using WarmlyShip.CollectionGenericObjects;
using WarmlyShip.Drawnings; using WarmlyShip.Drawnings;
using WarmlyShip.Drawningsp;
namespace WarmlyShip.CollectionGenericObjects; namespace WarmlyShip.CollectionGenericObjects;
@ -35,7 +36,7 @@ public abstract class AbstractCompany
public static int operator +(AbstractCompany company, DrawningShip ship) public static int operator +(AbstractCompany company, DrawningShip ship)
{ {
return company._collection.Insert(ship); return company._collection.Insert(ship, new DrawningShipEqutables());
} }
public static DrawningShip operator -(AbstractCompany company, int position) public static DrawningShip operator -(AbstractCompany company, int position)
@ -67,6 +68,7 @@ public abstract class AbstractCompany
return bitmap; return bitmap;
} }
public void Sort(IComparer<DrawningShip?> comparer) => _collection?.CollectionSort(comparer);
protected abstract void DrawBackgroud(Graphics g); protected abstract void DrawBackgroud(Graphics g);

View File

@ -0,0 +1,76 @@
namespace WarmlyShip.CollectionGenericObjects;
/// <summary>
/// Класс, хранящий информацию о коллекции
/// </summary>
public class CollectionInfo : IEquatable<CollectionInfo>
{
/// <summary>
/// Название
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Тип
/// </summary>
public CollectionType CollectionType { get; private set; }
/// <summary>
/// Описание
/// </summary>
public string Description { get; private set; }
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly string _separator = "-";
/// <summary>
/// Конструктор
/// </summary>
/// <param name="name">Название</param>
/// <param name="collectionType">Тип</param>
/// <param name="description">Описание</param>
public CollectionInfo(string name, CollectionType collectionType, string description)
{
Name = name;
CollectionType = collectionType;
Description = description;
}
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="data">Строка</param>
/// <returns>Объект или null</returns>
public static CollectionInfo? GetCollectionInfo(string data)
{
string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
if (strs.Length < 1 || strs.Length > 3)
{
return null;
}
return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty);
}
public override string ToString()
{
return Name + _separator + CollectionType + _separator + Description;
}
public bool Equals(CollectionInfo? other)
{
return Name == other?.Name;
}
public override bool Equals(object? obj)
{
return Equals(obj as CollectionInfo);
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}

View File

@ -11,10 +11,10 @@ public interface ICollectionGenericObjects<T>
int MaxCount { set; get; } int MaxCount { set; get; }
int Insert(T obj); int Insert(T obj, IEqualityComparer<T?>? comparer = null);
int Insert(T obj, int position); int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
T Remove(int position); T Remove(int position);
@ -26,4 +26,6 @@ public interface ICollectionGenericObjects<T>
IEnumerable<T?> GetItems(); IEnumerable<T?> GetItems();
void CollectionSort(IComparer<T?> comparer);
} }

View File

@ -40,8 +40,16 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (comparer != null)
{
if (_collection.Contains(obj, comparer))
{
throw new ObjectNotUniqueException();
}
}
if (Count == _maxCount) if (Count == _maxCount)
{ {
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
@ -50,8 +58,16 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
return Count; return Count;
} }
public int Insert(T obj, int position) public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if (comparer != null)
{
if (_collection.Contains(obj, comparer))
{
throw new ObjectNotUniqueException();
}
}
if (Count == _maxCount) if (Count == _maxCount)
{ {
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
@ -82,4 +98,8 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
yield return _collection[i]; yield return _collection[i];
} }
} }
void ICollectionGenericObjects<T>.CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
} }

View File

@ -1,5 +1,6 @@
using WarmlyShip.Exceptions; using WarmlyShip.Drawnings;
using WarmlyShip.Exceptions;
using WarmlyShip.Scripts.Exceptions;
namespace WarmlyShip.CollectionGenericObjects; namespace WarmlyShip.CollectionGenericObjects;
@ -52,8 +53,18 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (comparer != null)
{
foreach (T? item in _collection)
{
if ((comparer as IEqualityComparer<DrawningShip>).Equals(obj as DrawningShip, item as DrawningShip))
{
throw new ObjectNotUniqueException();
}
}
}
// TODO вставка в свободное место набора // TODO вставка в свободное место набора
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
{ {
@ -67,9 +78,9 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
} }
public int Insert(T obj, int position) public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= Count)
{ {
throw new PositionOutOfCollectionException(position); throw new PositionOutOfCollectionException(position);
@ -127,4 +138,8 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i]; yield return _collection[i];
} }
} }
void ICollectionGenericObjects<T>.CollectionSort(IComparer<T?> comparer)
{
Array.Sort(_collection, comparer);
}
} }

View File

@ -8,13 +8,13 @@ namespace WarmlyShip.CollectionGenericObjects;
public class StorageCollection<T> where T : DrawningShip public class StorageCollection<T> where T : DrawningShip
{ {
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages; readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList(); public List<CollectionInfo> Keys => _storages.Keys.ToList();
private readonly string _collectionKey = "CollectionStorage"; private readonly string _collectionKey = "CollectionStorage";
private readonly string _separatorForKeyValue = "|"; private readonly string _separatorForKeyValue = "|";
@ -23,33 +23,36 @@ public class StorageCollection<T> where T : DrawningShip
public StorageCollection() public StorageCollection()
{ {
_storages = new Dictionary<string, ICollectionGenericObjects<T>>(); _storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
} }
public void AddCollection(string name, CollectionType collectionType) public void AddCollection(string name, CollectionType collectionType)
{ {
if (_storages.ContainsKey(name) || collectionType == CollectionType.None) CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
if (_storages.ContainsKey(collectionInfo) || collectionInfo.CollectionType == CollectionType.None)
{ {
return; return;
} }
switch (collectionType) switch (collectionInfo.CollectionType)
{ {
case CollectionType.Massive: case CollectionType.Massive:
_storages[name] = new MassiveGenericObjects<T>(); _storages[collectionInfo] = new MassiveGenericObjects<T>();
break; break;
case CollectionType.List: case CollectionType.List:
_storages[name] = new ListGenericObjects<T>(); _storages[collectionInfo] = new ListGenericObjects<T>();
break; break;
} }
} }
public void DelCollection(string name) public void DelCollection(string name)
{ {
if (_storages.ContainsKey(name) && name != null) CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
if (_storages.ContainsKey(collectionInfo) && collectionInfo != null)
{ {
_storages.Remove(name); _storages.Remove(collectionInfo);
} }
} }
@ -57,9 +60,10 @@ public class StorageCollection<T> where T : DrawningShip
{ {
get get
{ {
if (_storages.ContainsKey(name)) CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
if (_storages.ContainsKey(collectionInfo))
{ {
return _storages[name]; return _storages[collectionInfo];
} }
return null; return null;
} }
@ -79,7 +83,7 @@ public class StorageCollection<T> where T : DrawningShip
using (StreamWriter writer = new StreamWriter(filename)) using (StreamWriter writer = new StreamWriter(filename))
{ {
writer.Write(_collectionKey); writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages) foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
{ {
StringBuilder sb = new(); StringBuilder sb = new();
@ -92,8 +96,6 @@ public class StorageCollection<T> where T : DrawningShip
sb.Append(value.Key); sb.Append(value.Key);
sb.Append(_separatorForKeyValue); sb.Append(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.MaxCount); sb.Append(value.Value.MaxCount);
sb.Append(_separatorForKeyValue); sb.Append(_separatorForKeyValue);
@ -111,15 +113,9 @@ public class StorageCollection<T> where T : DrawningShip
writer.Write(sb); writer.Write(sb);
} }
} }
} }
/// <summary>
/// Загрузка информации по автомобилям в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public void LoadData(string filename) public void LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
@ -146,18 +142,18 @@ public class StorageCollection<T> where T : DrawningShip
while ((strs = fs.ReadLine()) != null) while ((strs = fs.ReadLine()) != null)
{ {
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 3)
{ {
continue; continue;
} }
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ?? throw new Exception("Не удалось определить информацию коллекции:" + record[0]);
throw new Exception("Не удалось определить тип коллекции: " + record[1]); ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
throw new Exception("Не удалось определить тип коллекции: " + record[1]);
collection.MaxCount = Convert.ToInt32(record[1]);
collection.MaxCount = Convert.ToInt32(record[2]); string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
if (elem?.CreateDrawningShip() is T ship) if (elem?.CreateDrawningShip() is T ship)
@ -175,7 +171,7 @@ public class StorageCollection<T> where T : DrawningShip
} }
} }
} }
_storages.Add(record[0], collection); _storages.Add(collectionInfo, collection);
} }
} }
} }

View File

@ -0,0 +1,20 @@
namespace WarmlyShip.Drawnings;
public class DrawningShipCompareByColor : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null) return 1;
if (y == null || y.EntityShip == null) return -1;
var bodycolorCompare = x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name);
if (bodycolorCompare != 0) return bodycolorCompare;
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,19 @@
namespace WarmlyShip.Drawnings;
public class DrawningShipCompareByType : IComparer<DrawningShip?>
{
public int Compare(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null) return 1;
if (y == null || y.EntityShip == null) return -1;
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

@ -0,0 +1,42 @@
using WarmlyShip.Entities;
using System.Diagnostics.CodeAnalysis;
using WarmlyShip.Drawnings;
namespace WarmlyShip.Drawningsp;
public class DrawningShipEqutables : IEqualityComparer<DrawningShip?>
{
public bool Equals(DrawningShip? x, DrawningShip? y)
{
if (x == null || x.EntityShip == null) return false;
if (y == null || y.EntityShip == null) return false;
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 DrawningWarmlyShip bigX && y is DrawningWarmlyShip bigY)
{
EntityWarmlyShip _x = (EntityWarmlyShip)x.EntityShip;
EntityWarmlyShip _y = (EntityWarmlyShip)y.EntityShip;
if (_x.FuelHole != _y.FuelHole) return false;
if (_x.Pipes != _y.Pipes) return false;
if (_x.SeckondColor != _y.SeckondColor) return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningShip obj)
{
return obj.GetHashCode();
}
}

View File

@ -1,6 +1,6 @@
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace WarmlyShip.Exceptions; namespace WarmlyShip.Scripts.Exceptions;
[Serializable] [Serializable]

View File

@ -0,0 +1,18 @@
using System.Runtime.Serialization;
namespace WarmlyShip.Exceptions;
[Serializable]
internal class ObjectNotUniqueException : ApplicationException
{
public ObjectNotUniqueException(int i) : base("В коллекции есть такой же элемент на позиции: " + i) { }
public ObjectNotUniqueException() : base() { }
public ObjectNotUniqueException(string message) : base(message) { }
public ObjectNotUniqueException(string message, Exception exception) : base(message, exception) { }
protected ObjectNotUniqueException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

View File

@ -20,302 +20,328 @@
base.Dispose(disposing); base.Dispose(disposing);
} }
#region Windows Form Designer generated code #region Windows Form Designer generated code
/// <summary> /// <summary>
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
panelCompanyTools = new Panel(); panelCompanyTools = new Panel();
buttonAddShip = new Button(); buttonAddShip = new Button();
buttonRefresh = new Button(); buttonRefresh = new Button();
maskedTextBoxPosition = new MaskedTextBox(); maskedTextBoxPosition = new MaskedTextBox();
buttonGoToCheck = new Button(); buttonGoToCheck = new Button();
buttonRemoveShip = new Button(); buttonRemoveShip = new Button();
buttonCreateCompany = new Button(); buttonCreateCompany = new Button();
panelStoreage = new Panel(); panelStoreage = new Panel();
buttonCollectionDel = new Button(); buttonCollectionDel = new Button();
listBoxCollection = new ListBox(); listBoxCollection = new ListBox();
buttonCollectionAdd = new Button(); buttonCollectionAdd = new Button();
radioButtonList = new RadioButton(); radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton(); radioButtonMassive = new RadioButton();
textBoxCollectionName = new TextBox(); textBoxCollectionName = new TextBox();
labelCollectionName = new Label(); labelCollectionName = new Label();
comboBoxSelectorCompany = new ComboBox(); comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox(); pictureBox = new PictureBox();
menuStrip = new MenuStrip(); menuStrip = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem(); файлToolStripMenuItem = new ToolStripMenuItem();
saveToolStripMenuItem = new ToolStripMenuItem(); saveToolStripMenuItem = new ToolStripMenuItem();
loadToolStripMenuItem = new ToolStripMenuItem(); loadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog(); saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog(); openFileDialog = new OpenFileDialog();
groupBoxTools.SuspendLayout(); buttonSortByType = new Button();
panelCompanyTools.SuspendLayout(); buttonSortByColor = new Button();
panelStoreage.SuspendLayout(); groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); panelCompanyTools.SuspendLayout();
menuStrip.SuspendLayout(); panelStoreage.SuspendLayout();
SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
// menuStrip.SuspendLayout();
// groupBoxTools SuspendLayout();
// //
groupBoxTools.Controls.Add(panelCompanyTools); // groupBoxTools
groupBoxTools.Controls.Add(buttonCreateCompany); //
groupBoxTools.Controls.Add(panelStoreage); groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Controls.Add(buttonCreateCompany);
groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Controls.Add(panelStoreage);
groupBoxTools.Location = new Point(961, 24); groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Size = new Size(187, 521); groupBoxTools.Location = new Point(961, 24);
groupBoxTools.TabIndex = 0; groupBoxTools.Name = "groupBoxTools";
groupBoxTools.TabStop = false; groupBoxTools.Size = new Size(187, 532);
groupBoxTools.Text = "Инструменты"; groupBoxTools.TabIndex = 0;
// groupBoxTools.TabStop = false;
// panelCompanyTools groupBoxTools.Text = "Инструменты";
// //
panelCompanyTools.Controls.Add(buttonAddShip); // panelCompanyTools
panelCompanyTools.Controls.Add(buttonRefresh); //
panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonRemoveShip); panelCompanyTools.Controls.Add(buttonAddShip);
panelCompanyTools.Enabled = false; panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Location = new Point(6, 292); panelCompanyTools.Controls.Add(maskedTextBoxPosition);
panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Size = new Size(178, 234); panelCompanyTools.Controls.Add(buttonRemoveShip);
panelCompanyTools.TabIndex = 4; panelCompanyTools.Enabled = false;
// panelCompanyTools.Location = new Point(6, 283);
// buttonAddShip panelCompanyTools.Name = "panelCompanyTools";
// panelCompanyTools.Size = new Size(178, 249);
buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; panelCompanyTools.TabIndex = 4;
buttonAddShip.Location = new Point(3, 3); //
buttonAddShip.Name = "buttonAddShip"; // buttonAddShip
buttonAddShip.Size = new Size(166, 36); //
buttonAddShip.TabIndex = 1; buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddShip.Text = "Добавление корабля"; buttonAddShip.Location = new Point(3, 3);
buttonAddShip.UseVisualStyleBackColor = true; buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Click += ButtonAddShip_Click; buttonAddShip.Size = new Size(166, 36);
// buttonAddShip.TabIndex = 1;
// buttonRefresh buttonAddShip.Text = "Добавление корабля";
// buttonAddShip.UseVisualStyleBackColor = true;
buttonRefresh.Anchor = AnchorStyles.None; buttonAddShip.Click += ButtonAddShip_Click;
buttonRefresh.Location = new Point(3, 156); //
buttonRefresh.Name = "buttonRefresh"; // buttonRefresh
buttonRefresh.Size = new Size(169, 28); //
buttonRefresh.TabIndex = 7; buttonRefresh.Anchor = AnchorStyles.None;
buttonRefresh.Text = "Обновить"; buttonRefresh.Location = new Point(3, 162);
buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Click += ButtonRefresh_Click; buttonRefresh.Size = new Size(169, 28);
// buttonRefresh.TabIndex = 7;
// maskedTextBoxPosition buttonRefresh.Text = "Обновить";
// buttonRefresh.UseVisualStyleBackColor = true;
maskedTextBoxPosition.Location = new Point(6, 45); buttonRefresh.Click += ButtonRefresh_Click;
maskedTextBoxPosition.Mask = "00"; //
maskedTextBoxPosition.Name = "maskedTextBoxPosition"; // maskedTextBoxPosition
maskedTextBoxPosition.Size = new Size(169, 23); //
maskedTextBoxPosition.TabIndex = 4; maskedTextBoxPosition.Location = new Point(3, 45);
maskedTextBoxPosition.ValidatingType = typeof(int); maskedTextBoxPosition.Mask = "00";
// maskedTextBoxPosition.Name = "maskedTextBoxPosition";
// buttonGoToCheck maskedTextBoxPosition.Size = new Size(169, 23);
// maskedTextBoxPosition.TabIndex = 4;
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; maskedTextBoxPosition.ValidatingType = typeof(int);
buttonGoToCheck.Location = new Point(6, 116); //
buttonGoToCheck.Name = "buttonGoToCheck"; // buttonGoToCheck
buttonGoToCheck.Size = new Size(166, 36); //
buttonGoToCheck.TabIndex = 6; buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.Location = new Point(3, 116);
buttonGoToCheck.UseVisualStyleBackColor = true; buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Click += ButtonGoToCheck_Click; buttonGoToCheck.Size = new Size(166, 36);
// buttonGoToCheck.TabIndex = 6;
// buttonRemoveShip buttonGoToCheck.Text = "Передать на тесты";
// buttonGoToCheck.UseVisualStyleBackColor = true;
buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonGoToCheck.Click += ButtonGoToCheck_Click;
buttonRemoveShip.Location = new Point(6, 74); //
buttonRemoveShip.Name = "buttonRemoveShip"; // buttonRemoveShip
buttonRemoveShip.Size = new Size(166, 36); //
buttonRemoveShip.TabIndex = 5; buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveShip.Text = "Удаление судна"; buttonRemoveShip.Location = new Point(3, 74);
buttonRemoveShip.UseVisualStyleBackColor = true; buttonRemoveShip.Name = "buttonRemoveShip";
buttonRemoveShip.Click += ButtonRemoveShip_Click; buttonRemoveShip.Size = new Size(166, 36);
// buttonRemoveShip.TabIndex = 5;
// buttonCreateCompany buttonRemoveShip.Text = "Удаление судна";
// buttonRemoveShip.UseVisualStyleBackColor = true;
buttonCreateCompany.Location = new Point(6, 263); buttonRemoveShip.Click += ButtonRemoveShip_Click;
buttonCreateCompany.Name = "buttonCreateCompany"; //
buttonCreateCompany.Size = new Size(175, 23); // buttonCreateCompany
buttonCreateCompany.TabIndex = 7; //
buttonCreateCompany.Text = "Создать компанию"; buttonCreateCompany.Location = new Point(6, 254);
buttonCreateCompany.UseVisualStyleBackColor = false; buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Click += ButtonCreateCompany_Click; buttonCreateCompany.Size = new Size(175, 23);
// buttonCreateCompany.TabIndex = 7;
// panelStoreage buttonCreateCompany.Text = "Создать компанию";
// buttonCreateCompany.UseVisualStyleBackColor = false;
panelStoreage.Controls.Add(buttonCollectionDel); buttonCreateCompany.Click += ButtonCreateCompany_Click;
panelStoreage.Controls.Add(listBoxCollection); //
panelStoreage.Controls.Add(buttonCollectionAdd); // panelStoreage
panelStoreage.Controls.Add(radioButtonList); //
panelStoreage.Controls.Add(radioButtonMassive); panelStoreage.Controls.Add(buttonCollectionDel);
panelStoreage.Controls.Add(textBoxCollectionName); panelStoreage.Controls.Add(listBoxCollection);
panelStoreage.Controls.Add(labelCollectionName); panelStoreage.Controls.Add(buttonCollectionAdd);
panelStoreage.Dock = DockStyle.Top; panelStoreage.Controls.Add(radioButtonList);
panelStoreage.Location = new Point(3, 19); panelStoreage.Controls.Add(radioButtonMassive);
panelStoreage.Name = "panelStoreage"; panelStoreage.Controls.Add(textBoxCollectionName);
panelStoreage.Size = new Size(181, 213); panelStoreage.Controls.Add(labelCollectionName);
panelStoreage.TabIndex = 9; panelStoreage.Dock = DockStyle.Top;
// panelStoreage.Location = new Point(3, 19);
// buttonCollectionDel panelStoreage.Name = "panelStoreage";
// panelStoreage.Size = new Size(181, 200);
buttonCollectionDel.Location = new Point(0, 186); panelStoreage.TabIndex = 9;
buttonCollectionDel.Name = "buttonCollectionDel"; //
buttonCollectionDel.Size = new Size(175, 23); // buttonCollectionDel
buttonCollectionDel.TabIndex = 6; //
buttonCollectionDel.Text = "Удалить коллекцию"; buttonCollectionDel.Location = new Point(0, 171);
buttonCollectionDel.UseVisualStyleBackColor = false; buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Click += ButtonCollectionDel_Click; buttonCollectionDel.Size = new Size(175, 23);
// buttonCollectionDel.TabIndex = 6;
// listBoxCollection buttonCollectionDel.Text = "Удалить коллекцию";
// buttonCollectionDel.UseVisualStyleBackColor = false;
listBoxCollection.FormattingEnabled = true; buttonCollectionDel.Click += ButtonCollectionDel_Click;
listBoxCollection.ItemHeight = 15; //
listBoxCollection.Location = new Point(3, 101); // listBoxCollection
listBoxCollection.Name = "listBoxCollection"; //
listBoxCollection.Size = new Size(175, 79); listBoxCollection.FormattingEnabled = true;
listBoxCollection.TabIndex = 5; listBoxCollection.ItemHeight = 15;
// listBoxCollection.Location = new Point(3, 101);
// buttonCollectionAdd listBoxCollection.Name = "listBoxCollection";
// listBoxCollection.Size = new Size(175, 64);
buttonCollectionAdd.Location = new Point(3, 72); listBoxCollection.TabIndex = 5;
buttonCollectionAdd.Name = "buttonCollectionAdd"; //
buttonCollectionAdd.Size = new Size(175, 23); // buttonCollectionAdd
buttonCollectionAdd.TabIndex = 4; //
buttonCollectionAdd.Text = "Добавить коллекцию"; buttonCollectionAdd.Location = new Point(3, 72);
buttonCollectionAdd.UseVisualStyleBackColor = false; buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Click += ButtonCollectionAdd_Click; buttonCollectionAdd.Size = new Size(175, 23);
// buttonCollectionAdd.TabIndex = 4;
// radioButtonList buttonCollectionAdd.Text = "Добавить коллекцию";
// buttonCollectionAdd.UseVisualStyleBackColor = false;
radioButtonList.AutoSize = true; buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
radioButtonList.Location = new Point(106, 47); //
radioButtonList.Name = "radioButtonList"; // radioButtonList
radioButtonList.Size = new Size(66, 19); //
radioButtonList.TabIndex = 3; radioButtonList.AutoSize = true;
radioButtonList.TabStop = true; radioButtonList.Location = new Point(106, 47);
radioButtonList.Text = "Список"; radioButtonList.Name = "radioButtonList";
radioButtonList.UseVisualStyleBackColor = true; radioButtonList.Size = new Size(66, 19);
// radioButtonList.TabIndex = 3;
// radioButtonMassive radioButtonList.TabStop = true;
// radioButtonList.Text = "Список";
radioButtonMassive.AutoSize = true; radioButtonList.UseVisualStyleBackColor = true;
radioButtonMassive.Location = new Point(3, 47); //
radioButtonMassive.Name = "radioButtonMassive"; // radioButtonMassive
radioButtonMassive.Size = new Size(67, 19); //
radioButtonMassive.TabIndex = 2; radioButtonMassive.AutoSize = true;
radioButtonMassive.TabStop = true; radioButtonMassive.Location = new Point(3, 47);
radioButtonMassive.Text = "Массив"; radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.UseVisualStyleBackColor = true; radioButtonMassive.Size = new Size(67, 19);
// radioButtonMassive.TabIndex = 2;
// textBoxCollectionName radioButtonMassive.TabStop = true;
// radioButtonMassive.Text = "Массив";
textBoxCollectionName.Location = new Point(3, 18); radioButtonMassive.UseVisualStyleBackColor = true;
textBoxCollectionName.Name = "textBoxCollectionName"; //
textBoxCollectionName.Size = new Size(175, 23); // textBoxCollectionName
textBoxCollectionName.TabIndex = 1; //
// textBoxCollectionName.Location = new Point(3, 18);
// labelCollectionName textBoxCollectionName.Name = "textBoxCollectionName";
// textBoxCollectionName.Size = new Size(175, 23);
labelCollectionName.AutoSize = true; textBoxCollectionName.TabIndex = 1;
labelCollectionName.Location = new Point(28, 0); //
labelCollectionName.Name = "labelCollectionName"; // labelCollectionName
labelCollectionName.Size = new Size(122, 15); //
labelCollectionName.TabIndex = 0; labelCollectionName.AutoSize = true;
labelCollectionName.Text = "Название коллекции"; labelCollectionName.Location = new Point(28, 0);
// labelCollectionName.Name = "labelCollectionName";
// comboBoxSelectorCompany labelCollectionName.Size = new Size(122, 15);
// labelCollectionName.TabIndex = 0;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); labelCollectionName.Text = "Название коллекции";
comboBoxSelectorCompany.Location = new Point(6, 234); //
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; // comboBoxSelectorCompany
comboBoxSelectorCompany.Size = new Size(175, 23); //
comboBoxSelectorCompany.TabIndex = 8; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; comboBoxSelectorCompany.Location = new Point(6, 225);
// comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
// pictureBox comboBoxSelectorCompany.Size = new Size(175, 23);
// comboBoxSelectorCompany.TabIndex = 8;
pictureBox.Dock = DockStyle.Fill; comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged;
pictureBox.Location = new Point(0, 24); //
pictureBox.Name = "pictureBox"; // pictureBox
pictureBox.Size = new Size(961, 521); //
pictureBox.TabIndex = 3; pictureBox.Dock = DockStyle.Fill;
pictureBox.TabStop = false; pictureBox.Location = new Point(0, 24);
// pictureBox.Name = "pictureBox";
// menuStrip pictureBox.Size = new Size(961, 532);
// pictureBox.TabIndex = 3;
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); pictureBox.TabStop = false;
menuStrip.Location = new Point(0, 0); //
menuStrip.Name = "menuStrip"; // menuStrip
menuStrip.Size = new Size(1148, 24); //
menuStrip.TabIndex = 4; menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Text = "Файл"; menuStrip.Location = new Point(0, 0);
// menuStrip.Name = "menuStrip";
// файлToolStripMenuItem menuStrip.Size = new Size(1148, 24);
// menuStrip.TabIndex = 4;
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); menuStrip.Text = "Файл";
файлToolStripMenuItem.Name = айлToolStripMenuItem"; //
файлToolStripMenuItem.Size = new Size(48, 20); // файлToolStripMenuItem
файлToolStripMenuItem.Text = "Файл"; //
// файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem });
// saveToolStripMenuItem файлToolStripMenuItem.Name = айлToolStripMenuItem";
// файлToolStripMenuItem.Size = new Size(48, 20);
saveToolStripMenuItem.Name = "saveToolStripMenuItem"; файлToolStripMenuItem.Text = "Файл";
saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; //
saveToolStripMenuItem.Size = new Size(181, 22); // saveToolStripMenuItem
saveToolStripMenuItem.Text = "Сохранение"; //
saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; saveToolStripMenuItem.Name = "saveToolStripMenuItem";
// saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S;
// loadToolStripMenuItem saveToolStripMenuItem.Size = new Size(181, 22);
// saveToolStripMenuItem.Text = "Сохранение";
loadToolStripMenuItem.Name = "loadToolStripMenuItem"; saveToolStripMenuItem.Click += saveToolStripMenuItem_Click;
loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; //
loadToolStripMenuItem.Size = new Size(181, 22); // loadToolStripMenuItem
loadToolStripMenuItem.Text = "Загрузка"; //
loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; loadToolStripMenuItem.Name = "loadToolStripMenuItem";
// loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
// saveFileDialog loadToolStripMenuItem.Size = new Size(181, 22);
// loadToolStripMenuItem.Text = "Загрузка";
saveFileDialog.Filter = "txt file | *.txt"; loadToolStripMenuItem.Click += loadToolStripMenuItem_Click;
// //
// openFileDialog // saveFileDialog
// //
openFileDialog.FileName = "openFileDialog1"; saveFileDialog.Filter = "txt file | *.txt";
openFileDialog.Filter = "txt file | *.txt"; //
// // openFileDialog
// FormShipCollection //
// openFileDialog.FileName = "openFileDialog1";
AutoScaleDimensions = new SizeF(7F, 15F); openFileDialog.Filter = "txt file | *.txt";
AutoScaleMode = AutoScaleMode.Font; //
ClientSize = new Size(1148, 545); // buttonSortByType
Controls.Add(pictureBox); //
Controls.Add(groupBoxTools); buttonSortByType.Anchor = AnchorStyles.None;
Controls.Add(menuStrip); buttonSortByType.Location = new Point(3, 196);
MainMenuStrip = menuStrip; buttonSortByType.Name = "buttonSortByType";
Name = "FormShipCollection"; buttonSortByType.Size = new Size(169, 22);
Text = "Коллекция кораблей"; buttonSortByType.TabIndex = 8;
groupBoxTools.ResumeLayout(false); buttonSortByType.Text = "Сортировка по типу";
panelCompanyTools.ResumeLayout(false); buttonSortByType.UseVisualStyleBackColor = true;
panelCompanyTools.PerformLayout(); buttonSortByType.Click += buttonSortByType_Click;
panelStoreage.ResumeLayout(false); //
panelStoreage.PerformLayout(); // buttonSortByColor
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); //
menuStrip.ResumeLayout(false); buttonSortByColor.Anchor = AnchorStyles.None;
menuStrip.PerformLayout(); buttonSortByColor.Location = new Point(3, 221);
ResumeLayout(false); buttonSortByColor.Name = "buttonSortByColor";
PerformLayout(); buttonSortByColor.Size = new Size(169, 22);
} buttonSortByColor.TabIndex = 9;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1148, 556);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Name = "FormShipCollection";
Text = "Коллекция кораблей";
groupBoxTools.ResumeLayout(false);
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
panelStoreage.ResumeLayout(false);
panelStoreage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion #endregion
private GroupBox groupBoxTools; private GroupBox groupBoxTools;
private ComboBox comboBoxSelectorCompany; private ComboBox comboBoxSelectorCompany;
private Button buttonAddShip; private Button buttonAddShip;
private PictureBox pictureBox; private PictureBox pictureBox;
@ -339,5 +365,7 @@
private ToolStripMenuItem loadToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@ -1,17 +1,8 @@
using WarmlyShip.CollectionGenericObjects; using WarmlyShip.CollectionGenericObjects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WarmlyShip.Drawnings; using WarmlyShip.Drawnings;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WarmlyShip.Exceptions; using WarmlyShip.Exceptions;
using WarmlyShip.Scripts.Exceptions;
namespace WarmlyShip; namespace WarmlyShip;
@ -70,6 +61,11 @@ public partial class FormShipCollection : Form
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", ex.Message); _logger.LogError("Ошибка: {Message}", ex.Message);
} }
catch (ObjectNotUniqueException ex)
{
MessageBox.Show("Такой объект уже присутствует в коллекции");
_logger.LogError("Ошибка: {Message}", ex.Message);
}
} }
@ -235,7 +231,7 @@ public partial class FormShipCollection : Form
listBoxCollection.Items.Clear(); listBoxCollection.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i) for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{ {
string? colName = _storageCollection.Keys?[i]; string? colName = _storageCollection.Keys?[i].Name;
if (!string.IsNullOrEmpty(colName)) if (!string.IsNullOrEmpty(colName))
{ {
listBoxCollection.Items.Add(colName); listBoxCollection.Items.Add(colName);
@ -279,4 +275,25 @@ public partial class FormShipCollection : Form
} }
RefreshListBoxItems(); RefreshListBoxItems();
} }
private void buttonSortByType_Click(object sender, EventArgs e)
{
CompareShips(new DrawningShipCompareByType());
}
private void buttonSortByColor_Click(object sender, EventArgs e)
{
CompareShips(new DrawningShipCompareByColor());
}
private void CompareShips(IComparer<DrawningShip?> comparer)
{
if (_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
} }