LabWork06

This commit is contained in:
kirkorovka 2024-05-05 20:07:29 +04:00
parent 8ec22ced57
commit c518fb92c9
13 changed files with 366 additions and 16 deletions

View File

@ -26,7 +26,7 @@ public abstract class AbstractCompany
_pictureWidth = picWidth;
_pictureHeight = picHeigth;
_collection = collection;
_collection.SetMaxCount = GetMaxCount;
_collection.MaxCount = GetMaxCount;
}
public static int operator +(AbstractCompany company, DrawingBase sau)

View File

@ -10,9 +10,12 @@ public interface ICollectionGenericObjects<T>
where T : class
{
int Count { get; }
int SetMaxCount { set; }
int MaxCount { get; set; }
int Insert(T obj);
int Insert(T obj, int position);
T? Remove(int position);
T? Get(int position);
CollectionType GetCollectionType { get; }
IEnumerable<T?> GetItems();
}

View File

@ -11,12 +11,22 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
private readonly List<T?> _collection;
private int _maxCount;
public int MaxCount => _maxCount;
public int MaxCount
{
get => _maxCount;
set
{
if (value > 0)
{
_maxCount = value;
}
}
}
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
public CollectionType GetCollectionType => CollectionType.List;
public ListGenericObjects()
{
@ -51,4 +61,12 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
_collection[position] = null;
return obj;
}
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Count; ++i)
{
yield return _collection[i];
}
}
}

View File

@ -12,9 +12,30 @@ public class MassiveGenericObjects <T> : ICollectionGenericObjects<T>
{
private T?[] _collection;
public int Count => _collection.Length;
public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } }
public int MaxCount
{
get
{
return _collection.Length;
}
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
public CollectionType GetCollectionType => CollectionType.Massive;
public MassiveGenericObjects()
{
_collection = Array.Empty<T?>();
@ -94,4 +115,12 @@ public class MassiveGenericObjects <T> : ICollectionGenericObjects<T>
_collection[position] = null;
return temp;
}
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Length; ++i)
{
yield return _collection[i];
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using Project_SelfPropelledArtilleryUnit.Drawings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -7,12 +8,18 @@ using System.Threading.Tasks;
namespace Project_SelfPropelledArtilleryUnit.CollectionGenericObjects;
internal class StorageCollection<T>
where T : class
where T : DrawingBase
{
private Dictionary<string, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList();
private readonly string _collectionKey = "CollectionsStorage";
private readonly string _separatorForKeyValue = "|";
private readonly string _separatorItems = ";";
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
@ -48,4 +55,109 @@ internal class StorageCollection<T>
}
}
public bool SaveData(string filename)
{
if (_storages.Count == 0)
{
return false;
}
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter writer = new(filename))
{
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции
if (value.Value.Count == 0)
{
continue;
}
writer.Write(value.Key);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.GetCollectionType);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.MaxCount);
writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
writer.Write(data);
writer.Write(_separatorItems);
}
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader reader = new(filename))
{
string line = reader.ReadLine();
if (line == null || line.Length == 0)
{
return false;
}
if (!line.Equals(_collectionKey))
{
return false;
}
_storages.Clear();
while ((line = reader.ReadLine()) != null)
{
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
return false;
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?.CreateDrawningBase() is T truck)
{
if (collection.Insert(truck) == -1)
{
return false;
}
}
}
_storages.Add(record[0], collection);
}
}
return true;
}
private static ICollectionGenericObjects<T>?
CreateCollection(CollectionType collectionType)
{
return collectionType switch
{
CollectionType.Massive => new MassiveGenericObjects<T>(),
CollectionType.List => new ListGenericObjects<T>(),
_ => null,
};
}
}

View File

@ -46,6 +46,14 @@ namespace Project_SelfPropelledArtilleryUnit.Drawings
_drawingSAUHeight = height;
}
public DrawingBase(BaseSAU? baseSAU)
{
if (baseSAU != null)
{
BaseSAU = baseSAU;
}
}
public bool SetPictureSize(int width, int height)
{
if (width > _pictureWidth || height > _pictureHeight)

View File

@ -17,6 +17,13 @@ public class DrawingSAU : DrawingBase
BaseSAU = new EntitySAU( speed, weight, bodyColor, additionalColor, guns);
}
public DrawingSAU(BaseSAU? baseSAU)
{
if (baseSAU != null)
{
BaseSAU = baseSAU;
}
}
public override void DrawSAU(Graphics g)
{

View File

@ -0,0 +1,45 @@
using Project_SelfPropelledArtilleryUnit.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project_SelfPropelledArtilleryUnit.Drawings;
public static class ExtentionDrawing
{
private static readonly string _separatorForObject = ":";
public static DrawingBase? CreateDrawningBase(this string info)
{
string[] strs = info.Split(_separatorForObject);
BaseSAU? sau = EntitySAU.CreateEntitySAU(strs);
if (sau != null)
{
return new DrawingSAU(sau);
}
sau = BaseSAU.CreateBaseSAU(strs);
if (sau != null)
{
return new DrawingBase(sau);
}
return null;
}
public static string GetDataForSave(this DrawingBase drawingBase)
{
string[]? array = drawingBase?.BaseSAU?.GetStringRepresentation();
if (array == null)
{
return string.Empty;
}
return string.Join(_separatorForObject, array);
}
}

View File

@ -26,5 +26,20 @@ namespace Project_SelfPropelledArtilleryUnit.Entity
{
BodyColor = bodyColor;
}
public virtual string[] GetStringRepresentation()
{
return new[] { nameof(BaseSAU), Speed.ToString(), Weight.ToString(), BodyColor.Name };
}
public static BaseSAU? CreateBaseSAU(string[] strs)
{
if (strs.Length != 4 || strs[0] != nameof(BaseSAU))
{
return null;
}
return new BaseSAU(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]));
}
}
}

View File

@ -26,4 +26,19 @@ public class EntitySAU: BaseSAU
AdditionalColor = additionalColor;
}
public override string[] GetStringRepresentation()
{
return new[] { nameof(EntitySAU), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Guns.ToString() };
}
public static EntitySAU? CreateEntitySAU(string[] strs)
{
if (strs.Length != 6 || strs[0] != nameof(EntitySAU))
{
return null;
}
return new EntitySAU(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]),
Convert.ToBoolean(strs[5]));
}
}

View File

@ -46,10 +46,17 @@
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
pictureBox = new PictureBox();
menuStrip = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
saveToolStripMenuItem = new ToolStripMenuItem();
loadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
groupBoxTools.SuspendLayout();
panelCompanyTools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
@ -57,9 +64,9 @@
groupBoxTools.Controls.Add(panelCompanyTools);
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(1123, 0);
groupBoxTools.Location = new Point(1123, 28);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(218, 636);
groupBoxTools.Size = new Size(218, 639);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
@ -73,7 +80,7 @@
panelCompanyTools.Controls.Add(buttonRemoveSAU);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 364);
panelCompanyTools.Location = new Point(3, 367);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(212, 269);
panelCompanyTools.TabIndex = 10;
@ -234,19 +241,62 @@
// pictureBox
//
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Location = new Point(0, 28);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(1123, 636);
pictureBox.Size = new Size(1123, 639);
pictureBox.TabIndex = 4;
pictureBox.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(1341, 28);
menuStrip.TabIndex = 5;
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.ShortcutKeys = Keys.Control | Keys.S;
saveToolStripMenuItem.Size = new Size(227, 26);
saveToolStripMenuItem.Text = "Сохранение";
saveToolStripMenuItem.Click += saveToolStripMenuItem_Click;
//
// loadToolStripMenuItem
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
loadToolStripMenuItem.Size = new Size(227, 26);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += loadToolStripMenuItem_Click;
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// openFileDialog
//
openFileDialog.Filter = "txt file | *.txt";
//
// FormSAUCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1341, 636);
ClientSize = new Size(1341, 667);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Name = "FormSAUCollection";
Text = "FormSAUCollection";
groupBoxTools.ResumeLayout(false);
@ -255,7 +305,10 @@
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -278,5 +331,11 @@
private Button buttonCollectionDel;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private ToolStripMenuItem loadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
}
}

View File

@ -30,7 +30,7 @@ public partial class FormSAUCollection : Form
panelCompanyTools.Enabled = false;
}
private void buttonAddSAU_Click(object sender, EventArgs e)
@ -207,4 +207,34 @@ public partial class FormSAUCollection : Form
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storageCollection.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 (_storageCollection.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно", "Реузльтат", MessageBoxButtons.OK, MessageBoxIcon.Information);
UpdateListBoxItems();
}
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="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>145, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>310, 17</value>
</metadata>
</root>