готовая лаба 6

This commit is contained in:
spacyboy 2023-12-12 22:38:46 +04:00
parent 305f7ef6bd
commit fa3ef0f00a
8 changed files with 354 additions and 121 deletions

View File

@ -10,7 +10,7 @@ namespace RoadTrain.Entities
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public EntityRoadTrain(int speed, double weight, Color bodyColor)
{

View File

@ -0,0 +1,52 @@
using RoadTrain.DrawingObjects;
using RoadTrain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RoadTrain
{
public static class ExtentionDrawingTrain
{
public static DrawingRoadTrain? CreateDrawingTrain(this string info, char
separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawingRoadTrain(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawingRoadTrainWithTank(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), width, height);
}
return null;
}
public static string GetDataForSave(this DrawingRoadTrain drawingTrain,
char separatorForObject)
{
var train = drawingTrain.EntityRoadTrain;
if (train == null)
{
return string.Empty;
}
var str =
$"{train.Speed}{separatorForObject}{train.Weight}{separatorForObject}{train.BodyColor.Name}";
if (train is not EntityRoadTrainWithTank roadTrain)
{
return str;
}
return
$"{str}{separatorForObject}{roadTrain.AdditionalColor.Name}{separatorForObject}{roadTrain.Tank}{separatorForObject}{roadTrain.Brush}";
}
}
}

View File

@ -34,14 +34,21 @@
buttonDelObject = new Button();
buttonnAddObject = new Button();
listBoxStorages = new ListBox();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
сохранениеToolStripMenuItem = new ToolStripMenuItem();
загрузкаToolStripMenuItem = new ToolStripMenuItem();
maskedTextBoxNumber = new MaskedTextBox();
labelCollection = new Label();
buttonRefreshCollection = new Button();
buttonRemoveTrain = new Button();
buttonAddTrain = new Button();
pictureBoxCollection = new PictureBox();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
panelCollection.SuspendLayout();
groupBoxSets.SuspendLayout();
menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@ -65,26 +72,27 @@
groupBoxSets.Controls.Add(buttonDelObject);
groupBoxSets.Controls.Add(buttonnAddObject);
groupBoxSets.Controls.Add(listBoxStorages);
groupBoxSets.Location = new Point(10, 32);
groupBoxSets.Controls.Add(menuStrip1);
groupBoxSets.Location = new Point(10, 24);
groupBoxSets.Margin = new Padding(3, 2, 3, 2);
groupBoxSets.Name = "groupBoxSets";
groupBoxSets.Padding = new Padding(3, 2, 3, 2);
groupBoxSets.Size = new Size(174, 182);
groupBoxSets.Size = new Size(174, 239);
groupBoxSets.TabIndex = 7;
groupBoxSets.TabStop = false;
groupBoxSets.Text = "Наборы";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(0, 20);
textBoxStorageName.Location = new Point(3, 92);
textBoxStorageName.Margin = new Padding(3, 2, 3, 2);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(169, 23);
textBoxStorageName.Size = new Size(166, 23);
textBoxStorageName.TabIndex = 3;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(5, 155);
buttonDelObject.Location = new Point(4, 213);
buttonDelObject.Margin = new Padding(3, 2, 3, 2);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(164, 22);
@ -95,10 +103,10 @@
//
// buttonnAddObject
//
buttonnAddObject.Location = new Point(4, 53);
buttonnAddObject.Location = new Point(2, 119);
buttonnAddObject.Margin = new Padding(3, 2, 3, 2);
buttonnAddObject.Name = "buttonnAddObject";
buttonnAddObject.Size = new Size(165, 22);
buttonnAddObject.Size = new Size(166, 22);
buttonnAddObject.TabIndex = 1;
buttonnAddObject.Text = "Добавить набор";
buttonnAddObject.UseVisualStyleBackColor = true;
@ -108,19 +116,49 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(5, 88);
listBoxStorages.Location = new Point(4, 145);
listBoxStorages.Margin = new Padding(3, 2, 3, 2);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(164, 64);
listBoxStorages.TabIndex = 0;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// menuStrip1
//
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip1.Location = new Point(3, 18);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(168, 24);
menuStrip1.TabIndex = 4;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранениеToolStripMenuItem, загрузкаToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Text = "Файл";
//
// сохранениеToolStripMenuItem
//
сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem";
сохранениеToolStripMenuItem.Size = new Size(141, 22);
сохранениеToolStripMenuItem.Text = "Сохранение";
сохранениеToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// загрузкаToolStripMenuItem
//
загрузкаToolStripMenuItem.Name = агрузкаToolStripMenuItem";
загрузкаToolStripMenuItem.Size = new Size(141, 22);
загрузкаToolStripMenuItem.Text = "Загрузка";
загрузкаToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(14, 281);
maskedTextBoxNumber.Location = new Point(14, 301);
maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(150, 23);
maskedTextBoxNumber.Size = new Size(164, 23);
maskedTextBoxNumber.TabIndex = 6;
//
// labelCollection
@ -137,7 +175,7 @@
buttonRefreshCollection.Location = new Point(14, 362);
buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(150, 30);
buttonRefreshCollection.Size = new Size(164, 30);
buttonRefreshCollection.TabIndex = 5;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
@ -145,10 +183,10 @@
//
// buttonRemoveTrain
//
buttonRemoveTrain.Location = new Point(14, 318);
buttonRemoveTrain.Location = new Point(14, 328);
buttonRemoveTrain.Margin = new Padding(3, 2, 3, 2);
buttonRemoveTrain.Name = "buttonRemoveTrain";
buttonRemoveTrain.Size = new Size(150, 30);
buttonRemoveTrain.Size = new Size(164, 30);
buttonRemoveTrain.TabIndex = 4;
buttonRemoveTrain.Text = "Удалить машину";
buttonRemoveTrain.UseVisualStyleBackColor = true;
@ -156,10 +194,10 @@
//
// buttonAddTrain
//
buttonAddTrain.Location = new Point(14, 232);
buttonAddTrain.Location = new Point(14, 267);
buttonAddTrain.Margin = new Padding(3, 2, 3, 2);
buttonAddTrain.Name = "buttonAddTrain";
buttonAddTrain.Size = new Size(150, 30);
buttonAddTrain.Size = new Size(164, 30);
buttonAddTrain.TabIndex = 2;
buttonAddTrain.Text = "Добавить машину";
buttonAddTrain.UseVisualStyleBackColor = true;
@ -174,6 +212,15 @@
pictureBoxCollection.TabIndex = 2;
pictureBoxCollection.TabStop = false;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// TrainCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -181,11 +228,14 @@
ClientSize = new Size(956, 400);
Controls.Add(panelCollection);
Controls.Add(pictureBoxCollection);
MainMenuStrip = menuStrip1;
Name = "TrainCollection";
panelCollection.ResumeLayout(false);
panelCollection.PerformLayout();
groupBoxSets.ResumeLayout(false);
groupBoxSets.PerformLayout();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -204,5 +254,11 @@
private Button buttonRemoveTrain;
private Button buttonAddTrain;
private PictureBox pictureBoxCollection;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранениеToolStripMenuItem;
private ToolStripMenuItem загрузкаToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -141,7 +141,38 @@ namespace RoadTrain
}
pictureBoxCollection.Image = obj.ShowTrains();
}
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);
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ReloadObjects();
}
}
}
}

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>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>
</root>

View File

@ -30,14 +30,6 @@
{
pictureBoxObject = new PictureBox();
labelSimpleObject = new Label();
panelRed = new Panel();
panelBlue = new Panel();
panelYellow = new Panel();
panelWhite = new Panel();
panelGray = new Panel();
panelBlack = new Panel();
panelPurple = new Panel();
panelGreen = new Panel();
labelModifiedObject = new Label();
groupBoxColor = new GroupBox();
checkBoxBrush = new CheckBox();
@ -52,6 +44,14 @@
labelBodyColor = new Label();
checkBoxTank = new CheckBox();
groupBoxParameters = new GroupBox();
panelRed = new Panel();
panelBlue = new Panel();
panelYellow = new Panel();
panelWhite = new Panel();
panelGray = new Panel();
panelBlack = new Panel();
panelPurple = new Panel();
panelGreen = new Panel();
((System.ComponentModel.ISupportInitialize)pictureBoxObject).BeginInit();
groupBoxColor.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit();
@ -82,86 +82,6 @@
labelSimpleObject.TextAlign = ContentAlignment.MiddleCenter;
labelSimpleObject.MouseDown += LabelObject_MouseDown;
//
// panelRed
//
panelRed.BackColor = Color.Tomato;
panelRed.Location = new Point(6, 29);
panelRed.Margin = new Padding(3, 2, 3, 2);
panelRed.Name = "panelRed";
panelRed.Size = new Size(40, 40);
panelRed.TabIndex = 1;
panelRed.MouseDown += PanelColor_MouseDown;
//
// panelBlue
//
panelBlue.BackColor = Color.CornflowerBlue;
panelBlue.Location = new Point(125, 29);
panelBlue.Margin = new Padding(3, 2, 3, 2);
panelBlue.Name = "panelBlue";
panelBlue.Size = new Size(40, 40);
panelBlue.TabIndex = 1;
panelBlue.MouseDown += PanelColor_MouseDown;
//
// panelYellow
//
panelYellow.BackColor = Color.FromArgb(255, 255, 128);
panelYellow.Location = new Point(187, 29);
panelYellow.Margin = new Padding(3, 2, 3, 2);
panelYellow.Name = "panelYellow";
panelYellow.Size = new Size(40, 40);
panelYellow.TabIndex = 1;
panelYellow.MouseDown += PanelColor_MouseDown;
//
// panelWhite
//
panelWhite.BackColor = Color.White;
panelWhite.Location = new Point(5, 86);
panelWhite.Margin = new Padding(3, 2, 3, 2);
panelWhite.Name = "panelWhite";
panelWhite.Size = new Size(40, 40);
panelWhite.TabIndex = 1;
panelWhite.MouseDown += PanelColor_MouseDown;
//
// panelGray
//
panelGray.BackColor = Color.Gray;
panelGray.Location = new Point(65, 86);
panelGray.Margin = new Padding(3, 2, 3, 2);
panelGray.Name = "panelGray";
panelGray.Size = new Size(40, 40);
panelGray.TabIndex = 1;
panelGray.MouseDown += PanelColor_MouseDown;
//
// panelBlack
//
panelBlack.BackColor = Color.Black;
panelBlack.Location = new Point(125, 86);
panelBlack.Margin = new Padding(3, 2, 3, 2);
panelBlack.Name = "panelBlack";
panelBlack.Size = new Size(40, 40);
panelBlack.TabIndex = 1;
panelBlack.MouseDown += PanelColor_MouseDown;
//
// panelPurple
//
panelPurple.BackColor = Color.Violet;
panelPurple.Location = new Point(187, 86);
panelPurple.Margin = new Padding(3, 2, 3, 2);
panelPurple.Name = "panelPurple";
panelPurple.Size = new Size(40, 40);
panelPurple.TabIndex = 1;
panelPurple.MouseDown += PanelColor_MouseDown;
//
// panelGreen
//
panelGreen.BackColor = Color.LightGreen;
panelGreen.Location = new Point(65, 29);
panelGreen.Margin = new Padding(3, 2, 3, 2);
panelGreen.Name = "panelGreen";
panelGreen.Size = new Size(40, 40);
panelGreen.TabIndex = 0;
panelGreen.MouseDown += PanelColor_MouseDown;
//
// labelModifiedObject
//
labelModifiedObject.AllowDrop = true;
@ -179,12 +99,12 @@
//
groupBoxColor.Controls.Add(panelRed);
groupBoxColor.Controls.Add(panelBlue);
groupBoxColor.Controls.Add(panelYellow);
groupBoxColor.Controls.Add(panelWhite);
groupBoxColor.Controls.Add(panelGray);
groupBoxColor.Controls.Add(panelBlack);
groupBoxColor.Controls.Add(panelPurple);
groupBoxColor.Controls.Add(panelGreen);
groupBoxColor.Controls.Add(panelYellow);
groupBoxColor.Controls.Add(panelPurple);
groupBoxColor.Controls.Add(panelWhite);
groupBoxColor.Controls.Add(panelBlack);
groupBoxColor.Controls.Add(panelGray);
groupBoxColor.Location = new Point(274, 28);
groupBoxColor.Margin = new Padding(3, 2, 3, 2);
groupBoxColor.Name = "groupBoxColor";
@ -341,6 +261,86 @@
groupBoxParameters.TabStop = false;
groupBoxParameters.Text = "Параметры";
//
// panelRed
//
panelRed.BackColor = Color.Tomato;
panelRed.Location = new Point(6, 20);
panelRed.Margin = new Padding(3, 2, 3, 2);
panelRed.Name = "panelRed";
panelRed.Size = new Size(40, 40);
panelRed.TabIndex = 4;
panelRed.MouseDown += PanelColor_MouseDown;
//
// panelBlue
//
panelBlue.BackColor = Color.CornflowerBlue;
panelBlue.Location = new Point(125, 20);
panelBlue.Margin = new Padding(3, 2, 3, 2);
panelBlue.Name = "panelBlue";
panelBlue.Size = new Size(40, 40);
panelBlue.TabIndex = 5;
panelBlue.MouseDown += PanelColor_MouseDown;
//
// panelYellow
//
panelYellow.BackColor = Color.Yellow;
panelYellow.Location = new Point(187, 20);
panelYellow.Margin = new Padding(3, 2, 3, 2);
panelYellow.Name = "panelYellow";
panelYellow.Size = new Size(40, 40);
panelYellow.TabIndex = 6;
panelYellow.MouseDown += PanelColor_MouseDown;
//
// panelWhite
//
panelWhite.BackColor = Color.White;
panelWhite.Location = new Point(5, 77);
panelWhite.Margin = new Padding(3, 2, 3, 2);
panelWhite.Name = "panelWhite";
panelWhite.Size = new Size(40, 40);
panelWhite.TabIndex = 7;
panelWhite.MouseDown += PanelColor_MouseDown;
//
// panelGray
//
panelGray.BackColor = Color.Gray;
panelGray.Location = new Point(65, 77);
panelGray.Margin = new Padding(3, 2, 3, 2);
panelGray.Name = "panelGray";
panelGray.Size = new Size(40, 40);
panelGray.TabIndex = 8;
panelGray.MouseDown += PanelColor_MouseDown;
//
// panelBlack
//
panelBlack.BackColor = Color.Black;
panelBlack.Location = new Point(125, 77);
panelBlack.Margin = new Padding(3, 2, 3, 2);
panelBlack.Name = "panelBlack";
panelBlack.Size = new Size(40, 40);
panelBlack.TabIndex = 9;
panelBlack.MouseDown += PanelColor_MouseDown;
//
// panelPurple
//
panelPurple.BackColor = Color.Violet;
panelPurple.Location = new Point(187, 77);
panelPurple.Margin = new Padding(3, 2, 3, 2);
panelPurple.Name = "panelPurple";
panelPurple.Size = new Size(40, 40);
panelPurple.TabIndex = 10;
panelPurple.MouseDown += PanelColor_MouseDown;
//
// panelGreen
//
panelGreen.BackColor = Color.LightGreen;
panelGreen.Location = new Point(65, 20);
panelGreen.Margin = new Padding(3, 2, 3, 2);
panelGreen.Name = "panelGreen";
panelGreen.Size = new Size(40, 40);
panelGreen.TabIndex = 3;
panelGreen.MouseDown += PanelColor_MouseDown;
//
// TrainConfig
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -364,14 +364,6 @@
private PictureBox pictureBoxObject;
private Label labelSimpleObject;
private Panel panelRed;
private Panel panelBlue;
private Panel panelYellow;
private Panel panelWhite;
private Panel panelGray;
private Panel panelBlack;
private Panel panelPurple;
private Panel panelGreen;
private Label labelModifiedObject;
private GroupBox groupBoxColor;
private CheckBox checkBoxBrush;
@ -386,5 +378,13 @@
private Label labelBodyColor;
private CheckBox checkBoxTank;
private GroupBox groupBoxParameters;
private Panel panelRed;
private Panel panelBlue;
private Panel panelGreen;
private Panel panelYellow;
private Panel panelPurple;
private Panel panelWhite;
private Panel panelBlack;
private Panel panelGray;
}
}

View File

@ -14,6 +14,7 @@ namespace RoadTrain.Generics
where T : DrawingRoadTrain
where U : IMoveableObject
{
public IEnumerable<T?> GetTrain => _collection.GetTrains();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;

View File

@ -49,5 +49,89 @@ namespace RoadTrain.Generics
return null;
}
}
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>> record in _trainStorages)
{
StringBuilder records = new();
foreach (DrawingRoadTrain? elem in record.Value.GetTrain)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"TrainStorage{Environment.NewLine}{data}");
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader fs = File.OpenText(filename))
{
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!str.StartsWith("TrainStorage"))
{
return false;
}
_trainStorages.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
{
if (strs == null)
{
return false;
}
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingRoadTrain? train = elem?.CreateDrawingTrain(_separatorForObject, _pictureWidth, _pictureHeight);
if (train != null)
{
if (!(collection + train))
{
return false;
}
}
}
_trainStorages.Add(record[0], collection);
}
}
return true;
}
}
}
}