Laba4 KozyrevSS PIbd-21 GasolineTanker #5

Closed
Serxionaft wants to merge 3 commits from Laba4 into Laba3
9 changed files with 344 additions and 122 deletions

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace Lab.Entities
{
public class BaseCar
public class BaseTanker
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public BaseCar(int speed, double weight, Color bodyColor)
public BaseTanker(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;

View File

@ -26,29 +26,28 @@ namespace Lab.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int? operator +(CarsGenericCollection<T, U> collect, T?
public static bool operator +(CarsGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(CarsGenericCollection<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 -(CarsGenericCollection<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;
}
public Bitmap ShowCars()
{
@ -78,14 +77,16 @@ namespace Lab.Generics
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var tank in _collection.GetCars())
{
DrawTanker? tank = _collection.Get(i);
if (tank == null)
continue;
tank.SetPosition(i % height * _placeSizeWidth, (height - i / height - 1) * _placeSizeHeight);
tank.DrawTransport(g);
if (tank != null)
{
tank.SetPosition(i % (width) * _placeSizeWidth, (height - i / width - 1) * _placeSizeHeight);
tank.DrawTransport(g);
}
i++;
}
}
}}
}
}

47
Lab/CarsGenericStorage.cs Normal file
View File

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

View File

@ -29,32 +29,98 @@
private void InitializeComponent()
{
panel1 = new Panel();
panel2 = new Panel();
DeleteCollectButton = new Button();
CollectionListBox = new ListBox();
AddCollectButton = new Button();
SetTextBox = new TextBox();
label2 = new Label();
UpdateButton = new Button();
DeleteButton = new Button();
AddButton = new Button();
InputNum = new TextBox();
CarTextBox = new TextBox();
label1 = new Label();
DrawTank = new PictureBox();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton);
panel1.Controls.Add(AddButton);
panel1.Controls.Add(InputNum);
panel1.Controls.Add(CarTextBox);
panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(550, 0);
panel1.Name = "panel1";
panel1.Size = new Size(250, 450);
panel1.Size = new Size(250, 514);
panel1.TabIndex = 0;
//
// panel2
//
panel2.Controls.Add(DeleteCollectButton);
panel2.Controls.Add(CollectionListBox);
panel2.Controls.Add(AddCollectButton);
panel2.Controls.Add(SetTextBox);
panel2.Controls.Add(label2);
panel2.Location = new Point(16, 38);
panel2.Name = "panel2";
panel2.Size = new Size(214, 220);
panel2.TabIndex = 5;
//
// DeleteCollectButton
//
DeleteCollectButton.Location = new Point(3, 181);
DeleteCollectButton.Name = "DeleteCollectButton";
DeleteCollectButton.Size = new Size(208, 29);
DeleteCollectButton.TabIndex = 4;
DeleteCollectButton.Text = "Удалить набор";
DeleteCollectButton.UseVisualStyleBackColor = true;
DeleteCollectButton.Click += ButtonDelObject_Click;
//
// CollectionListBox
//
CollectionListBox.FormattingEnabled = true;
CollectionListBox.ItemHeight = 20;
CollectionListBox.Location = new Point(3, 91);
CollectionListBox.Name = "CollectionListBox";
CollectionListBox.Size = new Size(208, 84);
CollectionListBox.TabIndex = 3;
CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// AddCollectButton
//
AddCollectButton.Location = new Point(3, 56);
AddCollectButton.Name = "AddCollectButton";
AddCollectButton.Size = new Size(208, 29);
AddCollectButton.TabIndex = 2;
AddCollectButton.Text = "Добавить набор";
AddCollectButton.UseVisualStyleBackColor = true;
AddCollectButton.Click += ButtonAddObject_Click;
//
// SetTextBox
//
SetTextBox.Location = new Point(2, 23);
SetTextBox.Name = "SetTextBox";
SetTextBox.Size = new Size(209, 27);
SetTextBox.TabIndex = 1;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(3, 0);
label2.Name = "label2";
label2.Size = new Size(66, 20);
label2.TabIndex = 0;
label2.Text = "Наборы";
//
// UpdateButton
//
UpdateButton.Location = new Point(10, 241);
UpdateButton.Location = new Point(10, 401);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(228, 37);
UpdateButton.TabIndex = 4;
@ -64,7 +130,7 @@
//
// DeleteButton
//
DeleteButton.Location = new Point(10, 176);
DeleteButton.Location = new Point(10, 358);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(228, 37);
DeleteButton.TabIndex = 3;
@ -74,20 +140,20 @@
//
// AddButton
//
AddButton.Location = new Point(10, 53);
AddButton.Location = new Point(10, 282);
AddButton.Name = "AddButton";
AddButton.Size = new Size(228, 37);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить автомобиль";
AddButton.UseVisualStyleBackColor = true;
AddButton.Click += ButtonAddTank_Click;
AddButton.Click += ButtonAddCar_Click;
//
// InputNum
// CarTextBox
//
InputNum.Location = new Point(10, 133);
InputNum.Name = "InputNum";
InputNum.Size = new Size(228, 27);
InputNum.TabIndex = 1;
CarTextBox.Location = new Point(10, 325);
CarTextBox.Name = "CarTextBox";
CarTextBox.Size = new Size(228, 27);
CarTextBox.TabIndex = 1;
//
// label1
//
@ -103,7 +169,7 @@
DrawTank.Dock = DockStyle.Fill;
DrawTank.Location = new Point(0, 0);
DrawTank.Name = "DrawTank";
DrawTank.Size = new Size(550, 450);
DrawTank.Size = new Size(550, 514);
DrawTank.TabIndex = 1;
DrawTank.TabStop = false;
//
@ -111,13 +177,15 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(800, 514);
Controls.Add(DrawTank);
Controls.Add(panel1);
Name = "CollectionsFrame";
Text = "Form1";
Text = "Гаражи бензовозов";
panel1.ResumeLayout(false);
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).EndInit();
ResumeLayout(false);
}
@ -128,8 +196,14 @@
private Button UpdateButton;
private Button DeleteButton;
private Button AddButton;
private TextBox InputNum;
private TextBox CarTextBox;
private Label label1;
private PictureBox DrawTank;
private Panel panel2;
private Button DeleteCollectButton;
private ListBox CollectionListBox;
private Button AddCollectButton;
private TextBox SetTextBox;
private Label label2;
}
}

View File

@ -15,23 +15,106 @@ namespace Lab
{
public partial class CollectionsFrame : Form
{
private readonly CarsGenericCollection<DrawTanker, DrawingObjectTanker> _cars;
private readonly CarsGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public CollectionsFrame()
{
InitializeComponent();
_cars = new CarsGenericCollection<DrawTanker, DrawingObjectTanker>(DrawTank.Width, DrawTank.Height);
_storage = new CarsGenericStorage(DrawTank.Width, DrawTank.Height);
}
private void ButtonAddTank_Click(object sender, EventArgs e)
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
Frame form = new Frame();
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
foreach (var key in _storage.Keys)
{
CollectionListBox.Items.Add(key);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawTank.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCars();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(CollectionListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddCar_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
Frame form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_cars + form.SelectedCar != -1)
if (obj + form.SelectedCar)
{
MessageBox.Show("Объект добавлен");
DrawTank.Image = _cars.ShowCars();
DrawTank.Image = obj.ShowCars();
}
else
{
@ -39,33 +122,58 @@ namespace Lab
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = -1;
try
{
pos = Convert.ToInt32(InputNum.Text);
}
catch (Exception ex) { }
if (_cars - pos)
int pos = Convert.ToInt32(CarTextBox.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawTank.Image = _cars.ShowCars();
DrawTank.Image = obj.ShowCars();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
DrawTank.Image = _cars.ShowCars();
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawTank.Image = obj.ShowCars();
}
}
}

View File

@ -14,12 +14,12 @@ namespace Lab.DrawningObjects
{
if (GasolineTanker != null)
{
GasolineTanker = new AddBaseCar(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
}
}
public override void DrawTransport(Graphics g)
{
if (GasolineTanker is not AddBaseCar Gasoline)
if (GasolineTanker is not GasolineTanker Gasoline)
return;
base.DrawTransport(g);
if (Gasoline.BodyKit)

View File

@ -11,7 +11,7 @@ namespace Lab.DrawningObjects
{
public class DrawTanker
{
public BaseCar? GasolineTanker { get; protected set; }
public BaseTanker? GasolineTanker { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
@ -42,7 +42,7 @@ namespace Lab.DrawningObjects
{
_pictureHeight = height;
_pictureWidth = width;
GasolineTanker = new BaseCar(speed, weight, bodyColor);
GasolineTanker = new BaseTanker(speed, weight, bodyColor);
}
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight)
@ -51,7 +51,7 @@ namespace Lab.DrawningObjects
_pictureWidth = width;
_carHeight = carHeight;
_carWidth = carWidth;
GasolineTanker = new BaseCar(speed, weight, bodyColor);
GasolineTanker = new BaseTanker(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)

View File

@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace Lab.Entities
{
public class AddBaseCar : BaseCar
public class GasolineTanker : BaseTanker
{
public Color AdditionalColor { get; private set; }
public bool BodyKit { get; private set; }
public bool Wing { get; private set; }
public bool SportLine { get; private set; }
public AddBaseCar(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine) : base(speed, weight, bodyColor)
public GasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
BodyKit = bodyKit;

View File

@ -8,77 +8,69 @@ namespace Lab.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
private readonly List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(_maxCount);
}
public int Insert(T car)
public bool Insert(T car)
{
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] = car;
return 0;
return Insert(car, 0);
}
public int Insert(T car, int position)
public bool Insert(T car, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] == null)
{
_places[position] = car;
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] = car;
return position;
if (position < 0 || position >= _maxCount)
return false;
if (Count >= _maxCount)
return false;
_places.Insert(0, car);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position > _maxCount)
return false;
_places[position] = null;
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?> GetCars(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
}
}