Готовая лабораторная №6

This commit is contained in:
Stranni15k 2022-12-22 23:00:37 +04:00
parent 227bfeed97
commit e3b123b725
11 changed files with 298 additions and 56 deletions

View File

@ -14,25 +14,23 @@ namespace ElectricLocomotive
_locomotive = locomotive;
}
public float Step => _locomotive?.Locomotive?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _locomotive?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_locomotive?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_locomotive.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
_locomotive.DrawTransport(g);
}
public string GetInfo() => _locomotive?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningCar());
}
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

View File

@ -8,6 +8,11 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Include="ElectricLocomotive.csproj" />
<None Include="ElectricLocomotive.csproj.user" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
{
internal static class ExtentionLocomotive
{
private static readonly char _separatorForObject = ':';
public static DrawningLocomotive CreateDrawningCar(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawningLocomotive(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 6)
{
return new DrawningElectricLocomotive(Convert.ToInt32(strs[0]),Convert.ToInt32(strs[1]), Color.FromName(strs[2]),Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5]));
}
return null;
}
public static string GetDataForSave(this DrawningLocomotive drawningCar)
{
var car = drawningCar.Locomotive;
var str = $"{car.Speed}{_separatorForObject}{car.Weight}{_separatorForObject}{car.BodyColor.Name}";
if (car is not EntityElectricLocomotive sportCar)
{
return str;
}
return
$"{str}{_separatorForObject}{sportCar.DopColor.Name}{_separatorForObject}{sportCar.BodyKit}{_separatorForObject}{sportCar.SportLine}";
}
}
}

View File

@ -29,10 +29,6 @@
private void InitializeComponent()
{
this.ButtonCreate = new System.Windows.Forms.Button();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
@ -40,8 +36,12 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.buttonselectlocomotive = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// ButtonCreate
@ -56,36 +56,6 @@
this.ButtonCreate.UseVisualStyleBackColor = true;
this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17);
this.toolStripStatusLabelWeight.Text = "Вес";
//
// toolStripStatusLabelColor
//
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17);
this.toolStripStatusLabelColor.Text = "Цвет";
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
@ -170,6 +140,36 @@
this.buttonselectlocomotive.UseVisualStyleBackColor = true;
this.buttonselectlocomotive.Click += new System.EventHandler(this.ButtonSelectLocomotive_Click);
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17);
this.toolStripStatusLabelWeight.Text = "Вес";
//
// toolStripStatusLabelColor
//
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17);
this.toolStripStatusLabelColor.Text = "Цвет";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// FormLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -187,9 +187,9 @@
this.Name = "FormLocomotive";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Локомотив";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -198,10 +198,6 @@
#endregion
private Button ButtonCreate;
private StatusStrip statusStrip1;
private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelColor;
private PictureBox pictureBox1;
private Button buttonLeft;
private Button buttonDown;
@ -209,5 +205,9 @@
private Button buttonUp;
private Button buttonCreateModif;
private Button buttonselectlocomotive;
private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelColor;
private StatusStrip statusStrip1;
}
}

View File

@ -44,7 +44,14 @@
this.ListBoxMaps = new System.Windows.Forms.ListBox();
this.ButtonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
@ -113,7 +120,7 @@
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(837, 19);
this.label1.Location = new System.Drawing.Point(837, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(83, 15);
this.label1.TabIndex = 15;
@ -219,11 +226,53 @@
this.textBoxNewMapName.Size = new System.Drawing.Size(151, 23);
this.textBoxNewMapName.TabIndex = 30;
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1000, 24);
this.menuStrip1.TabIndex = 31;
this.menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.файлToolStripMenuItem.Name = айлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
this.файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog1";
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormMapWithSetLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1000, 722);
this.Controls.Add(this.menuStrip1);
this.Controls.Add(this.textBoxNewMapName);
this.Controls.Add(this.ButtonAddMap);
this.Controls.Add(this.ListBoxMaps);
@ -240,9 +289,12 @@
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.pictureBox1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormMapWithSetLocomotive";
this.Text = "Карта с набором объектов";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@ -265,5 +317,11 @@
private ListBox ListBoxMaps;
private Button ButtonAddMap;
private TextBox textBoxNewMapName;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -197,6 +197,39 @@ namespace ElectricLocomotive
ReloadMaps();
}
}
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 LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
{
ReloadMaps();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -57,4 +57,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>279, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>146, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -13,5 +13,6 @@ namespace ElectricLocomotive
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
namespace ElectricLocomotive
{
internal class MapWithSetLocomotivGeneric<T, U>
where T : class, IDrawningObject
@ -138,5 +132,21 @@ namespace ElectricLocomotive
}
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var boat in _setLocomotive.GetLocomotive())
{
data += $"{boat.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setLocomotive.Insert(DrawningObjectLocomotive.Create(rec) as T);
}
}
}
}

View File

@ -8,13 +8,16 @@ namespace ElectricLocomotive
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetLocomotivGeneric<DrawningObjectLocomotive, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetLocomotivGeneric<IDrawningObject, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetLocomotivGeneric<DrawningObjectLocomotive, AbstractMap>>();
_mapStorages = new Dictionary<string, MapWithSetLocomotivGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -22,14 +25,14 @@ namespace ElectricLocomotive
{
if (!_mapStorages.ContainsKey(name))
{
_mapStorages.Add(name, new MapWithSetLocomotivGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.Add(name, new MapWithSetLocomotivGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
}
public MapWithSetLocomotivGeneric<DrawningObjectLocomotive, AbstractMap> this[string ind]
public MapWithSetLocomotivGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
@ -40,5 +43,66 @@ namespace ElectricLocomotive
return null;
}
}
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter fs = new StreamWriter(filename))
{
fs.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string str = "";
using (StreamReader fs = new StreamReader(filename))
{
str = fs.ReadLine();
if (!str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
_mapStorages.Clear();
while ((str = fs.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "BushesMap":
map = new BushesMap();
break;
case "FieldMap":
map = new FieldMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetLocomotivGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
}
}