Yahontov O.U. LabWork6 #7

Closed
Arklightning wants to merge 2 commits from LabWork6 into LabWork5
11 changed files with 246 additions and 14 deletions

View File

@ -41,7 +41,9 @@ namespace Trolleybus
void IDrawningObject.DrawningObject(Graphics g)
{
// TODO
_trolleybus.DrawTransport(g);
}
public string GetInfo() => _trolleybus?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
}
}

View File

@ -42,5 +42,12 @@ namespace Trolleybus
{
_trolleybus.DrawTransport(g);
}
public string GetInfo() => _trolleybus?.GetDataForSave();
public static IDrawningObject Create(string data)
{
return new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
{
internal static class ExtentionTrolleybus
{
private static readonly char _separatorForObject = ':';
public static DrawingTrolleybus CreateDrawingTrolleybus(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawingTrolleybus(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 6)
{
return new DrawningSmallTrolleybus(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]));
}
return null;
}
public static string GetDataForSave(this DrawingTrolleybus drawningTrolleybus)
{
var trolleybus = drawningTrolleybus.Trolleybus;
var str = $"{trolleybus.Speed}{_separatorForObject}{trolleybus.Weight}{_separatorForObject}{trolleybus.BodyColor.Name}";
if (trolleybus is not EntitySmallTrolleybus smallTrolleybus)
{
return str;
}
return $"{str}{_separatorForObject}{smallTrolleybus.DopColor.Name}{_separatorForObject}{smallTrolleybus.Horns}{_separatorForObject}{smallTrolleybus.Battary}";
}
}
}

View File

@ -45,9 +45,16 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddTrolleybus = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.FiletoolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.downloadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
@ -63,11 +70,11 @@
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddTrolleybus);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(900, 0);
this.groupBox1.Location = new System.Drawing.Point(900, 33);
this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.groupBox1.Size = new System.Drawing.Size(300, 997);
this.groupBox1.Size = new System.Drawing.Size(300, 964);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -245,13 +252,56 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(900, 997);
this.pictureBox.Size = new System.Drawing.Size(900, 964);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// menuStrip1
//
this.menuStrip1.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2);
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.FiletoolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1200, 33);
this.menuStrip1.TabIndex = 2;
this.menuStrip1.Text = "menuStrip1";
//
// FiletoolStripMenuItem
//
this.FiletoolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem,
this.downloadToolStripMenuItem});
this.FiletoolStripMenuItem.Name = "FiletoolStripMenuItem";
this.FiletoolStripMenuItem.Size = new System.Drawing.Size(54, 29);
this.FiletoolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// downloadToolStripMenuItem
//
this.downloadToolStripMenuItem.Name = "downloadToolStripMenuItem";
this.downloadToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.downloadToolStripMenuItem.Text = "Download";
this.downloadToolStripMenuItem.Click += new System.EventHandler(this.downloadToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormMapWithSetTrolleybus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
@ -259,6 +309,8 @@
this.ClientSize = new System.Drawing.Size(1200, 997);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.Name = "FormMapWithSetTrolleybus";
this.Text = "FormMapWithSetTrolleybus";
@ -267,7 +319,10 @@
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -290,5 +345,11 @@
private System.Windows.Forms.ListBox listBoxMaps;
private System.Windows.Forms.Button buttonAddMap;
private System.Windows.Forms.TextBox textBoxNewMapName;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem FiletoolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem downloadToolStripMenuItem;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
}
}

View File

@ -230,5 +230,38 @@ namespace Trolleybus
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
_mapsCollection.LoadData(openFileDialog.FileName);
ReloadMaps();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
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="menuStrip1.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>173, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>357, 17</value>
</metadata>
</root>

View File

@ -38,5 +38,6 @@ namespace Trolleybus
/// <returns></returns>
void nothing();
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -112,6 +112,24 @@ namespace Trolleybus
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var tractor in _setTrolleybus.GetTrolleybus())
{
data += $"{tractor.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setTrolleybus.Insert(DrawningObjectTrolleybus.Create(rec) as T);
}
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>

View File

@ -1,15 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Trolleybus
{
internal class MapsCollection
{
/// Словарь (хранилище) с картами
readonly Dictionary<string, MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus,
readonly Dictionary<string, MapWithSetTrolleybusGeneric<IDrawningObject,
AbstractMap>> _mapStorages;
/// Возвращение списка названий карт
public List<string> Keys => _mapStorages.Keys.ToList();
@ -17,13 +19,15 @@ namespace Trolleybus
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
/// Конструктор
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string,
MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>>();
MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -45,12 +49,64 @@ namespace Trolleybus
/// Доступ к депо
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> this[string ind]
public MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
_mapStorages.TryGetValue(ind, out var mapWithSetTractorsGeneric);
return mapWithSetTractorsGeneric;
_mapStorages.TryGetValue(ind, out var mapWithSetTrolleybusGeneric);
return mapWithSetTrolleybusGeneric;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
if (!str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
throw new FileFormatException("Формат данных в файле не правильный");
}
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "HardMap":
map = new AutoStopMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetTrolleybusGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, (char)StringSplitOptions.RemoveEmptyEntries));
}
}
}
}

View File

@ -68,13 +68,13 @@ namespace Trolleybus
Insert(value, position);
}
}
public IEnumerable<T> GetTractors()
public IEnumerable<T> GetTrolleybus()
{
foreach (var tractor in _places)
foreach (var trolleybus in _places)
{
if (tractor != null)
if (trolleybus != null)
{
yield return tractor;
yield return trolleybus;
}
else
{

View File

@ -45,6 +45,7 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractMap.cs" />
@ -55,6 +56,7 @@
<Compile Include="DrawningObjectTrolleybus.cs" />
<Compile Include="EntitySmallTrolleybus.cs" />
<Compile Include="EntityTrolleybus.cs" />
<Compile Include="ExtentionTrolleybus.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>