diff --git a/ProjectElectroTrans/CollectionGenericObjects/AbstractCompany.cs b/ProjectElectroTrans/CollectionGenericObjects/AbstractCompany.cs index 68b0455..09d85f0 100644 --- a/ProjectElectroTrans/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectElectroTrans/CollectionGenericObjects/AbstractCompany.cs @@ -48,7 +48,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/ProjectElectroTrans/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectElectroTrans/CollectionGenericObjects/ICollectionGenericObjects.cs index f22a93e..b68ae47 100644 --- a/ProjectElectroTrans/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectElectroTrans/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,6 +1,4 @@ -using ProjectElectroTrans.Drawnings; - -namespace ProjectElectroTrans.CollectionGenericObjects; +namespace ProjectElectroTrans.CollectionGenericObjects; /// /// Интерфейс описания действий для набора хранимых объектов @@ -17,7 +15,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -39,7 +37,7 @@ public interface ICollectionGenericObjects /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - T? Remove(int position); + T Remove(int position); /// /// Получение объекта по позиции @@ -47,4 +45,15 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } \ No newline at end of file diff --git a/ProjectElectroTrans/CollectionGenericObjects/ListGenericObjects.cs b/ProjectElectroTrans/CollectionGenericObjects/ListGenericObjects.cs index e442bab..4d72837 100644 --- a/ProjectElectroTrans/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectElectroTrans/CollectionGenericObjects/ListGenericObjects.cs @@ -11,13 +11,27 @@ public class ListGenericObjects : ICollectionGenericObjects /// Список объектов, которые храним /// private readonly List _collection; + /// /// Максимально допустимое число объектов в списке /// private int _maxCount; + public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount + { + get { return Count; } + set + { + if (value > 0) + { + _maxCount = value; + } + } + } + + public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор @@ -27,43 +41,40 @@ public class ListGenericObjects : ICollectionGenericObjects _collection = new(); } - public T? Get(int position) + public T Get(int position) { - - if (position < 0 || position > _collection.Count - 1) - { - return null; - } + if (position >= Count || position < 0) return null; return _collection[position]; } public int Insert(T obj) { - if (_collection.Count + 1 > _maxCount) { return 0; } + if (Count == _maxCount) return -1; _collection.Add(obj); - - return 1; + return Count; } public int Insert(T obj, int position) { - if (_collection.Count + 1 < _maxCount) { return 0; } - if (position > _collection.Count || position < 0) - { - return 0; - } + if (Count == _maxCount) return -1; + if (position >= Count || position < 0) return -1; _collection.Insert(position, obj); - return 1; + return position; } public T Remove(int position) { - if (position > _collection.Count || position < 0) - { - return null; - } - T temp = _collection[position]; + if (position >= _collection.Count || position < 0) return null; + T obj = _collection[position]; _collection.RemoveAt(position); - return temp; + return obj; + } + + public IEnumerable GetItems() + { + for (int i = 0; i < Count; ++i) + { + yield return _collection[i]; + } } } \ No newline at end of file diff --git a/ProjectElectroTrans/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectElectroTrans/CollectionGenericObjects/MassiveGenericObjects.cs index 2beb914..8bf183d 100644 --- a/ProjectElectroTrans/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectElectroTrans/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,6 +1,4 @@ -using System.Runtime.Remoting; -using ProjectElectroTrans.Drawnings; - + namespace ProjectElectroTrans.CollectionGenericObjects; /// @@ -8,42 +6,48 @@ namespace ProjectElectroTrans.CollectionGenericObjects; /// /// Параметр: ограничение - ссылочный тип public class MassiveGenericObjects : ICollectionGenericObjects - where T : class + where T : class { - /// - /// Массив объектов, которые храним - /// - private T?[] _collection; + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; - public int Count => _collection.Length; + public int Count => _collection.Length; - public int SetMaxCount - { - set - { - if (value > 0) - { - if (_collection.Length > 0) - { - Array.Resize(ref _collection, value); - } - else - { - _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 MassiveGenericObjects() - { - _collection = Array.Empty(); - } + public CollectionType GetCollectionType => CollectionType.Massive; - public T? Get(int position) + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) { if (position >= 0 && position < Count) { @@ -130,4 +134,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[position] = null; return temp; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } \ No newline at end of file diff --git a/ProjectElectroTrans/CollectionGenericObjects/StorageCollection.cs b/ProjectElectroTrans/CollectionGenericObjects/StorageCollection.cs index ef5a1f9..036892c 100644 --- a/ProjectElectroTrans/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectElectroTrans/CollectionGenericObjects/StorageCollection.cs @@ -1,11 +1,15 @@ -namespace ProjectElectroTrans.CollectionGenericObjects; +using ProjectElectroTrans.Drawnings; +using System.Text; +using ProjectElectroTrans.CollectionGenericObjects; + +namespace ProjectElectroTrans.CollectionGenericObjects; /// /// Класс-хранилище коллекций /// /// public class StorageCollection - where T : class + where T : DrawingTrans { /// /// Словарь (хранилище) с коллекциями @@ -17,6 +21,21 @@ public class StorageCollection /// public List Keys => _storages.Keys.ToList(); + /// + /// Ключевое слово, с которого должен начинаться файл + /// + private readonly string _collectionKey = "CollectionsStorage"; + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + /// /// Конструктор /// @@ -32,20 +51,12 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (name == null || _storages.ContainsKey(name)) { return; } - switch (collectionType) - - { - case CollectionType.None: - return; - case CollectionType.Massive: - _storages.Add(name, new MassiveGenericObjects { }); - return; - case CollectionType.List: - _storages.Add(name, new ListGenericObjects { }); - return; - } - + if (_storages.ContainsKey(name)) return; + if (collectionType == CollectionType.None) return; + else if (collectionType == CollectionType.Massive) + _storages[name] = new MassiveGenericObjects(); + else if (collectionType == CollectionType.List) + _storages[name] = new ListGenericObjects(); } /// @@ -54,8 +65,8 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (name == null || !_storages.ContainsKey(name)) { return; } - _storages.Remove(name); + if (_storages.ContainsKey(name)) + _storages.Remove(name); } /// @@ -67,8 +78,141 @@ public class StorageCollection { get { - if (name == null || !_storages.ContainsKey(name)) { return null; } - return _storages[name]; + if (_storages.ContainsKey(name)) + return _storages[name]; + return null; } } + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + + if (File.Exists(filename)) + { + File.Delete(filename); + } + + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) + { + StringBuilder sb = new(); + sb.Append(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) + { + continue; + } + + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + + sb.Append(data); + sb.Append(_separatorItems); + } + + writer.Write(sb); + } + } + + return true; + } + + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + 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(_collectionKey)) + { + return false; + } + + _storages.Clear(); + string strs = ""; + while ((strs = fs.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) + { + continue; + } + + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.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?.CreateDrawningTrans() is T ship) + { + if (collection.Insert(ship) == -1) + { + return false; + } + } + } + + _storages.Add(record[0], collection); + } + + return true; + } + } + + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Massive => new MassiveGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } } \ No newline at end of file diff --git a/ProjectElectroTrans/Drawnings/DrawingElectroTrans.cs b/ProjectElectroTrans/Drawnings/DrawingElectroTrans.cs index 44f206c..611854d 100644 --- a/ProjectElectroTrans/Drawnings/DrawingElectroTrans.cs +++ b/ProjectElectroTrans/Drawnings/DrawingElectroTrans.cs @@ -22,6 +22,15 @@ public class DrawingElectroTrans : DrawingTrans { EntityTrans = new EntityElectroTrans(speed, weight, bodyColor, additionalColor, horns, battery); } + + public DrawingElectroTrans(EntityTrans trans) : base(110, 60) +{ + if (trans != null && trans is EntityElectroTrans electroTrans) + { + EntityTrans = new EntityElectroTrans(electroTrans.Speed, electroTrans.Weight, electroTrans.BodyColor, electroTrans.AdditionalColor, electroTrans.Horns, electroTrans.Battery); + } +} + public override void DrawTransport(Graphics g) { if (EntityTrans == null || EntityTrans is not EntityElectroTrans electroTrans || diff --git a/ProjectElectroTrans/Drawnings/DrawingTrans.cs b/ProjectElectroTrans/Drawnings/DrawingTrans.cs index 85a7ec1..11e443e 100644 --- a/ProjectElectroTrans/Drawnings/DrawingTrans.cs +++ b/ProjectElectroTrans/Drawnings/DrawingTrans.cs @@ -76,6 +76,19 @@ public class DrawingTrans { EntityTrans = new EntityTrans(speed, weight, bodyColor); } + + /// + /// Конструктор + /// + /// Класс-сущность + public DrawingTrans(EntityTrans trans) : this() +{ + if (trans != null) + { + EntityTrans = new EntityTrans(trans.Speed, trans.Weight, trans.BodyColor); + } +} + /// /// Конструктор для наследников /// diff --git a/ProjectElectroTrans/Drawnings/ExtentionDrawningTrans.cs b/ProjectElectroTrans/Drawnings/ExtentionDrawningTrans.cs new file mode 100644 index 0000000..3868d01 --- /dev/null +++ b/ProjectElectroTrans/Drawnings/ExtentionDrawningTrans.cs @@ -0,0 +1,53 @@ +using ProjectElectroTrans.Drawnings; +using ProjectElectroTrans.Entities; + +namespace ProjectElectroTrans; + +public static class ExtentionDrawningTrans +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawingTrans? CreateDrawningTrans(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityTrans? airplan = EntityElectroTrans.CreateEntityElectroTrans(strs); + if (airplan != null) + { + return new DrawingElectroTrans((EntityElectroTrans)airplan); + } + + airplan = EntityTrans.CreateEntityTrans(strs); + if (airplan != null) + { + return new DrawingTrans(airplan); + } + + return null; + } + + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingTrans DrawingTrans) + { + string[]? array = DrawingTrans?.EntityTrans?.GetStringRepresentation(); + + if (array == null) + { + return string.Empty; + } + + return string.Join(_separatorForObject, array); + } + +} \ No newline at end of file diff --git a/ProjectElectroTrans/Entities/EntityElectroTrans.cs b/ProjectElectroTrans/Entities/EntityElectroTrans.cs index 093f73b..0c44185 100644 --- a/ProjectElectroTrans/Entities/EntityElectroTrans.cs +++ b/ProjectElectroTrans/Entities/EntityElectroTrans.cs @@ -48,4 +48,34 @@ internal class EntityElectroTrans : EntityTrans Horns = horns; Battery = battery; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public override string[] GetStringRepresentation() + { + return new[] + { + nameof(EntityElectroTrans), Speed.ToString(), + Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Horns.ToString(), Battery.ToString() + }; + } + + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityTrans? CreateEntityElectroTrans(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityElectroTrans)) + { + return null; + } + + return new EntityElectroTrans(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3]), + Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[5])); + } } \ No newline at end of file diff --git a/ProjectElectroTrans/Entities/EntityTrans.cs b/ProjectElectroTrans/Entities/EntityTrans.cs index ebc1c9d..e345b6e 100644 --- a/ProjectElectroTrans/Entities/EntityTrans.cs +++ b/ProjectElectroTrans/Entities/EntityTrans.cs @@ -43,5 +43,30 @@ public class EntityTrans Weight = weight; BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityTrans), Speed.ToString(), + Weight.ToString(), BodyColor.Name }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityTrans? CreateEntityTrans(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityTrans)) + { + return null; + } + return new EntityTrans(Convert.ToInt32(strs[1]), + Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } + } diff --git a/ProjectElectroTrans/FormTransCollection.Designer.cs b/ProjectElectroTrans/FormTransCollection.Designer.cs index a605cbd..a13535f 100644 --- a/ProjectElectroTrans/FormTransCollection.Designer.cs +++ b/ProjectElectroTrans/FormTransCollection.Designer.cs @@ -24,268 +24,327 @@ namespace ProjectElectroTrans base.Dispose(disposing); } - #region Windows Form Designer generated code + #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - groupBoxTools = new GroupBox(); - buttonCreateCompany = new Button(); - panelStorage = new Panel(); - buttonCollectionDel = new Button(); - listBoxCollection = new ListBox(); - buttonCollectionAdd = new Button(); - radioButtonList = new RadioButton(); - radioButtonMassive = new RadioButton(); - textBoxCollectionName = new TextBox(); - labelCollectionName = new Label(); - buttonRefresh = new Button(); - buttonGoToCheck = new Button(); - buttonRemoveTrans = new Button(); - maskedTextBoxPosition = new MaskedTextBox(); - buttonAddTrans = new Button(); - comboBoxSelectorCompany = new ComboBox(); - pictureBox = new PictureBox(); - panelCompanyTools = new Panel(); - groupBoxTools.SuspendLayout(); - panelStorage.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); - panelCompanyTools.SuspendLayout(); - SuspendLayout(); - // - // groupBoxTools - // - groupBoxTools.Controls.Add(panelCompanyTools); - groupBoxTools.Controls.Add(buttonCreateCompany); - groupBoxTools.Controls.Add(panelStorage); - groupBoxTools.Controls.Add(comboBoxSelectorCompany); - groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(783, 0); - groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(179, 616); - groupBoxTools.TabIndex = 0; - groupBoxTools.TabStop = false; - groupBoxTools.Text = "Инструменты"; - // - // buttonCreateCompany - // - buttonCreateCompany.Location = new Point(6, 320); - buttonCreateCompany.Name = "buttonCreateCompany"; - buttonCreateCompany.Size = new Size(167, 23); - buttonCreateCompany.TabIndex = 8; - buttonCreateCompany.Text = "Создать компанию"; - buttonCreateCompany.UseVisualStyleBackColor = true; - buttonCreateCompany.Click += ButtonCreateCompany_Click; - // - // panelStorage - // - panelStorage.Controls.Add(buttonCollectionDel); - panelStorage.Controls.Add(listBoxCollection); - panelStorage.Controls.Add(buttonCollectionAdd); - panelStorage.Controls.Add(radioButtonList); - panelStorage.Controls.Add(radioButtonMassive); - panelStorage.Controls.Add(textBoxCollectionName); - panelStorage.Controls.Add(labelCollectionName); - panelStorage.Dock = DockStyle.Top; - panelStorage.Location = new Point(3, 19); - panelStorage.Name = "panelStorage"; - panelStorage.Size = new Size(173, 266); - panelStorage.TabIndex = 7; - // - // buttonCollectionDel - // - buttonCollectionDel.Location = new Point(3, 227); - buttonCollectionDel.Name = "buttonCollectionDel"; - buttonCollectionDel.Size = new Size(167, 23); - buttonCollectionDel.TabIndex = 6; - buttonCollectionDel.Text = "Удалить коллекцию"; - buttonCollectionDel.UseVisualStyleBackColor = true; - buttonCollectionDel.Click += ButtonCollectionDel_Click; - // - // listBoxCollection - // - listBoxCollection.FormattingEnabled = true; - listBoxCollection.ItemHeight = 15; - listBoxCollection.Location = new Point(3, 112); - listBoxCollection.Name = "listBoxCollection"; - listBoxCollection.Size = new Size(167, 109); - listBoxCollection.TabIndex = 5; - // - // buttonCollectionAdd - // - buttonCollectionAdd.Location = new Point(3, 83); - buttonCollectionAdd.Name = "buttonCollectionAdd"; - buttonCollectionAdd.Size = new Size(167, 23); - buttonCollectionAdd.TabIndex = 4; - buttonCollectionAdd.Text = "Добавить коллекцию"; - buttonCollectionAdd.UseVisualStyleBackColor = true; - buttonCollectionAdd.Click += ButtonCollectionAdd_Click; - // - // radioButtonList - // - radioButtonList.AutoSize = true; - radioButtonList.Location = new Point(98, 58); - radioButtonList.Name = "radioButtonList"; - radioButtonList.Size = new Size(66, 19); - radioButtonList.TabIndex = 3; - radioButtonList.TabStop = true; - radioButtonList.Text = "Список"; - radioButtonList.UseVisualStyleBackColor = true; - // - // radioButtonMassive - // - radioButtonMassive.AutoSize = true; - radioButtonMassive.Location = new Point(16, 58); - radioButtonMassive.Name = "radioButtonMassive"; - radioButtonMassive.Size = new Size(67, 19); - radioButtonMassive.TabIndex = 2; - radioButtonMassive.TabStop = true; - radioButtonMassive.Text = "Массив"; - radioButtonMassive.UseVisualStyleBackColor = true; - // - // textBoxCollectionName - // - textBoxCollectionName.Location = new Point(3, 29); - textBoxCollectionName.Name = "textBoxCollectionName"; - textBoxCollectionName.Size = new Size(167, 23); - textBoxCollectionName.TabIndex = 1; - // - // labelCollectionName - // - labelCollectionName.AutoSize = true; - labelCollectionName.Location = new Point(26, 11); - labelCollectionName.Name = "labelCollectionName"; - labelCollectionName.Size = new Size(125, 15); - labelCollectionName.TabIndex = 0; - labelCollectionName.Text = "Название коллекции:"; - // - // buttonRefresh - // - buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(3, 210); - buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(167, 40); - buttonRefresh.TabIndex = 6; - buttonRefresh.Text = "Обновить"; - buttonRefresh.UseVisualStyleBackColor = true; - buttonRefresh.Click += ButtonRefresh_Click; - // - // buttonGoToCheck - // - buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(3, 170); - buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(167, 40); - buttonGoToCheck.TabIndex = 5; - buttonGoToCheck.Text = "Передать на тесты"; - buttonGoToCheck.UseVisualStyleBackColor = true; - buttonGoToCheck.Click += ButtonGoToCheck_Click; - // - // buttonRemoveTrans - // - buttonRemoveTrans.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveTrans.Location = new Point(3, 124); - buttonRemoveTrans.Name = "buttonRemoveTrans"; - buttonRemoveTrans.Size = new Size(167, 40); - buttonRemoveTrans.TabIndex = 4; - buttonRemoveTrans.Text = "Удалить поезд"; - buttonRemoveTrans.UseVisualStyleBackColor = true; - buttonRemoveTrans.Click += ButtonRemoveTrans_Click; - // - // maskedTextBoxPosition - // - maskedTextBoxPosition.Location = new Point(3, 95); - maskedTextBoxPosition.Mask = "00"; - maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(167, 23); - maskedTextBoxPosition.TabIndex = 3; - maskedTextBoxPosition.ValidatingType = typeof(int); - // buttonAddTrans - // - buttonAddTrans.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTrans.Location = new Point(3, 3); - buttonAddTrans.Name = "buttonAddTrans"; - buttonAddTrans.Size = new Size(167, 40); - buttonAddTrans.TabIndex = 1; - buttonAddTrans.Text = "Добавление поезд"; - buttonAddTrans.UseVisualStyleBackColor = true; - buttonAddTrans.Click += ButtonAddTrans_Click; - // - // comboBoxSelectorCompany - // - comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxSelectorCompany.FormattingEnabled = true; - comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 291); - comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(167, 23); - comboBoxSelectorCompany.TabIndex = 0; - comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; - // - // pictureBox - // - pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); - pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(783, 616); - pictureBox.TabIndex = 1; - pictureBox.TabStop = false; - // - // panelCompanyTools - // - panelCompanyTools.Controls.Add(buttonAddTrans); - panelCompanyTools.Controls.Add(maskedTextBoxPosition); - panelCompanyTools.Controls.Add(buttonRefresh); - panelCompanyTools.Controls.Add(buttonRemoveTrans); - panelCompanyTools.Controls.Add(buttonGoToCheck); - panelCompanyTools.Dock = DockStyle.Bottom; - panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 360); - panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(173, 253); - panelCompanyTools.TabIndex = 9; - // - // FormTransCollection - // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(962, 616); - Controls.Add(pictureBox); - Controls.Add(groupBoxTools); - Name = "FormTransCollection"; - Text = "Коллекция поездов"; - groupBoxTools.ResumeLayout(false); - panelStorage.ResumeLayout(false); - panelStorage.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); - panelCompanyTools.ResumeLayout(false); - panelCompanyTools.PerformLayout(); - ResumeLayout(false); - } + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); + buttonAddCar = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonRefresh = new Button(); + buttonRemoveCar = new Button(); + buttonGoToCheck = new Button(); + buttonCreateCompany = new Button(); + panelStorage = new Panel(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); + comboBoxSelectorCompany = new ComboBox(); + 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 + // + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(783, 24); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(179, 608); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddCar); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonRemoveCar); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 352); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(173, 253); + panelCompanyTools.TabIndex = 9; + // + // buttonAddCar + // + buttonAddCar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddCar.Location = new Point(3, 3); + buttonAddCar.Name = "buttonAddCar"; + buttonAddCar.Size = new Size(167, 40); + buttonAddCar.TabIndex = 1; + buttonAddCar.Text = "Добавление автомобиля"; + buttonAddCar.UseVisualStyleBackColor = true; + buttonAddCar.Click += ButtonAddTrans_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(3, 95); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(167, 23); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(3, 210); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(167, 40); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonRemoveCar + // + buttonRemoveCar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveCar.Location = new Point(3, 124); + buttonRemoveCar.Name = "buttonRemoveCar"; + buttonRemoveCar.Size = new Size(167, 40); + buttonRemoveCar.TabIndex = 4; + buttonRemoveCar.Text = "Удалить автомобиль"; + buttonRemoveCar.UseVisualStyleBackColor = true; + buttonRemoveCar.Click += ButtonRemoveTrans_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(3, 170); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(167, 40); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 320); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(167, 23); + buttonCreateCompany.TabIndex = 8; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += ButtonCreateCompany_Click; + // + // panelStorage + // + panelStorage.Controls.Add(buttonCollectionDel); + panelStorage.Controls.Add(listBoxCollection); + panelStorage.Controls.Add(buttonCollectionAdd); + panelStorage.Controls.Add(radioButtonList); + panelStorage.Controls.Add(radioButtonMassive); + panelStorage.Controls.Add(textBoxCollectionName); + panelStorage.Controls.Add(labelCollectionName); + panelStorage.Dock = DockStyle.Top; + panelStorage.Location = new Point(3, 19); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(173, 266); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 227); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(167, 23); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 15; + listBoxCollection.Location = new Point(3, 112); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(167, 109); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 83); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(167, 23); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(98, 58); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(66, 19); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(16, 58); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(67, 19); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 29); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(167, 23); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(26, 11); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(125, 15); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 291); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(167, 23); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 24); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(783, 608); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(962, 24); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(48, 20); + файлToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(181, 22); + saveToolStripMenuItem.Text = "Сохранение"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(181, 22); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // + // FormCarCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(962, 632); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Name = "FormCarCollection"; + Text = "Коллекция автомобилей"; + groupBoxTools.ResumeLayout(false); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } - #endregion + #endregion - private GroupBox groupBoxTools; - private ComboBox comboBoxSelectorCompany; - private Button buttonAddTrans; - private Button buttonRemoveTrans; - private MaskedTextBox maskedTextBoxPosition; - private PictureBox pictureBox; - private Button buttonGoToCheck; - private Button buttonRefresh; - private Panel panelStorage; - private Label labelCollectionName; - private TextBox textBoxCollectionName; - private RadioButton radioButtonList; - private RadioButton radioButtonMassive; - private Button buttonCollectionAdd; - private ListBox listBoxCollection; - private Button buttonCollectionDel; - private Button buttonCreateCompany; - private Panel panelCompanyTools; + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddCar; + private Button buttonRemoveCar; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private Button buttonGoToCheck; + private Button buttonRefresh; + private Panel panelStorage; + private Label labelCollectionName; + private TextBox textBoxCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private Button buttonCollectionAdd; + private ListBox listBoxCollection; + 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; } } \ No newline at end of file diff --git a/ProjectElectroTrans/FormTransCollection.cs b/ProjectElectroTrans/FormTransCollection.cs index e70734e..429c2df 100644 --- a/ProjectElectroTrans/FormTransCollection.cs +++ b/ProjectElectroTrans/FormTransCollection.cs @@ -62,7 +62,7 @@ public partial class FormTransCollection : Form { return; } - + if (_company + drawingTrans != -1) { MessageBox.Show("Объект добавлен"); @@ -253,4 +253,49 @@ public partial class FormTransCollection : Form panelCompanyTools.Enabled = true; RerfreshListBoxItems(); } + + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + 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); + RerfreshListBoxItems(); + } + else + { + MessageBox.Show("Не загружено", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } \ No newline at end of file