Lab-6 done

This commit is contained in:
strwbrry1 2024-04-20 00:22:42 +04:00
parent ebc2c222c6
commit def409392f
16 changed files with 387 additions and 13 deletions

View File

@ -27,7 +27,7 @@ namespace Catamaran.CollectionGenericObjects
_pictureHeight = picHeight; _pictureHeight = picHeight;
_pictureWidth = picWidth; _pictureWidth = picWidth;
_collection = collection; _collection = collection;
_collection.SetMaxCount = GetMaxCount; _collection.MaxCount = GetMaxCount;
} }
public static int operator +(AbstractCompany company, DrawingBoat boat) public static int operator +(AbstractCompany company, DrawingBoat boat)

View File

@ -13,8 +13,13 @@ namespace Catamaran.CollectionGenericObjects
public int Count => _collection.Length; public int Count => _collection.Length;
public int SetMaxCount public int MaxCount
{ {
get
{
return _collection.Length;
}
set set
{ {
if (value > 0) if (value > 0)
@ -31,6 +36,8 @@ namespace Catamaran.CollectionGenericObjects
} }
} }
public CollectionType GetCollectionType => CollectionType.Array;
public ArrayGenericObjects() public ArrayGenericObjects()
{ {
_collection = Array.Empty<T?>(); _collection = Array.Empty<T?>();
@ -94,5 +101,12 @@ namespace Catamaran.CollectionGenericObjects
return obj; return obj;
} }
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Length; i++)
{
yield return _collection[i];
}
}
} }
} }

View File

@ -11,7 +11,7 @@ namespace Catamaran.CollectionGenericObjects
{ {
int Count { get; } int Count { get; }
int SetMaxCount { set; } int MaxCount { get; set; }
int Insert(T obj); int Insert(T obj);
@ -20,5 +20,9 @@ namespace Catamaran.CollectionGenericObjects
T? Remove(int position); T? Remove(int position);
T? Get(int position); T? Get(int position);
CollectionType GetCollectionType { get; }
IEnumerable<T?> GetItems();
} }
} }

View File

@ -16,7 +16,24 @@ namespace Catamaran.CollectionGenericObjects
public int Count => _collection.Count; public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } public int MaxCount
{
get
{
if (_maxCount < _collection.Count) return _maxCount;
return _collection.Count;
}
set
{
if (value > 0)
{
_maxCount = value;
}
}
}
public CollectionType GetCollectionType => CollectionType.List;
public ListGenericObjects() public ListGenericObjects()
{ {
@ -62,5 +79,13 @@ namespace Catamaran.CollectionGenericObjects
_collection.RemoveAt(position); _collection.RemoveAt(position);
return obj; return obj;
} }
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Count; i++)
{
yield return _collection[i];
}
}
} }
} }

View File

@ -1,6 +1,7 @@
using Catamaran.Drawings; using Catamaran.Drawings;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,12 +9,18 @@ using System.Threading.Tasks;
namespace Catamaran.CollectionGenericObjects namespace Catamaran.CollectionGenericObjects
{ {
public class StorageCollection<T> public class StorageCollection<T>
where T : class where T : DrawingBoat
{ {
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages; readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
public List<string> Keys => _storages.Keys.ToList(); public List<string> Keys => _storages.Keys.ToList();
private readonly string _collectionKey = "CollectionsStorage";
private readonly string _separatorKeyValue = "|";
private readonly string _separatorItems = ";";
public StorageCollection() public StorageCollection()
{ {
_storages = new Dictionary<string, ICollectionGenericObjects<T>>(); _storages = new Dictionary<string, ICollectionGenericObjects<T>>();
@ -58,5 +65,136 @@ namespace Catamaran.CollectionGenericObjects
return _storages[name]; return _storages[name];
} }
} }
public bool SaveData(string filename)
{
if (_storages.Count == 0)
{
return false;
}
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder sb = new();
sb.Append(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
sb.Append(Environment.NewLine);
if (value.Value.Count == 0)
{
continue;
}
sb.Append(value.Key);
sb.Append(_separatorKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
sb.Append(data);
sb.Append(_separatorItems);
}
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
fs.Close();
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
{
bufferTextFromFile += temp.GetString(b);
}
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].Equals(_collectionKey))
{
return false;
}
//_storages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorKeyValue, 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?.CreateDrawingBoat() is T boat)
{
if (collection.Insert(boat) < 0)
{
return false;
}
}
}
if (!_storages.ContainsKey(record[0]))
{
_storages.Add(record[0], collection);
}
else
{
return false;
}
}
return true;
}
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
{
return collectionType switch
{
CollectionType.Array => new ArrayGenericObjects<T>(),
CollectionType.List => new ListGenericObjects<T>(),
_ => null,
};
}
} }
} }

View File

@ -47,6 +47,12 @@ namespace Catamaran.Drawings
} }
public DrawingBoat(EntityBoat? entityBoat) : this()
{
if (entityBoat == null) return;
EntityBoat = new EntityBoat(entityBoat.Speed, entityBoat.Weight, entityBoat.BodyColor);
}
protected DrawingBoat(int drawingCatamaranWidth, int drawingCatamaranHeight) : this() protected DrawingBoat(int drawingCatamaranWidth, int drawingCatamaranHeight) : this()
{ {
_drawingCatamaranWidth = drawingCatamaranWidth; _drawingCatamaranWidth = drawingCatamaranWidth;

View File

@ -17,6 +17,12 @@ namespace Catamaran.Drawings
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, leftBobber, rightBobber, sail); EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, leftBobber, rightBobber, sail);
} }
public DrawingCatamaran(EntityCatamaran? entityCatamaran) : base(120, 90)
{
if (entityCatamaran == null) return;
EntityBoat = new EntityCatamaran(entityCatamaran.Speed, entityCatamaran.Weight, entityCatamaran.BodyColor,
entityCatamaran.AdditionalColor, entityCatamaran.LeftBobber, entityCatamaran.RightBobber, entityCatamaran.Sail);
}
public override void DrawTransport(Graphics g) public override void DrawTransport(Graphics g)
{ {

View File

@ -0,0 +1,42 @@
using Catamaran.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.Drawings
{
public static class ExtentionDrawingBoat
{
private static readonly string _separator = ":";
public static DrawingBoat? CreateDrawingBoat(this string info)
{
string[] strs = info.Split(_separator);
EntityBoat? boat = EntityCatamaran.CreateEntityCatamaran(strs);
if (boat != null)
{
return new DrawingCatamaran((EntityCatamaran)boat);
}
boat = EntityBoat.CreateEntityBoat(strs);
if (boat != null)
{
return new DrawingBoat(boat);
}
return null;
}
public static string GetDataForSave(this DrawingBoat drawingBoat)
{
string[]? array = drawingBoat?.EntityBoat?.GetStringRepresentation();
if (array == null)
{
return string.Empty;
}
return string.Join(_separator, array);
}
}
}

View File

@ -24,6 +24,21 @@ namespace Catamaran.Entities
AdditionalColor = addColor; AdditionalColor = addColor;
} }
public override string[] GetStringRepresentation()
{
return new[] { nameof(EntityCatamaran), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, LeftBobber.ToString(), RightBobber.ToString(), Sail.ToString() };
}
public static EntityCatamaran? CreateEntityCatamaran(string[] strs)
{
if (strs.Length != 8 || strs[0] != nameof(EntityCatamaran))
{
return null;
}
return new EntityCatamaran(Convert.ToInt32(strs[1]),
Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), Convert.ToBoolean(strs[7]));
}
public EntityCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail) : base(speed, weight, bodyColor) public EntityCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail) : base(speed, weight, bodyColor)
{ {
AdditionalColor = additionalColor; AdditionalColor = additionalColor;

View File

@ -21,6 +21,22 @@ namespace Catamaran.Entities
BodyColor = color; BodyColor = color;
} }
public virtual string[] GetStringRepresentation()
{
return new[] { nameof(EntityBoat), Speed.ToString(), Weight.ToString(), BodyColor.Name };
}
public static EntityBoat? CreateEntityBoat(string[] strs)
{
if (strs.Length != 4 || strs[0] != nameof(EntityBoat))
{
return null;
}
return new EntityBoat(Convert.ToInt32(strs[1]),
Convert.ToDouble(strs[2]), Color.FromName(strs[3]));
}
public EntityBoat(int speed, double weight, Color bodyColor) public EntityBoat(int speed, double weight, Color bodyColor)
{ {
Speed = speed; Speed = speed;

View File

@ -46,10 +46,17 @@
labelCollectionName = new Label(); labelCollectionName = new Label();
comboBoxSelectorCompany = new ComboBox(); comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox(); pictureBox = new PictureBox();
menuStrip1 = new MenuStrip();
FileToolStripMenuItem = new ToolStripMenuItem();
SaveToolStripMenuItem = new ToolStripMenuItem();
LoadToolStripMenuItem = new ToolStripMenuItem();
saveFileDialog = new SaveFileDialog();
openFileDialog = new OpenFileDialog();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
panelCompanyTools.SuspendLayout(); panelCompanyTools.SuspendLayout();
panelCollection.SuspendLayout(); panelCollection.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
menuStrip1.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// groupBox1 // groupBox1
@ -59,9 +66,9 @@
groupBox1.Controls.Add(panelCollection); groupBox1.Controls.Add(panelCollection);
groupBox1.Controls.Add(comboBoxSelectorCompany); groupBox1.Controls.Add(comboBoxSelectorCompany);
groupBox1.Dock = DockStyle.Right; groupBox1.Dock = DockStyle.Right;
groupBox1.Location = new Point(809, 0); groupBox1.Location = new Point(814, 28);
groupBox1.Name = "groupBox1"; groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(237, 642); groupBox1.Size = new Size(237, 646);
groupBox1.TabIndex = 0; groupBox1.TabIndex = 0;
groupBox1.TabStop = false; groupBox1.TabStop = false;
groupBox1.Text = "Инструменты"; groupBox1.Text = "Инструменты";
@ -75,7 +82,7 @@
panelCompanyTools.Controls.Add(DeleteButton); panelCompanyTools.Controls.Add(DeleteButton);
panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false; panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 398); panelCompanyTools.Location = new Point(3, 402);
panelCompanyTools.Name = "panelCompanyTools"; panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(231, 241); panelCompanyTools.Size = new Size(231, 241);
panelCompanyTools.TabIndex = 9; panelCompanyTools.TabIndex = 9;
@ -235,19 +242,63 @@
// pictureBox // pictureBox
// //
pictureBox.Dock = DockStyle.Fill; pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0); pictureBox.Location = new Point(0, 28);
pictureBox.Name = "pictureBox"; pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(809, 642); pictureBox.Size = new Size(814, 646);
pictureBox.TabIndex = 1; pictureBox.TabIndex = 1;
pictureBox.TabStop = false; pictureBox.TabStop = false;
// //
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1051, 28);
menuStrip1.TabIndex = 2;
menuStrip1.Text = "menuStrip1";
//
// FileToolStripMenuItem
//
FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
FileToolStripMenuItem.Name = "FileToolStripMenuItem";
FileToolStripMenuItem.Size = new Size(59, 24);
FileToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S;
SaveToolStripMenuItem.Size = new Size(216, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
LoadToolStripMenuItem.Size = new Size(216, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog1";
openFileDialog.Filter = "txt file | *.txt";
//
// FormBoatColletion // FormBoatColletion
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1046, 642); ClientSize = new Size(1051, 674);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Controls.Add(groupBox1); Controls.Add(groupBox1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormBoatColletion"; Name = "FormBoatColletion";
Text = "Коллекция лодок"; Text = "Коллекция лодок";
groupBox1.ResumeLayout(false); groupBox1.ResumeLayout(false);
@ -256,7 +307,10 @@
panelCollection.ResumeLayout(false); panelCollection.ResumeLayout(false);
panelCollection.PerformLayout(); panelCollection.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -279,5 +333,11 @@
private ListBox listBoxCollection; private ListBox listBoxCollection;
private Button buttonCollectionAdd; private Button buttonCollectionAdd;
private Panel panelCompanyTools; private Panel panelCompanyTools;
private MenuStrip menuStrip1;
private ToolStripMenuItem FileToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
} }
} }

View File

@ -30,7 +30,7 @@ namespace Catamaran
panelCompanyTools.Enabled = false; panelCompanyTools.Enabled = false;
} }
private void AddBoatButton_Click(object sender, EventArgs e) private void AddBoatButton_Click(object sender, EventArgs e)
{ {
@ -211,6 +211,37 @@ namespace Catamaran
RefreshListBoxItems(); RefreshListBoxItems();
} }
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);
RefreshListBoxItems();
}
else
{
MessageBox.Show("Ошибка загрузки", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
} }
} }

View File

@ -117,4 +117,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </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="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>153, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>318, 17</value>
</metadata>
</root> </root>

2
save01.txt Normal file
View File

@ -0,0 +1,2 @@
CollectionsStorage
фф|Array|12|EntityBoat:100:100:Magenta;EntityCatamaran:100:100:Yellow:Black:True:True:False;

2
save02.txt Normal file
View File

@ -0,0 +1,2 @@
CollectionsStorage
asd|Array|16|EntityBoat:100:100:Blue;EntityCatamaran:100:100:Green:Blue:True:True:True;EntityCatamaran:100:100:White:Magenta:False:False:True;

4
sos.txt Normal file
View File

@ -0,0 +1,4 @@
CollectionsStorage
asd|Array|16|EntityBoat:100:100:Gray;
asdpenis|List|1|EntityCatamaran:100:100:Blue:Black:True:False:True;
adddd|Array|16|EntityBoat:100:100:Red;EntityCatamaran:100:100:Magenta:Blue:True:True:False;