PIbd-23. Yunusov N.N. Lab work 06 #6

Closed
Yunusov_Niyaz wants to merge 2 commits from Lab6 into Lab5
6 changed files with 292 additions and 32 deletions

View File

@ -5,7 +5,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTrolleybus.DrawingObjects;
using ProjectTrolleybus.Generics;
namespace ProjectTrolleybus.Generics
{
@ -130,5 +129,6 @@ namespace ProjectTrolleybus.Generics
i++;
}
}
public IEnumerable<T?> GetBuses => _collection.GetBuses();
}
}

View File

@ -28,14 +28,25 @@ namespace ProjectTrolleybus.Generics
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Разделитель для записи ключа и значения элемента словаря
/// </summary>
private static readonly char _separatorForKeyValue = '|';
/// <summary>
/// Разделитель для записей коллекции данных в файл
/// </summary>
private readonly char _separatorRecords = ';';
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static readonly char _separatorForObject = ':';
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public BusesGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string,
BusesGenericCollection<DrawingBus, DrawingObjectBus>>();
_busStorages = new Dictionary<string, BusesGenericCollection<DrawingBus, DrawingObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -63,8 +74,7 @@ namespace ProjectTrolleybus.Generics
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BusesGenericCollection<DrawingBus, DrawingObjectBus>?
this[string ind]
public BusesGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
{
get
{
@ -73,5 +83,88 @@ namespace ProjectTrolleybus.Generics
return null;
}
}
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
BusesGenericCollection<DrawingBus, DrawingObjectBus>> record in _busStorages)
{
StringBuilder records = new();
foreach (DrawingBus? elem in record.Value.GetBuses)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter streamWriter = new(filename))
{
streamWriter.WriteLine($"BusStorages{Environment.NewLine}{data}");
}
return true;
}
/// <summary>
/// Загрузка информации по автомобилям в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader streamReader = new(filename))
{
string str = streamReader.ReadLine();
var strings = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strings == null || strings.Length == 0)
{
return false;
}
if (!strings[0].StartsWith("BusStorages"))
{
return false;
}
_busStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = streamReader.ReadLine();
continue;
}
BusesGenericCollection<DrawingBus, DrawingObjectBus> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingBus? bus = elem?.CreateDrawingBus(_separatorForObject, _pictureWidth, _pictureHeight);
if (bus != null)
{
if (!(collection + bus))
{
return false;
}
}
}
_busStorages.Add(record[0], collection);
str = streamReader.ReadLine();
} while (str != null);
}
return true;
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTrolleybus.Entities;
namespace ProjectTrolleybus.DrawingObjects
{
public static class ExtentionDrawingBus
{
public static DrawingBus? CreateDrawingBus(this string info, char separatorForObject, int width, int height)
{
string[] strings = info.Split(separatorForObject);
if (strings.Length == 3)
{
return new DrawingBus(Convert.ToInt32(strings[0]),
Convert.ToInt32(strings[1]), Color.FromName(strings[2]), width, height);
}
if (strings.Length == 6)
{
return new DrawingTrolleybus(Convert.ToInt32(strings[0]),
Convert.ToInt32(strings[1]),
Color.FromName(strings[2]),
Color.FromName(strings[3]),
Convert.ToBoolean(strings[4]),
Convert.ToBoolean(strings[5]),
width, height);
}
return null;
}
public static string GetDataForSave(this DrawingBus drawingBus, char separatorForObject)
{
var bus = drawingBus.EntityBus;
if (bus == null)
{
return string.Empty;
}
var str = $"{bus.Speed}{separatorForObject}{bus.Weight}{separatorForObject}{bus.BodyColor.Name}";
if (bus is not EntityTrolleybus trolleybus)
{
return str;
}
return $"{str}{separatorForObject}{trolleybus.AdditionalColor.Name}{separatorForObject}{trolleybus.Roga}{separatorForObject}{trolleybus.Battery}";
}
}
}

View File

@ -39,9 +39,16 @@
maskedTextBoxNumber = new MaskedTextBox();
buttonAddBus = new Button();
pictureBoxCollection = new PictureBox();
menuStrip = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
groupBoxTrolleybus.SuspendLayout();
groupBoxSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout();
//
// groupBoxTrolleybus
@ -51,9 +58,11 @@
groupBoxTrolleybus.Controls.Add(buttonDeleteBus);
groupBoxTrolleybus.Controls.Add(maskedTextBoxNumber);
groupBoxTrolleybus.Controls.Add(buttonAddBus);
groupBoxTrolleybus.Location = new Point(538, 2);
groupBoxTrolleybus.Location = new Point(615, 32);
groupBoxTrolleybus.Margin = new Padding(3, 4, 3, 4);
groupBoxTrolleybus.Name = "groupBoxTrolleybus";
groupBoxTrolleybus.Size = new Size(262, 448);
groupBoxTrolleybus.Padding = new Padding(3, 4, 3, 4);
groupBoxTrolleybus.Size = new Size(299, 568);
groupBoxTrolleybus.TabIndex = 0;
groupBoxTrolleybus.TabStop = false;
groupBoxTrolleybus.Text = "Инструменты";
@ -65,25 +74,29 @@
groupBoxSets.Controls.Add(buttonDelObject);
groupBoxSets.Controls.Add(listBoxStorages);
groupBoxSets.Controls.Add(buttonAddObject);
groupBoxSets.Location = new Point(6, 22);
groupBoxSets.Location = new Point(11, 28);
groupBoxSets.Margin = new Padding(3, 4, 3, 4);
groupBoxSets.Name = "groupBoxSets";
groupBoxSets.Size = new Size(245, 234);
groupBoxSets.Padding = new Padding(3, 4, 3, 4);
groupBoxSets.Size = new Size(280, 312);
groupBoxSets.TabIndex = 4;
groupBoxSets.TabStop = false;
groupBoxSets.Text = "Наборы";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 22);
textBoxStorageName.Location = new Point(7, 29);
textBoxStorageName.Margin = new Padding(3, 4, 3, 4);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(233, 23);
textBoxStorageName.Size = new Size(266, 27);
textBoxStorageName.TabIndex = 4;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(6, 194);
buttonDelObject.Location = new Point(7, 259);
buttonDelObject.Margin = new Padding(3, 4, 3, 4);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(233, 30);
buttonDelObject.Size = new Size(266, 40);
buttonDelObject.TabIndex = 3;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
@ -92,19 +105,21 @@
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 94);
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(7, 125);
listBoxStorages.Margin = new Padding(3, 4, 3, 4);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(233, 94);
listBoxStorages.Size = new Size(266, 124);
listBoxStorages.TabIndex = 2;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// buttonAddObject
//
buttonAddObject.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddObject.Location = new Point(6, 62);
buttonAddObject.Location = new Point(7, 83);
buttonAddObject.Margin = new Padding(3, 4, 3, 4);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(233, 26);
buttonAddObject.Size = new Size(266, 35);
buttonAddObject.TabIndex = 1;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
@ -112,9 +127,10 @@
//
// buttonUpdateCollection
//
buttonUpdateCollection.Location = new Point(10, 408);
buttonUpdateCollection.Location = new Point(11, 499);
buttonUpdateCollection.Margin = new Padding(3, 4, 3, 4);
buttonUpdateCollection.Name = "buttonUpdateCollection";
buttonUpdateCollection.Size = new Size(241, 28);
buttonUpdateCollection.Size = new Size(275, 37);
buttonUpdateCollection.TabIndex = 3;
buttonUpdateCollection.Text = "Обновить коллекцию";
buttonUpdateCollection.UseVisualStyleBackColor = true;
@ -122,9 +138,10 @@
//
// buttonDeleteBus
//
buttonDeleteBus.Location = new Point(10, 343);
buttonDeleteBus.Location = new Point(11, 439);
buttonDeleteBus.Margin = new Padding(3, 4, 3, 4);
buttonDeleteBus.Name = "buttonDeleteBus";
buttonDeleteBus.Size = new Size(241, 29);
buttonDeleteBus.Size = new Size(275, 39);
buttonDeleteBus.TabIndex = 2;
buttonDeleteBus.Text = "Удалить автобус";
buttonDeleteBus.UseVisualStyleBackColor = true;
@ -132,17 +149,19 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(70, 305);
maskedTextBoxNumber.Location = new Point(74, 404);
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(115, 23);
maskedTextBoxNumber.Size = new Size(131, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// buttonAddBus
//
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddBus.Location = new Point(10, 262);
buttonAddBus.Location = new Point(11, 348);
buttonAddBus.Margin = new Padding(3, 4, 3, 4);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(241, 28);
buttonAddBus.Size = new Size(275, 37);
buttonAddBus.TabIndex = 0;
buttonAddBus.Text = "Добавить автобус";
buttonAddBus.UseVisualStyleBackColor = true;
@ -151,19 +170,64 @@
// pictureBoxCollection
//
pictureBoxCollection.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
pictureBoxCollection.Location = new Point(0, 2);
pictureBoxCollection.Location = new Point(0, 32);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(537, 448);
pictureBoxCollection.Size = new Size(616, 568);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(920, 28);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "busStorages";
openFileDialog.Filter = "«txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.FileName = "busStorages";
saveFileDialog.Filter = "«txt file | *.txt";
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(805, 450);
ClientSize = new Size(920, 600);
Controls.Add(groupBoxTrolleybus);
Controls.Add(pictureBoxCollection);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Margin = new Padding(3, 4, 3, 4);
Name = "FormBusCollection";
StartPosition = FormStartPosition.CenterScreen;
Text = "Набор автобусов";
@ -172,7 +236,10 @@
groupBoxSets.ResumeLayout(false);
groupBoxSets.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -188,5 +255,11 @@
private ListBox listBoxStorages;
private Button buttonAddObject;
private TextBox textBoxStorageName;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -81,7 +81,8 @@ namespace ProjectTrolleybus
}
FormBusConfig form = new FormBusConfig(pictureBoxCollection.Width, pictureBoxCollection.Height);
form.Show();
Action<DrawingBus>? busDelegate = new((bus) => {
Action<DrawingBus>? busDelegate = new((bus) =>
{
if (obj + bus)
{
MessageBox.Show("Объект добавлен");
@ -134,6 +135,41 @@ namespace ProjectTrolleybus
}
pictureBoxCollection.Image = obj.ShowBuses();
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -117,4 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>153, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>315, 17</value>
</metadata>
</root>