Лабораторная №6

This commit is contained in:
yuliya.mavrina@internet.ru 2023-11-25 18:01:23 +03:00
parent c47a81c735
commit f8ce47bbd3
7 changed files with 258 additions and 39 deletions

View File

@ -67,8 +67,6 @@ namespace DoubleDeckerBus.DrawningObjects
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 60)), new Point((int)(_startPosX + 10), (int)(_startPosY + 60)));
}
}
}
}
}

View File

@ -0,0 +1,46 @@
using DoubleDeckerBus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.DrawningObjects
{
public static class ExtentionDrawningBus
{
public static DrawningBus? CreateDrawningBus(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawningBus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 7)
{
return new DrawningDoubleDeckerBus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), width, height);
}
return null;
}
public static string GetDataForSave(this DrawningBus drawningbus, char separatorForObject)
{
var bus = drawningbus.EntityBus;
if (bus == null)
{
return string.Empty;
}
var str = $"{bus.Speed}{separatorForObject}{bus.Weight}{separatorForObject}{bus.BodyColor.Name}";
if (bus is not EntityDoubleDeckerBus doubleDeckerBus)
{
return str;
}
return
$"{str}{separatorForObject}{doubleDeckerBus.AdditionalColor.Name}{separatorForObject}{doubleDeckerBus.SecondFloor}" +
$"{separatorForObject}{doubleDeckerBus.Ladder}{separatorForObject}{doubleDeckerBus.LineBetweenFloor}";
}
}
}

View File

@ -38,14 +38,20 @@
listBoxStorages = new ListBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
menuStrip = new MenuStrip();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panel.SuspendLayout();
menuStrip.SuspendLayout();
SuspendLayout();
//
// buttonAddBus
//
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddBus.Location = new Point(13, 277);
buttonAddBus.Location = new Point(19, 277);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(138, 31);
buttonAddBus.TabIndex = 0;
@ -56,7 +62,7 @@
// buttonRemoveBus
//
buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonRemoveBus.Location = new Point(13, 343);
buttonRemoveBus.Location = new Point(19, 343);
buttonRemoveBus.Name = "buttonRemoveBus";
buttonRemoveBus.Size = new Size(138, 31);
buttonRemoveBus.TabIndex = 1;
@ -67,7 +73,7 @@
// ButtonRefreshCollection
//
ButtonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonRefreshCollection.Location = new Point(13, 406);
ButtonRefreshCollection.Location = new Point(19, 406);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(138, 31);
ButtonRefreshCollection.TabIndex = 2;
@ -78,7 +84,7 @@
// maskedTextBoxNumber
//
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right;
maskedTextBoxNumber.Location = new Point(30, 314);
maskedTextBoxNumber.Location = new Point(36, 314);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 23);
maskedTextBoxNumber.TabIndex = 3;
@ -86,7 +92,7 @@
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(2, 2);
pictureBoxCollection.Location = new Point(12, 12);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(633, 454);
pictureBoxCollection.TabIndex = 4;
@ -103,17 +109,18 @@
panel.Controls.Add(maskedTextBoxNumber);
panel.Controls.Add(ButtonRefreshCollection);
panel.Controls.Add(buttonRemoveBus);
panel.Location = new Point(660, 2);
panel.Controls.Add(menuStrip);
panel.Location = new Point(676, 12);
panel.Name = "panel";
panel.Size = new Size(162, 454);
panel.Size = new Size(168, 454);
panel.TabIndex = 5;
panel.Tag = "";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(13, 20);
textBoxStorageName.Location = new Point(13, 36);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(131, 23);
textBoxStorageName.Size = new Size(138, 23);
textBoxStorageName.TabIndex = 9;
//
// listBoxStorages
@ -146,11 +153,44 @@
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// menuStrip
//
menuStrip.Items.AddRange(new ToolStripItem[] { SaveToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(168, 24);
menuStrip.TabIndex = 10;
menuStrip.Text = "menuStrip1";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { LoadToolStripMenuItem });
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(144, 20);
SaveToolStripMenuItem.Text = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(201, 22);
LoadToolStripMenuItem.Text = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "«txt file | *.txt»";
//
// saveFileDialog
//
saveFileDialog.Filter = "«txt file | *.txt»";
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(823, 468);
ClientSize = new Size(879, 461);
Controls.Add(panel);
Controls.Add(pictureBoxCollection);
Name = "FormBusCollection";
@ -158,6 +198,8 @@
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
panel.ResumeLayout(false);
panel.PerformLayout();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false);
}
@ -174,5 +216,10 @@
private Button ButtonDelObject;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private MenuStrip menuStrip;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
}
}

View File

@ -9,9 +9,9 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using DoubleDeckerBus.DrawningObjects;
using DoubleDeckerBus.Generics;
using DoubleDeckerBus.Generics.ProjectSportCar.Generics;
using DoubleDeckerBus.MovementStrategy;
using static System.Windows.Forms.DataFormats;
using System.Windows.Forms;
namespace DoubleDeckerBus
{
@ -144,5 +144,39 @@ namespace DoubleDeckerBus
}
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,16 @@
<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>11, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>132, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>271, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>48</value>
</metadata>
</root>

View File

@ -12,6 +12,7 @@ namespace DoubleDeckerBus.Generics
where T : DrawningBus
where U : IMoveableObject
{
public IEnumerable<T?> GetTheBuses => _collection.GetTheBuses();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;

View File

@ -7,8 +7,6 @@ using DoubleDeckerBus.DrawningObjects;
using DoubleDeckerBus.MovementStrategy;
namespace DoubleDeckerBus.Generics
{
namespace ProjectSportCar.Generics
{
internal class BusesGenericStorage
{
@ -16,12 +14,96 @@ namespace DoubleDeckerBus.Generics
public List<string> Keys => _busStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public BusesGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, BusesGenericCollection<DrawningBus, DrawningObjectBus>> record in _busStorages)
{
StringBuilder records = new();
foreach (DrawningBus? elem in record.Value.GetTheBuses)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
string toWrite = $"BusStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("BusStorage"))
{
return false;
}
_busStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
BusesGenericCollection<DrawningBus, DrawningObjectBus> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningBus? bus =
elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight);
if (bus != null)
{
if (!(collection + bus))
{
return false;
}
}
}
_busStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
return true;
}
public void AddSet(string name)
{
_busStorages.Add(name, new BusesGenericCollection<DrawningBus, DrawningObjectBus>(_pictureWidth, _pictureHeight));
@ -50,4 +132,3 @@ namespace DoubleDeckerBus.Generics
}
}
}
}