diff --git a/lab1/FormLocomotivCollection.Designer.cs b/lab1/FormLocomotivCollection.Designer.cs index a4898d1..6487b8b 100644 --- a/lab1/FormLocomotivCollection.Designer.cs +++ b/lab1/FormLocomotivCollection.Designer.cs @@ -28,20 +28,27 @@ partial class FormLocomotivCollection { /// private void InitializeComponent() { toolsBox = new GroupBox(); + collectionGroupBoxes = new GroupBox(); + storageListBox = new ListBox(); + delStorageButton = new Button(); + addStorageButton = new Button(); + storageIndexInput = new TextBox(); refreshCollection = new Button(); deleteLoco = new Button(); - input = new TextBox(); + locoIndexInput = new TextBox(); addLocomotiv = new Button(); collectionPictureBox = new PictureBox(); toolsBox.SuspendLayout(); + collectionGroupBoxes.SuspendLayout(); ((ISupportInitialize)collectionPictureBox).BeginInit(); SuspendLayout(); // // toolsBox // + toolsBox.Controls.Add(collectionGroupBoxes); toolsBox.Controls.Add(refreshCollection); toolsBox.Controls.Add(deleteLoco); - toolsBox.Controls.Add(input); + toolsBox.Controls.Add(locoIndexInput); toolsBox.Controls.Add(addLocomotiv); toolsBox.Location = new Point(944, 12); toolsBox.Name = "toolsBox"; @@ -50,9 +57,59 @@ partial class FormLocomotivCollection { toolsBox.TabStop = false; toolsBox.Text = "Инструменты"; // + // collectionGroupBoxes + // + collectionGroupBoxes.Controls.Add(storageListBox); + collectionGroupBoxes.Controls.Add(delStorageButton); + collectionGroupBoxes.Controls.Add(addStorageButton); + collectionGroupBoxes.Controls.Add(storageIndexInput); + collectionGroupBoxes.Location = new Point(7, 38); + collectionGroupBoxes.Name = "collectionGroupBoxes"; + collectionGroupBoxes.Size = new Size(296, 325); + collectionGroupBoxes.TabIndex = 5; + collectionGroupBoxes.TabStop = false; + collectionGroupBoxes.Text = "Наборы"; + // + // storageListBox + // + storageListBox.FormattingEnabled = true; + storageListBox.ItemHeight = 20; + storageListBox.Location = new Point(14, 139); + storageListBox.Name = "storageListBox"; + storageListBox.Size = new Size(271, 124); + storageListBox.TabIndex = 7; + storageListBox.SelectedIndexChanged += storagesListBox_SelectedIndexChanged; + // + // delStorageButton + // + delStorageButton.Location = new Point(14, 278); + delStorageButton.Name = "delStorageButton"; + delStorageButton.Size = new Size(271, 32); + delStorageButton.TabIndex = 6; + delStorageButton.Text = "Удалить набор"; + delStorageButton.UseVisualStyleBackColor = true; + delStorageButton.Click += delStorageButton_Click; + // + // addStorageButton + // + addStorageButton.Location = new Point(14, 87); + addStorageButton.Name = "addStorageButton"; + addStorageButton.Size = new Size(271, 31); + addStorageButton.TabIndex = 5; + addStorageButton.Text = "Добавить набор"; + addStorageButton.UseVisualStyleBackColor = true; + addStorageButton.Click += addStorageButton_Click; + // + // storageIndexInput + // + storageIndexInput.Location = new Point(14, 41); + storageIndexInput.Name = "storageIndexInput"; + storageIndexInput.Size = new Size(271, 27); + storageIndexInput.TabIndex = 4; + // // refreshCollection // - refreshCollection.Location = new Point(19, 324); + refreshCollection.Location = new Point(21, 642); refreshCollection.Name = "refreshCollection"; refreshCollection.Size = new Size(271, 59); refreshCollection.TabIndex = 3; @@ -62,7 +119,7 @@ partial class FormLocomotivCollection { // // deleteLoco // - deleteLoco.Location = new Point(21, 199); + deleteLoco.Location = new Point(21, 559); deleteLoco.Name = "deleteLoco"; deleteLoco.Size = new Size(271, 59); deleteLoco.TabIndex = 2; @@ -70,16 +127,16 @@ partial class FormLocomotivCollection { deleteLoco.UseVisualStyleBackColor = true; deleteLoco.Click += deleteLoco_Click; // - // input + // locoIndexInput // - input.Location = new Point(51, 152); - input.Name = "input"; - input.Size = new Size(214, 27); - input.TabIndex = 1; + locoIndexInput.Location = new Point(53, 512); + locoIndexInput.Name = "locoIndexInput"; + locoIndexInput.Size = new Size(214, 27); + locoIndexInput.TabIndex = 1; // // addLocomotiv // - addLocomotiv.Location = new Point(21, 40); + addLocomotiv.Location = new Point(21, 431); addLocomotiv.Name = "addLocomotiv"; addLocomotiv.Size = new Size(271, 59); addLocomotiv.TabIndex = 0; @@ -106,6 +163,8 @@ partial class FormLocomotivCollection { Text = "Набор локомотивов"; toolsBox.ResumeLayout(false); toolsBox.PerformLayout(); + collectionGroupBoxes.ResumeLayout(false); + collectionGroupBoxes.PerformLayout(); ((ISupportInitialize)collectionPictureBox).EndInit(); ResumeLayout(false); } @@ -113,9 +172,14 @@ partial class FormLocomotivCollection { #endregion private GroupBox toolsBox; - private TextBox input; + private TextBox locoIndexInput; private Button addLocomotiv; private PictureBox collectionPictureBox; private Button refreshCollection; private Button deleteLoco; + private GroupBox collectionGroupBoxes; + private Button delStorageButton; + private Button addStorageButton; + private TextBox storageIndexInput; + private ListBox storageListBox; } \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.cs b/lab1/FormLocomotivCollection.cs index beb547b..668f858 100644 --- a/lab1/FormLocomotivCollection.cs +++ b/lab1/FormLocomotivCollection.cs @@ -1,33 +1,64 @@ namespace ElectricLocomotive; public partial class FormLocomotivCollection : Form { - private readonly LocosGenericCollection _locos; + private readonly LocosGenericStorage _storage; public FormLocomotivCollection() { InitializeComponent(); - _locos = new(collectionPictureBox.Width, collectionPictureBox.Height); + _storage = new(collectionPictureBox.Width, collectionPictureBox.Height); + } + private void ReloadObjects() { + int index = storageListBox.SelectedIndex; + storageListBox.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) { + storageListBox.Items.Add(_storage.Keys[i]); + } + if (storageListBox.Items.Count > 0 && (index == -1 || index + >= storageListBox.Items.Count)) { + storageListBox.SelectedIndex = 0; + } + else if (storageListBox.Items.Count > 0 && index > -1 && + index < storageListBox.Items.Count) { + storageListBox.SelectedIndex = index; + } } private void addLocomotiv_Click(object sender, EventArgs e) { + if (storageListBox.SelectedIndex == -1) { + return; + } + var obj = _storage[storageListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) { + return; + } FormLocomotiv form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_locos + form.SelectedLocomotiv != null) { + if (obj + form.SelectedLocomotiv) { MessageBox.Show("Объект добавлен"); - collectionPictureBox.Image = _locos.ShowLocos(); + collectionPictureBox.Image = obj.ShowLocos(); } else { MessageBox.Show("Не удалось добавить объект"); } + } } - private void deleteLoco_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + if (storageListBox.SelectedIndex == -1) { return; } - int pos = Convert.ToInt32(input.Text); - if (_locos - pos != null) { + var obj = _storage[storageListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + return; + } + int pos = Convert.ToInt32(locoIndexInput.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - collectionPictureBox.Image = _locos.ShowLocos(); + collectionPictureBox.Image = obj.ShowLocos(); } else { MessageBox.Show("Не удалось удалить объект"); @@ -35,8 +66,41 @@ public partial class FormLocomotivCollection : Form { } private void refreshCollection_Click(object sender, EventArgs e) { - collectionPictureBox.Image = _locos.ShowLocos(); + if (storageListBox.SelectedIndex == -1) { + return; + } + var obj = _storage[storageListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) { + return; + } + collectionPictureBox.Image = obj.ShowLocos(); + } + private void storagesListBox_SelectedIndexChanged(object sender, EventArgs e) { + collectionPictureBox.Image = + _storage[storageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowLocos(); + } + + private void delStorageButton_Click(object sender, EventArgs e) { + if (storageListBox.SelectedIndex == -1) { + return; + } + if (MessageBox.Show($"Удалить объект{storageListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) { + _storage.DelSet(storageListBox.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } + private void addStorageButton_Click(object sender, EventArgs e) { + if (string.IsNullOrEmpty(storageIndexInput.Text)) { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(storageIndexInput.Text); + ReloadObjects(); + } } \ No newline at end of file diff --git a/lab1/Generics/LocosGenericCollection.cs b/lab1/Generics/LocosGenericCollection.cs index 9140655..b98f55a 100644 --- a/lab1/Generics/LocosGenericCollection.cs +++ b/lab1/Generics/LocosGenericCollection.cs @@ -15,27 +15,27 @@ public class LocosGenericCollection where T : DrawingLocomotiv where U : _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int operator +(LocosGenericCollection collect, T? + public static bool operator +(LocosGenericCollection collect, T? obj) { - if (obj == null) return -1; + if (obj == null) return false; - return collect?._collection.Insert(obj) ?? -1; + return collect?._collection.Insert(obj) ?? false; } - public static bool operator -(LocosGenericCollection collect, int + public static T? operator -(LocosGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowLocos() { @@ -63,13 +63,16 @@ public class LocosGenericCollection where T : DrawingLocomotiv where U : } private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) { - DrawingLocomotiv locomotiv = _collection.Get(i); - if (locomotiv != null) { + int i = 0; + foreach(var electricLoco in _collection.GetElectricLocos()) + { + if (electricLoco != null) + { int inRow = _pictureWidth / _placeSizeWidth; - locomotiv.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight); - locomotiv.DrawLoco(g); + electricLoco.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight); + electricLoco.DrawLoco(g); } + i++; } } diff --git a/lab1/Generics/LocosGenericStorage.cs b/lab1/Generics/LocosGenericStorage.cs new file mode 100644 index 0000000..cf669f5 --- /dev/null +++ b/lab1/Generics/LocosGenericStorage.cs @@ -0,0 +1,41 @@ +namespace ElectricLocomotive; + +public class LocosGenericStorage +{ + readonly Dictionary> _electricLocoStorages; + public List Keys => _electricLocoStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public LocosGenericStorage(int pictureWidth, int pictureHeight) + { + _electricLocoStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + _electricLocoStorages.Add(name, new LocosGenericCollection (_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + if (!_electricLocoStorages.ContainsKey(name)) + return; + _electricLocoStorages.Remove(name); + } + + public LocosGenericCollection? this[string ind] + { + get + { + if (_electricLocoStorages.ContainsKey(ind)) + return _electricLocoStorages[ind]; + return null; + } + } +} \ No newline at end of file diff --git a/lab1/Generics/SetGeneric.cs b/lab1/Generics/SetGeneric.cs index 5da02e3..6141000 100644 --- a/lab1/Generics/SetGeneric.cs +++ b/lab1/Generics/SetGeneric.cs @@ -2,51 +2,64 @@ public class SetGeneric where T : class { - private readonly T?[] _places; + private readonly List _places; - public int Count => _places.Length; + public int Count => _places.Count; + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - public int Insert(T electricLocomotiv) + public bool Insert(T electricLocomotiv) { - if (_places[Count - 1] != null) - return -1; - return Insert(electricLocomotiv, 0); + if (_places.Count == _maxCount) + return false; + Insert(electricLocomotiv, 0); + return true; } - public int Insert(T electricLocomotiv, int position) + public bool Insert(T electricLocomotiv, int position) { - if (!(position >= 0 && position < Count)) - return -1; - if (_places[position] != null) { - int ind = position; - while (ind < Count && _places[ind] != null) - ind++; - if (ind == Count) - return -1; - for (int i = ind - 1; i >= position; i--) - _places[i + 1] = _places[i]; - } - _places[position] = electricLocomotiv; - return position; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, electricLocomotiv); + return true; } public bool Remove(int position) { - if (!(position >= 0 && position < Count) || _places[position] == null) + if (!(position >= 0 && position < Count)) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } - public T? Get(int position) + public T? this[int position] { - if (!(position >= 0 && position < Count)) - return null; - return _places[position]; + get { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + public IEnumerable GetElectricLocos(int? maxElectricLocos = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxElectricLocos.HasValue && i == maxElectricLocos.Value) + { + yield break; + } + } } } \ No newline at end of file