Сделанная четвертая лабороторная

This commit is contained in:
Максим Яковлев 2023-11-12 01:22:57 +04:00
parent 4ff5aeaad7
commit fc647fd9af
5 changed files with 258 additions and 59 deletions

View File

@ -29,17 +29,24 @@
private void InitializeComponent()
{
tools = new GroupBox();
groupBox1 = new GroupBox();
listBoxStorages = new ListBox();
buttonDelObject = new Button();
buttonAddObject = new Button();
textBoxStorageName = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveBoat = new Button();
buttonAddBoat = new Button();
pictureBoxCollection = new PictureBox();
tools.SuspendLayout();
groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// tools
//
tools.Controls.Add(groupBox1);
tools.Controls.Add(maskedTextBoxNumber);
tools.Controls.Add(buttonRefreshCollection);
tools.Controls.Add(buttonRemoveBoat);
@ -52,9 +59,59 @@
tools.TabStop = false;
tools.Text = "Инструменты";
//
// groupBox1
//
groupBox1.Controls.Add(listBoxStorages);
groupBox1.Controls.Add(buttonDelObject);
groupBox1.Controls.Add(buttonAddObject);
groupBox1.Controls.Add(textBoxStorageName);
groupBox1.Location = new Point(6, 22);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(188, 288);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Наборы";
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(11, 95);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(171, 139);
listBoxStorages.TabIndex = 3;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(6, 246);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(176, 36);
buttonDelObject.TabIndex = 2;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += buttonDelObject_Click;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(6, 51);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(176, 36);
buttonAddObject.TabIndex = 1;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += buttonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 22);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(176, 23);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(45, 104);
maskedTextBoxNumber.Location = new Point(45, 433);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(112, 23);
@ -63,7 +120,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 211);
buttonRefreshCollection.Location = new Point(6, 563);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(188, 39);
buttonRefreshCollection.TabIndex = 3;
@ -73,7 +130,7 @@
//
// buttonRemoveBoat
//
buttonRemoveBoat.Location = new Point(6, 133);
buttonRemoveBoat.Location = new Point(6, 483);
buttonRemoveBoat.Name = "buttonRemoveBoat";
buttonRemoveBoat.Size = new Size(188, 39);
buttonRemoveBoat.TabIndex = 1;
@ -83,7 +140,7 @@
//
// buttonAddBoat
//
buttonAddBoat.Location = new Point(6, 22);
buttonAddBoat.Location = new Point(6, 372);
buttonAddBoat.Name = "buttonAddBoat";
buttonAddBoat.Size = new Size(188, 39);
buttonAddBoat.TabIndex = 0;
@ -110,6 +167,8 @@
Text = "Набор лодок";
tools.ResumeLayout(false);
tools.PerformLayout();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -122,5 +181,10 @@
private Button buttonRefreshCollection;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox1;
private ListBox listBoxStorages;
private Button buttonDelObject;
private Button buttonAddObject;
private TextBox textBoxStorageName;
}
}

View File

@ -15,53 +15,134 @@ namespace SailBoat
{
public partial class FormBoatCollection : Form
{
private readonly BoatsGenericCollection<DrawningBoat, DrawningObjectBoat> _boats;
private readonly BoatsGenericStorage _storage;
public FormBoatCollection()
{
InitializeComponent();
_boats = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats();
}
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();
}
}
private void buttonAddBoat_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormSailBoat form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if ((_boats + form.SelectedBoat) == 0)
if (obj + form.SelectedBoat)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonRemoveBoat_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)
{
return;
}
if (maskedTextBoxNumber.Text == "") { return; }
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_boats - pos)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _boats.ShowBoats();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBoats();
}
}
}

View File

@ -32,15 +32,15 @@ namespace SailBoat.Generics
_collection = new SetGeneric<T>(width * height);
}
public static int? operator +(BoatsGenericCollection<T,U> collect, T? obj)
public static bool operator +(BoatsGenericCollection<T,U> collect, T? obj)
{
if (obj == null) return -1;
return collect?._collection.Insert(obj);
if (obj == null) return false;
return (bool)collect?._collection.Insert(obj);
}
public static bool operator -(BoatsGenericCollection<T,U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if(obj == null)
{
return false;
@ -51,7 +51,7 @@ namespace SailBoat.Generics
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObjects;
return (U?)_collection[pos]?.GetMoveableObjects;
}
@ -80,15 +80,15 @@ namespace SailBoat.Generics
{
int height = _pictureHeight / _placeSizeHeight;
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count ; i++)
int i = 0;
foreach (var boat in _collection.GetBoats())
{
DrawningBoat boat = _collection.Get(i);
if(boat == null)
if (boat != null)
{
continue;
boat.SetPosition((i % width) * _placeSizeWidth, (i / (height - 1)) * _placeSizeHeight);
boat.DrawTransport(g);
}
boat.SetPosition((i % width) * _placeSizeWidth, (i / (height - 1)) * _placeSizeHeight);
boat.DrawTransport(g);
i++;
}
}
}

View File

@ -0,0 +1,49 @@
using SailBoat.DrawningObjects;
using SailBoat.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SailBoat.Generics
{
internal class BoatsGenericStorage
{
readonly Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>> _boatStorages;
public List<string> Keys => _boatStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string, BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name)) return;
_boatStorages[name] = new BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_boatStorages.ContainsKey(name)) return;
_boatStorages.Remove(name);
}
public BoatsGenericCollection<DrawningBoat, DrawningObjectBoat>? this[string ind]
{
get
{
if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
return null;
}
}
}
}

View File

@ -9,65 +9,70 @@ namespace SailBoat.Generics
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
private readonly List<T?> _places;
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int Count)
{
_places = new T[Count];
_maxCount = Count;
_places = new List<T?>(Count);
}
public int Insert(T boat)
public bool Insert(T boat)
{
int index = 0;
while (_places[index] != null) {
index++;
if(index == Count) { return -1; }
}
while (index != 0)
{
_places[index] = _places[index - 1];
index--;
}
_places[0] = boat;
return 0;
return Insert(boat, 0);
}
public bool Insert(T boat, int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
{
return false;
}
if (_places[position] == null)
{
_places[position] = boat;
return true;
}
int index = position;
while (_places[index] != null) index++;
if (index == Count) return false;
for(int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = boat;
if(Count >= _maxCount) { return false; }
_places.Insert(0, boat);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
if (position < 0 || position >= _maxCount) return false;
if (position >= Count) return false;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count) return null;
return _places[position];
get
{
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
}
}
}
}