Четвертая лабораторная работа

This commit is contained in:
allllen4a 2023-11-03 11:09:02 +03:00
parent 2b931f9b23
commit 620117e636
10 changed files with 352 additions and 172 deletions

View File

@ -53,35 +53,36 @@ namespace projectDoubleDeckerBus
/// <param name="obj"></param>
/// <returns></returns>
///
public static int? operator +(BusesGenericCollection<T, U> collect, T?
//преобразовали bool в int
public static bool operator +(BusesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(BusesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj == null)
{
return false;
}
return collect._collection.Remove(pos);
return (bool)collect?._collection.Insert(obj);
}
public static T? operator -(BusesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCars()
public Bitmap ShowBuses()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
@ -114,18 +115,17 @@ namespace projectDoubleDeckerBus
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var bus in _collection.GetBuses())
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int j = 0; j < _collection.Count; j++)
if (bus != null)
{
DrawningBus? bus = _collection.Get(j);
if (bus == null)
continue;
bus.SetPosition((width - j % width - 1) * _placeSizeWidth, j / width * _placeSizeHeight);
bus.SetPosition((width - i % width - 1) * _placeSizeWidth, i / width * _placeSizeHeight);
bus.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using projectDoubleDeckerBus.Drawings;
using projectDouble_Decker_Bus.MovementStrategy;
using projectDoubleDeckerBus.Generics;
namespace projectDoubleDeckerBus.Generics
{
internal class BusesGenericStorage
{
readonly Dictionary<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages;
public List<string> Keys => _busStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public BusesGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_busStorages.ContainsKey(name)) return;
_busStorages[name] = new BusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_busStorages.ContainsKey(name)) return;
_busStorages.Remove(name);
}
public BusesGenericCollection<DrawningBus, DrawningObjectBus>?
this[string ind]
{
get
{
if (_busStorages.ContainsKey(ind)) return _busStorages[ind];
return null;
}
}
}
}

View File

@ -7,6 +7,7 @@ using projectDoubleDeckerBus;
using System.Drawing;
using projectDoubleDeckerBus.Entity;
using projectDouble_Decker_Bus.MovementStrategy;
using System.Collections.Generic;
namespace projectDoubleDeckerBus.Drawings
{
@ -19,11 +20,8 @@ namespace projectDoubleDeckerBus.Drawings
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
protected int _pictureWidth;
protected int _pictureHeight;
/// <summary>
///
/// Левая координата прорисовки автобуса
@ -185,22 +183,34 @@ namespace projectDoubleDeckerBus.Drawings
_busHeight = busHeight;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (((x >= 0) && (x + _busWidth > _pictureWidth)) || ((y >= 0) && (y + _busHeight > _pictureHeight)))
if (x < 0)
{
_startPosX = 0;
}
else if (x + _busWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
}
else
{
_startPosX = x;
}
if (y < 0)
{
_startPosY = 0;
}
else if (y + _busHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _busHeight;
}
else
{
_startPosY = y;
}
}
public void MoveBus(Direction direction)
{
if (EntityBus == null)
@ -255,7 +265,6 @@ namespace projectDoubleDeckerBus.Drawings
{
return;
}
int windowSize = 15;
Brush brBlack = new SolidBrush(Color.Black);
Brush brRed = new SolidBrush(Color.Red);
@ -287,5 +296,6 @@ namespace projectDoubleDeckerBus.Drawings
g.FillEllipse(brBlue, _startPosX + 145, _startPosY + 50, windowSize, windowSize);
}
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using projectDoubleDeckerBus.Entity;
using System.Drawing;
using System.Collections.Generic;
namespace projectDoubleDeckerBus.Drawings
{

View File

@ -7,6 +7,7 @@ using projectDoubleDeckerBus.Entity;
using projectDoubleDeckerBus.Drawings;
using System.Drawing;
using projectDoubleDeckerBus;
using System.Collections.Generic;
namespace projectDouble_Decker_Bus.MovementStrategy
{

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Collections.Generic;
namespace projectDoubleDeckerBus.Entity
{

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Collections.Generic;
namespace projectDoubleDeckerBus.Entity
{

View File

@ -29,11 +29,16 @@
private void InitializeComponent()
{
panel1 = new Panel();
label1 = new Label();
InputtextBox = new TextBox();
labelSet = new Label();
ButtonDelObject = new Button();
listBoxStorages = new ListBox();
buttonAddObject = new Button();
textBoxStorageName = new TextBox();
buttonUpdateCollection = new Button();
buttonDeleteBus = new Button();
buttonAddBus = new Button();
ButtonRemoveBus = new Button();
maskedTextBoxNumber = new TextBox();
ButtonAddCar = new Button();
labelTools = new Label();
DrawBus = new PictureBox();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawBus).BeginInit();
@ -41,71 +46,121 @@
//
// panel1
//
panel1.Controls.Add(label1);
panel1.Controls.Add(InputtextBox);
panel1.Controls.Add(labelSet);
panel1.Controls.Add(ButtonDelObject);
panel1.Controls.Add(listBoxStorages);
panel1.Controls.Add(buttonAddObject);
panel1.Controls.Add(textBoxStorageName);
panel1.Controls.Add(buttonUpdateCollection);
panel1.Controls.Add(buttonDeleteBus);
panel1.Controls.Add(buttonAddBus);
panel1.Controls.Add(ButtonRemoveBus);
panel1.Controls.Add(maskedTextBoxNumber);
panel1.Controls.Add(ButtonAddCar);
panel1.Controls.Add(labelTools);
panel1.Controls.Add(DrawBus);
panel1.Dock = DockStyle.Fill;
panel1.Location = new Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new Size(800, 450);
panel1.Size = new Size(813, 508);
panel1.TabIndex = 0;
//
// label1
// labelSet
//
label1.AutoSize = true;
label1.Location = new Point(700, 9);
label1.Name = "label1";
label1.Size = new Size(34, 15);
label1.TabIndex = 1;
label1.Text = "Tools";
label1.TextAlign = ContentAlignment.TopCenter;
labelSet.AutoSize = true;
labelSet.Location = new Point(692, 21);
labelSet.Name = "labelSet";
labelSet.Size = new Size(28, 15);
labelSet.TabIndex = 1;
labelSet.Text = "Sets";
//
// InputtextBox
// ButtonDelObject
//
InputtextBox.Location = new Point(700, 150);
InputtextBox.Name = "InputtextBox";
InputtextBox.Size = new Size(88, 23);
InputtextBox.TabIndex = 1;
ButtonDelObject.Location = new Point(692, 191);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(94, 36);
ButtonDelObject.TabIndex = 1;
ButtonDelObject.Text = "Delete set";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(692, 112);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(94, 64);
listBoxStorages.TabIndex = 1;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(692, 68);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(94, 29);
buttonAddObject.TabIndex = 1;
buttonAddObject.Text = "Add set";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(692, 39);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(94, 23);
textBoxStorageName.TabIndex = 1;
//
// buttonUpdateCollection
//
buttonUpdateCollection.Location = new Point(700, 206);
buttonUpdateCollection.Location = new Point(692, 440);
buttonUpdateCollection.Name = "buttonUpdateCollection";
buttonUpdateCollection.Size = new Size(88, 39);
buttonUpdateCollection.Size = new Size(94, 39);
buttonUpdateCollection.TabIndex = 1;
buttonUpdateCollection.Text = "Update the collection";
buttonUpdateCollection.UseVisualStyleBackColor = true;
buttonUpdateCollection.Click += buttonUpdateCollection_Click;
//
// buttonDeleteBus
// ButtonRemoveBus
//
buttonDeleteBus.Location = new Point(700, 92);
buttonDeleteBus.Name = "buttonDeleteBus";
buttonDeleteBus.Size = new Size(88, 36);
buttonDeleteBus.TabIndex = 1;
buttonDeleteBus.Text = "Delete bus";
buttonDeleteBus.UseVisualStyleBackColor = true;
buttonDeleteBus.Click += buttonDeleteBus_Click;
ButtonRemoveBus.Location = new Point(692, 370);
ButtonRemoveBus.Name = "ButtonRemoveBus";
ButtonRemoveBus.Size = new Size(94, 40);
ButtonRemoveBus.TabIndex = 1;
ButtonRemoveBus.Text = "Delete bus";
ButtonRemoveBus.UseVisualStyleBackColor = true;
ButtonRemoveBus.Click += ButtonRemoveBus_Click;
//
// buttonAddBus
// maskedTextBoxNumber
//
buttonAddBus.Location = new Point(700, 30);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(88, 36);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Add bus";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
maskedTextBoxNumber.Location = new Point(692, 314);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(94, 23);
maskedTextBoxNumber.TabIndex = 1;
//
// ButtonAddCar
//
ButtonAddCar.Location = new Point(692, 256);
ButtonAddCar.Name = "ButtonAddCar";
ButtonAddCar.Size = new Size(94, 34);
ButtonAddCar.TabIndex = 1;
ButtonAddCar.Text = "Add bus";
ButtonAddCar.UseVisualStyleBackColor = true;
ButtonAddCar.Click += ButtonAddBus_Click;
//
// labelTools
//
labelTools.AutoSize = true;
labelTools.Location = new Point(692, 6);
labelTools.Name = "labelTools";
labelTools.Size = new Size(34, 15);
labelTools.TabIndex = 1;
labelTools.Text = "Tools";
labelTools.TextAlign = ContentAlignment.TopCenter;
//
// DrawBus
//
DrawBus.Dock = DockStyle.Fill;
DrawBus.Location = new Point(0, 0);
DrawBus.Name = "DrawBus";
DrawBus.Size = new Size(800, 450);
DrawBus.Size = new Size(813, 508);
DrawBus.TabIndex = 0;
DrawBus.TabStop = false;
//
@ -113,7 +168,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(813, 508);
Controls.Add(panel1);
Name = "FormBusCollection";
Text = "FormBusCollection";
@ -126,11 +181,17 @@
#endregion
private Panel panel1;
private Label label1;
private TextBox InputtextBox;
private Label labelTools;
private Button buttonUpdateCollection;
private Button buttonDeleteBus;
private Button buttonAddBus;
private Button ButtonDelObject;
private Button buttonAddObject;
private PictureBox DrawBus;
private ListBox listBoxStorages;
private ListBox listBoxCollection;
private TextBox textBoxStorageName;
private Button ButtonAddCar;
private Button ButtonRemoveBus;
private TextBox maskedTextBoxNumber;
private Label labelSet;
}
}

View File

@ -11,58 +11,137 @@ using projectDoubleDeckerBus.Drawings;
using projectDouble_Decker_Bus.MovementStrategy;
using projectDoubleDeckerBus.Generics;
namespace projectDoubleDeckerBus
{
public partial class FormBusCollection : Form
{
private readonly BusesGenericCollection<DrawningBus,
DrawningObjectBus> _buses;
private readonly BusesGenericStorage _storage;
public FormBusCollection()
{
InitializeComponent();
_buses = new BusesGenericCollection<DrawningBus,
DrawningObjectBus>(DrawBus.Width, DrawBus.Height);
_storage = new BusesGenericStorage(DrawBus.Width, DrawBus.Height);
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
ReloadObjects();
}
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 buttonAddBus_Click(object sender, EventArgs e)
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 buttonUpdateCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawBus.Image = obj.ShowBuses();
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
DrawBus.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses();
}
private void ButtonAddBus_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormDoubleDeckerBus form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_buses + form.SelectedBus != -1)
if (obj + form.SelectedBus)
{
MessageBox.Show("Объект добавлен");
DrawBus.Image = _buses.ShowCars();
DrawBus.Image = obj.ShowBuses();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonDeleteBus_Click(object sender, EventArgs e)
}
private void ButtonRemoveBus_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;
}
int pos = Convert.ToInt32(InputtextBox.Text);
if (_buses - pos != null)
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawBus.Image = _buses.ShowCars();
DrawBus.Image = obj.ShowBuses();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonUpdateCollection_Click(object sender, EventArgs e)
{
DrawBus.Image = _buses.ShowCars();
}
}
}

View File

@ -7,104 +7,83 @@ using System.Threading.Tasks;
namespace projectDoubleDeckerBus.Generics
{
internal class SetGeneric<T>
where T : class
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="bus">Добавляемый автобус</param>
/// <returns></returns>
public int Insert(T bus)
public bool Insert(T bus)
{
int index = -1;
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
{
return -1;
}
for (int i = index; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = bus;
return 0;
return Insert(bus, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// /// </summary>
/// <param name="bus">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T bus, int position)
public bool Insert(T bus, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] == null)
{
_places[position] = bus;
return position;
}
int index = -1;
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
return -1;
for (int i = index; index > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = bus;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
return false;
_places[position] = null;
if (Count >= _maxCount)
return false;
_places.Insert(0, bus);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public bool Remove(int position)
{
if (position < 0 || position >= Count)
return null;
return _places[position];
if (position < 0 || position > _maxCount)
return false;
if (position >= Count)
return false;
_places.RemoveAt(position);
return true;
}
public T? this[int 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?> GetBuses(int? maxBuses = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBuses.HasValue && i == maxBuses.Value)
{
yield break;
}
}
}
}
}