лаба4

This commit is contained in:
AnnaLioness 2023-10-21 03:21:30 +04:00
parent 437d7894e2
commit 203249781c
6 changed files with 339 additions and 103 deletions

View File

@ -29,10 +29,14 @@
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.ButtonAddCar = new System.Windows.Forms.Button();
this.ButtonRemoveCar = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.ButtonAddObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.ButtonDelObject = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveCar = new System.Windows.Forms.Button();
this.ButtonAddCar = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
@ -40,6 +44,10 @@
//
// panel1
//
this.panel1.Controls.Add(this.textBoxStorageName);
this.panel1.Controls.Add(this.ButtonAddObject);
this.panel1.Controls.Add(this.listBoxStorages);
this.panel1.Controls.Add(this.ButtonDelObject);
this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonRefreshCollection);
this.panel1.Controls.Add(this.ButtonRemoveCar);
@ -49,42 +57,78 @@
this.panel1.Size = new System.Drawing.Size(157, 449);
this.panel1.TabIndex = 1;
//
// ButtonAddCar
// textBoxStorageName
//
this.ButtonAddCar.Location = new System.Drawing.Point(16, 29);
this.ButtonAddCar.Name = "ButtonAddCar";
this.ButtonAddCar.Size = new System.Drawing.Size(118, 53);
this.ButtonAddCar.TabIndex = 0;
this.ButtonAddCar.Text = "добавить";
this.ButtonAddCar.UseVisualStyleBackColor = true;
this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click);
this.textBoxStorageName.Location = new System.Drawing.Point(18, 25);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(118, 20);
this.textBoxStorageName.TabIndex = 5;
//
// ButtonRemoveCar
// ButtonAddObject
//
this.ButtonRemoveCar.Location = new System.Drawing.Point(16, 209);
this.ButtonRemoveCar.Name = "ButtonRemoveCar";
this.ButtonRemoveCar.Size = new System.Drawing.Size(118, 50);
this.ButtonRemoveCar.TabIndex = 1;
this.ButtonRemoveCar.Text = "удалить";
this.ButtonRemoveCar.UseVisualStyleBackColor = true;
this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click);
this.ButtonAddObject.Location = new System.Drawing.Point(18, 51);
this.ButtonAddObject.Name = "ButtonAddObject";
this.ButtonAddObject.Size = new System.Drawing.Size(118, 29);
this.ButtonAddObject.TabIndex = 2;
this.ButtonAddObject.Text = "добавить набор";
this.ButtonAddObject.UseVisualStyleBackColor = true;
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.Location = new System.Drawing.Point(18, 86);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(120, 95);
this.listBoxStorages.TabIndex = 4;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// ButtonDelObject
//
this.ButtonDelObject.Location = new System.Drawing.Point(18, 187);
this.ButtonDelObject.Name = "ButtonDelObject";
this.ButtonDelObject.Size = new System.Drawing.Size(118, 30);
this.ButtonDelObject.TabIndex = 2;
this.ButtonDelObject.Text = "удалить набор";
this.ButtonDelObject.UseVisualStyleBackColor = true;
this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(16, 330);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20);
this.maskedTextBoxNumber.TabIndex = 3;
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(16, 341);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(16, 401);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(118, 50);
this.ButtonRefreshCollection.Size = new System.Drawing.Size(118, 37);
this.ButtonRefreshCollection.TabIndex = 2;
this.ButtonRefreshCollection.Text = "обновить коллекцию";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// maskedTextBoxNumber
// ButtonRemoveCar
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(25, 183);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 20);
this.maskedTextBoxNumber.TabIndex = 3;
this.ButtonRemoveCar.Location = new System.Drawing.Point(16, 356);
this.ButtonRemoveCar.Name = "ButtonRemoveCar";
this.ButtonRemoveCar.Size = new System.Drawing.Size(118, 39);
this.ButtonRemoveCar.TabIndex = 1;
this.ButtonRemoveCar.Text = "удалить контейнеровоз";
this.ButtonRemoveCar.UseVisualStyleBackColor = true;
this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click);
//
// ButtonAddCar
//
this.ButtonAddCar.Location = new System.Drawing.Point(16, 286);
this.ButtonAddCar.Name = "ButtonAddCar";
this.ButtonAddCar.Size = new System.Drawing.Size(118, 38);
this.ButtonAddCar.TabIndex = 0;
this.ButtonAddCar.Text = "добавить контейнеровоз";
this.ButtonAddCar.UseVisualStyleBackColor = true;
this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click);
//
// pictureBoxCollection
//
@ -119,5 +163,9 @@
private System.Windows.Forms.Button ButtonRefreshCollection;
private System.Windows.Forms.Button ButtonRemoveCar;
private System.Windows.Forms.Button ButtonAddCar;
private System.Windows.Forms.TextBox textBoxStorageName;
private System.Windows.Forms.Button ButtonAddObject;
private System.Windows.Forms.ListBox listBoxStorages;
private System.Windows.Forms.Button ButtonDelObject;
}
}

View File

@ -14,28 +14,73 @@ namespace Lab1ContainersShip
{
public partial class FormShipCollection : Form
{
private readonly ShipGenericCollection<DrawingShip, DrawningObjectShip> _ships;
private readonly ShipGenericStorage _storage;
public FormShipCollection()
{
InitializeComponent();
_ships = new ShipGenericCollection<DrawingShip,
DrawningObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new ShipGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void FormShipCollection_Load(object sender, EventArgs e)
{
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender,EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
}
private void ButtonAddCar_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormContainerShip form = new FormContainerShip();
if (form.ShowDialog() == DialogResult.OK)
{
if (_ships + form.SelectedShip != -1)
if ((obj + form.SelectedShip) != 1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ships.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@ -43,20 +88,31 @@ DrawningObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
}
}
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (!(_ships - pos))
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _ships.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@ -67,7 +123,32 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _ships.ShowShips();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowShips();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект{ listBoxStorages.SelectedItem}", "Удаление", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
}
}

View File

@ -73,6 +73,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SetGeneric.cs" />
<Compile Include="ShipGenericStorage.cs" />
<Compile Include="Status.cs" />
<EmbeddedResource Include="FormContainerShip.resx">
<DependentUpon>FormContainerShip.cs</DependentUpon>

View File

@ -14,18 +14,20 @@ namespace Lab1ContainersShip
// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
public int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>(count);
}
/// <summary>
/// Добавление объекта в набор
@ -35,35 +37,13 @@ namespace Lab1ContainersShip
public int Insert(T ship)
{
// TODO вставка в начало набора
int temp = 0;
int k = 0;
for(int j = 0; j < _places.Length; j++) {
if (_places[j] != null)
{
k++;
}
}
if (k == _places.Length)
if(_places.Count >= _maxCount)
{
return -1;
}
else
{
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > 0; i--)
{
_places[i] = _places[i -1];
}
_places[0] = ship;
_places.Insert(0, ship);
return 0;
}
}
@ -83,40 +63,23 @@ namespace Lab1ContainersShip
// сдвиг всех объектов, находящихся справа от
//позиции до первого пустого элемента
// TODO вставка по позиции
int temp = 0;
int k = 0;
for (int j = position; j < _places.Length; j++)
{
if (_places[j] != null)
{
k++;
}
}
if (position < _places.Length && k < _places.Length-position)
{
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
return true;
}
else
if(_places.Count >= _maxCount)
{
return false;
}
if(position < 0 || position > _places.Count)
{
return false;
}
if(position == _places.Count)
{
_places.Add(ship);
}
else
{
_places.Insert(position, ship);
}
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -128,7 +91,7 @@ namespace Lab1ContainersShip
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива
//значение null
if(position < _places.Length)
if(position < _places.Count && _places.Count < _maxCount)
{
_places[position] = null;
return true;
@ -147,7 +110,7 @@ namespace Lab1ContainersShip
public T Get(int position)
{
// TODO проверка позиции
if(position < _places.Length)
if(position < _places.Count && position >= 0)
{
return _places[position];
}
@ -156,6 +119,51 @@ namespace Lab1ContainersShip
return null;
}
}
public T this[int position]
{
get
{
if (position < _places.Count && position >= 0)
{
return _places[position];
}
else
{
return null;
}
// TODO проверка позиции
}
set
{
/*if ((position < _places.Count && position >= 0) && _places.Count < _maxCount)
{
_places[position] = value;
}
else
{
return;
}*/
Insert(value, position);
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
}
}
public IEnumerable<T> GetShips(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
}
}

View File

@ -70,7 +70,7 @@ namespace Lab1ContainersShip
public static bool operator -(ShipGenericCollection<T, U> collect, int
pos)
{
T obj = collect._collection.Get(pos);
T obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -84,7 +84,7 @@ namespace Lab1ContainersShip
/// <returns></returns>
public U GetU(int pos)
{
return (U)_collection.Get(pos)?.GetMoveableObject;
return (U)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -124,15 +124,17 @@ namespace Lab1ContainersShip
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var ship in _collection.GetShips())
{
T t = _collection.Get(i);
if (t != null)
if (ship != null)
{
t.SetPosition(((_collection.Count -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
t.DrawShip(g);
ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
ship.DrawShip(g);
}
i++;
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта

View File

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Lab1ContainersShip.DrawingObjects;
using Lab1ContainersShip.MovementStrategy;
namespace Lab1ContainersShip
{
internal class ShipGenericStorage
{
readonly Dictionary<string, ShipGenericCollection<DrawingShip,
DrawningObjectShip>> _shipStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _shipStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public ShipGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string, ShipGenericCollection<DrawingShip,
DrawningObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (_shipStorages.ContainsKey(name))
{
return;
}
else
{
_shipStorages[name] = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_shipStorages.ContainsKey(name))
{
return;
}
else
{
_shipStorages.Remove(name);
}
// TODO Прописать логику для удаления
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ShipGenericCollection<DrawingShip, DrawningObjectShip>
this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_shipStorages.ContainsKey(ind))
{
return _shipStorages[ind];
}
else
{
return null;
}
}
}
}
}