1 Commits
lab8 ... lab7

Author SHA1 Message Date
6e4f175cd6 зафиксировать 2023-12-25 21:39:51 +04:00
10 changed files with 53 additions and 299 deletions

View File

@@ -21,7 +21,8 @@ namespace ProjectBoat_bae.DrawningObjects
if (EntityBoat != null)
{
EntityBoat = new EntityMotorBoat(speed, weight, bodyColor,
additionalColor, body, wing);
additionalColor, body, wing);
}
}

View File

@@ -43,8 +43,6 @@
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
ButtonSortByType = new Button();
ButtonSortByColor = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout();
@@ -61,16 +59,16 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 25;
listBoxStorages.Location = new Point(935, 130);
listBoxStorages.Location = new Point(935, 146);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(231, 79);
listBoxStorages.Size = new Size(231, 129);
listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// ButtonAddObject
//
ButtonAddObject.BackColor = SystemColors.ButtonHighlight;
ButtonAddObject.Location = new Point(935, 86);
ButtonAddObject.Location = new Point(935, 102);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(231, 38);
ButtonAddObject.TabIndex = 2;
@@ -80,7 +78,7 @@
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(935, 49);
textBoxStorageName.Location = new Point(935, 65);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(231, 31);
textBoxStorageName.TabIndex = 3;
@@ -88,7 +86,7 @@
// ButtonDelObject
//
ButtonDelObject.BackColor = SystemColors.ButtonHighlight;
ButtonDelObject.Location = new Point(935, 215);
ButtonDelObject.Location = new Point(935, 281);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(231, 34);
ButtonDelObject.TabIndex = 4;
@@ -156,14 +154,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(212, 34);
SaveToolStripMenuItem.Size = new Size(270, 34);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenu_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(212, 34);
LoadToolStripMenuItem.Size = new Size(270, 34);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenu_Click;
//
@@ -176,35 +174,11 @@
//
saveFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortByType
//
ButtonSortByType.BackColor = SystemColors.ButtonHighlight;
ButtonSortByType.Location = new Point(935, 284);
ButtonSortByType.Name = "ButtonSortByType";
ButtonSortByType.Size = new Size(231, 34);
ButtonSortByType.TabIndex = 10;
ButtonSortByType.Text = "Сортировать по типу";
ButtonSortByType.UseVisualStyleBackColor = false;
ButtonSortByType.Click += ButtonSortByType_Click;
//
// ButtonSortByColor
//
ButtonSortByColor.BackColor = SystemColors.ButtonHighlight;
ButtonSortByColor.Location = new Point(935, 329);
ButtonSortByColor.Name = "ButtonSortByColor";
ButtonSortByColor.Size = new Size(231, 34);
ButtonSortByColor.TabIndex = 11;
ButtonSortByColor.Text = "Сортировать по цвету";
ButtonSortByColor.UseVisualStyleBackColor = false;
ButtonSortByColor.Click += ButtonSortByColor_Click;
//
// FormBoatCollection
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1178, 644);
Controls.Add(ButtonSortByColor);
Controls.Add(ButtonSortByType);
Controls.Add(menuStrip1);
Controls.Add(textBoxBoat);
Controls.Add(ButtonRefreshCollection);
@@ -242,7 +216,5 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@@ -15,7 +15,6 @@ using ProjectBoat_bae.Generics;
using ProjectBoat_bae.Exceptions;
using ProjectBoat_bae.MovementStrategy;
using NLog;
using System.Numerics;
namespace ProjectBoat_bae
{
@@ -25,7 +24,7 @@ namespace ProjectBoat_bae
private readonly BoatsGenericStorage _storage;
// Логер
private readonly
private readonly
Microsoft.Extensions.Logging.ILogger? _logger;
public FormBoatCollection(ILogger<FormBoatCollection> logger)
@@ -62,10 +61,11 @@ namespace ProjectBoat_bae
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text.ToString());
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
_logger.LogInformation($"Added set: {textBoxStorageName.Text}");
}
@@ -73,34 +73,27 @@ namespace ProjectBoat_bae
// Удаление набора
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"Delete Object {name}?", "Deleting",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
_storage.DelSet(name);
ReloadObjects();
_logger.LogInformation($"Deleted set: {name}");
MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены");
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
}
// Добавление объекта в набор
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;
}
FormBoatConfig formPlaneConfig = new FormBoatConfig();
formPlaneConfig.AddEvent(AddBoat);
formPlaneConfig.Show();
@@ -108,28 +101,21 @@ namespace ProjectBoat_bae
private void AddBoat(Drawningboat boat)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
boat._pictureWidth = pictureBoxCollection.Width;
boat._pictureHeight = pictureBoxCollection.Height;
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
if (obj == null) return;
if (obj + boat)
{
_logger.LogWarning("Добавление пустого объекта");
return;
}
try
{
_ = obj + boat;
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowBoats();
_logger.LogInformation($"plane added in set {listBoxStorages.SelectedItem.ToString()}");
_logger.LogInformation($"Добавлен объект {obj}");
}
catch (Exception ex)
else
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"plane not added in set {listBoxStorages.SelectedItem.ToString()}");
MessageBox.Show("Не удалось добавить объект");
}
}
@@ -137,7 +123,7 @@ namespace ProjectBoat_bae
//удаление объекта
private void ButtonRemoveBoat_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
@@ -147,7 +133,7 @@ namespace ProjectBoat_bae
{
return;
}
if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo,
if (MessageBox.Show("Удалить объект?", "Удалить", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
{
return;
@@ -157,24 +143,24 @@ namespace ProjectBoat_bae
int pos = Convert.ToInt32(textBoxBoat.Text);
if (obj - pos != null)
{
MessageBox.Show("Object deleted");
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowBoats();
_logger.LogInformation($"plane deleted in set {listBoxStorages.SelectedItem.ToString()}");
_logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Object not deleted");
_logger.LogWarning($"plane not deleted in set {listBoxStorages.SelectedItem.ToString()}");
MessageBox.Show("Объект не удален");
_logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}");
}
}
catch (BoatNotFoundException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"PlaneNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
_logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show("Not input");
MessageBox.Show("Объекта нет");
_logger.LogWarning("Not input");
}
}
@@ -182,12 +168,12 @@ namespace ProjectBoat_bae
// Обновление
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
@@ -205,7 +191,7 @@ namespace ProjectBoat_bae
//сохранение
private void SaveToolStripMenu_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
@@ -227,7 +213,7 @@ namespace ProjectBoat_bae
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
@@ -243,21 +229,5 @@ namespace ProjectBoat_bae
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType());
public void CompareBoats(IComparer<Drawningboat?> comparer)
{
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowBoats();
}
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor());
}
}

View File

@@ -1,37 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBoat_bae.DrawningObjects;
using ProjectBoat_bae.Entities;
namespace ProjectBoat_bae.Generics
{
internal class BoatCompareByColor : IComparer<Drawningboat?>
{
public int Compare(Drawningboat? x, Drawningboat? y)
{
if (x == null || x.EntityBoat == null)
throw new NotImplementedException(nameof(x));
if (y == null || y.EntityBoat == null)
throw new NotImplementedException(nameof(y));
var bodyColor = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name);
if (bodyColor != 0) return bodyColor;
if (x.EntityBoat is EntityMotorBoat &&
y.EntityBoat is EntityMotorBoat)
{
var addcolor = (x.EntityBoat as EntityMotorBoat).AdditionalColor.Name.CompareTo((y.EntityBoat
as EntityMotorBoat).AdditionalColor.Name);
if (addcolor != 0) return addcolor;
}
return 1;
}
}
}

View File

@@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBoat_bae.DrawningObjects;
namespace ProjectBoat_bae.Generics
{
internal class BoatCompareByType : IComparer<Drawningboat?>
{
public int Compare(Drawningboat? x, Drawningboat? y)
{
if (x == null || x.EntityBoat == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityBoat == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight);
}
}
}

View File

@@ -1,36 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBoat_bae.Generics
{
internal class BoatsCollectionInfo : IEquatable<BoatsCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public BoatsCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(BoatsCollectionInfo other)
{
if (Name != other?.Name)
throw new NotImplementedException(nameof(Name));
return true;
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}

View File

@@ -45,7 +45,7 @@ namespace ProjectBoat_bae.Generics
{
return false;
}
return collect._collection.Insert(obj, new DrawingBoatEqutables());
return (bool)collect._collection.Insert(obj);
}
// Перегрузка оператора вычитания
@@ -111,6 +111,5 @@ namespace ProjectBoat_bae.Generics
// Получение объектов коллекции
public IEnumerable<T?> GetBoats => _collection.GetBoats();
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
}
}

View File

@@ -15,7 +15,6 @@ namespace ProjectBoat_bae.Generics
internal class BoatsGenericStorage
{
readonly Dictionary<string, BoatsGenericCollection<Drawningboat, DrawningObjectBoat>> _boatStorages;
public List<string> Keys => _boatStorages.Keys.ToList();
private readonly int _pictureWidth;
@@ -28,7 +27,6 @@ namespace ProjectBoat_bae.Generics
}
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name)) return;
_boatStorages[name] = new BoatsGenericCollection<Drawningboat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
}

View File

@@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBoat_bae.DrawningObjects;
using ProjectBoat_bae.Entities;
namespace ProjectBoat_bae.Generics
{
internal class DrawingBoatEqutables : IEqualityComparer<Drawningboat?>
{
public bool Equals(Drawningboat? x, Drawningboat? y)
{
if (x == null || x.EntityBoat == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityBoat == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
return false;
if (x.EntityBoat.Speed != y.EntityBoat.Speed)
return false;
if (x.EntityBoat.Weight != y.EntityBoat.Weight)
return false;
if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor)
return false;
// to do logic for "сравнения" additional parameters :)
if (x is DrawningMotorBoat && y is DrawningMotorBoat)
{
if ((x.EntityBoat as EntityMotorBoat).AdditionalColor != (y.EntityBoat as EntityMotorBoat).AdditionalColor)
{
return false;
}
if ((x.EntityBoat as EntityMotorBoat).Body != (y.EntityBoat as EntityMotorBoat).Body)
{
return false;
}
if ((x.EntityBoat as EntityMotorBoat).Wing != (y.EntityBoat as EntityMotorBoat).Wing)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] Drawningboat obj)
{
return obj.GetHashCode();
}
}
}

View File

@@ -4,8 +4,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBoat_bae.Exceptions;
namespace ProjectBoat_bae.Generics
{
internal class SetGeneric<T> where T : class
@@ -16,8 +14,6 @@ namespace ProjectBoat_bae.Generics
// Максимальное количество объектов в списке
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
// Конструктор
public SetGeneric(int count)
{
@@ -25,47 +21,27 @@ namespace ProjectBoat_bae.Generics
_places = new List<T?>(_maxCount);
}
public bool Insert(T boat, IEqualityComparer<T?>? equal = null)
public bool Insert(T boat)
{
if (equal != null)
{
foreach (var secondBoat in _places)
{
if (equal.Equals(boat, secondBoat))
{
throw new Exception("Такой объект уже есть в коллекции");
}
}
}
return Insert(boat, 0);
}
public bool Insert(T boat, int position, IEqualityComparer<T?>? equal = null)
public bool Insert(T boat, int position)
{
if (position < 0 || position >= _maxCount)
throw new StorageOverflowException("Impossible to insert");
return false;
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null)
{
foreach (var secondBoat in _places)
{
if (equal.Equals(boat, secondBoat))
{
throw new ApplicationException("Такой объект уже есть в коллекции");
}
}
}
_places.Insert(position, boat);
return false;
_places.Insert(0, boat);
return true;
}
public bool Remove(int position)
{
if (position >= Count || position < 0)
throw new BoatNotFoundException("Invalid operation");
if (_places[position] == null)
throw new BoatNotFoundException(position);
if (position < 0 || position > _maxCount)
return false;
if (position >= Count)
return false;
_places.RemoveAt(position);
return true;
}