This commit is contained in:
Nikita 2025-02-24 21:31:48 +04:00
parent aac002fb3a
commit fb452c0f70
4 changed files with 22 additions and 21 deletions

View File

@ -24,14 +24,14 @@ public abstract class AbstractCompany
_collection.SetMaxCount = GetMaxCount; _collection.SetMaxCount = GetMaxCount;
} }
public static bool operator +(AbstractCompany company, DrawningBoat boat) public static int operator +(AbstractCompany company, DrawningBoat boat)
{ {
return company._collection?.Insert(boat) ?? false; return company._collection?.Insert(boat) ?? -1;
} }
public static bool operator -(AbstractCompany company, int position) public static DrawningBoat? operator -(AbstractCompany company, int position)
{ {
return company._collection?.Remove(position) ?? false; return company._collection?.Remove(position);
} }
public DrawningBoat? GetRandomObject() public DrawningBoat? GetRandomObject()

View File

@ -10,9 +10,9 @@ public interface ICollectionGenericObjects<T> where T : class
{ {
int Count { get; } int Count { get; }
int SetMaxCount { set; } int SetMaxCount { set; }
bool Insert(T obj); int Insert(T obj);
bool Insert(T obj, int position); int Insert(T obj, int position);
bool Remove(int position); T? Remove(int position);
T? Get(int position); T? Get(int position);
} }

View File

@ -36,7 +36,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
return null; return null;
} }
public bool Insert(T obj) public int Insert(T obj)
{ {
//todo //todo
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
@ -44,19 +44,19 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
if (_collection[i] == null) if (_collection[i] == null)
{ {
_collection[i] = obj; _collection[i] = obj;
return true; return i;
} }
} }
return false; return -1;
} }
public bool Insert(T obj, int position) public int Insert(T obj, int position)
{ {
//todo //todo
if (!(position > 0 && position < _collection.Length)) if (!(position > 0 && position < _collection.Length))
{ {
return false; return -1;
} }
if (_collection[position] == null) if (_collection[position] == null)
@ -68,7 +68,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
if (_collection[i] == null) if (_collection[i] == null)
{ {
_collection[i] = obj; _collection[i] = obj;
return true; return i;
} }
} }
@ -77,22 +77,23 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
if (_collection[i] == null) if (_collection[i] == null)
{ {
_collection[i] = obj; _collection[i] = obj;
return true; return i;
} }
} }
} }
return false; return -1;
} }
public bool Remove(int position) public T? Remove(int position)
{ {
if (!(position >= 0 && position < _collection.Length) || _collection[position] == null ) if (!(position >= 0 && position < _collection.Length) || _collection[position] == null )
{ {
return false; return null;
} }
T? deletedElement = _collection[position];
_collection[position] = null; _collection[position] = null;
return true; return deletedElement;
} }
} }

View File

@ -52,7 +52,7 @@ public partial class FormBoatCollection : Form
return; return;
} }
if (_company + drawningBoat) if (_company + drawningBoat != -1)
{ {
MessageBox.Show(("Объект добавлен")); MessageBox.Show(("Объект добавлен"));
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();
@ -100,7 +100,7 @@ public partial class FormBoatCollection : Form
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - pos) if (_company - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show(); pictureBox.Image = _company.Show();