diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs index 86e7714..22ccdf8 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs @@ -28,6 +28,11 @@ private void InitializeComponent() { this.groupBoxTool = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); @@ -40,11 +45,13 @@ this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTool.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxTool // + this.groupBoxTool.Controls.Add(this.groupBoxMaps); this.groupBoxTool.Controls.Add(this.buttonShowOnMap); this.groupBoxTool.Controls.Add(this.buttonLeft); this.groupBoxTool.Controls.Add(this.buttonRight); @@ -54,23 +61,70 @@ this.groupBoxTool.Controls.Add(this.buttonRemoveTanker); this.groupBoxTool.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTool.Controls.Add(this.buttonAddTanker); - this.groupBoxTool.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTool.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTool.Location = new System.Drawing.Point(842, 0); - this.groupBoxTool.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.groupBoxTool.Location = new System.Drawing.Point(962, 0); this.groupBoxTool.Name = "groupBoxTool"; - this.groupBoxTool.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.groupBoxTool.Size = new System.Drawing.Size(192, 565); + this.groupBoxTool.Size = new System.Drawing.Size(220, 753); this.groupBoxTool.TabIndex = 0; this.groupBoxTool.TabStop = false; this.groupBoxTool.Text = "Инструменты"; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(6, 26); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(207, 289); + this.groupBoxMaps.TabIndex = 10; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(4, 142); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(200, 104); + this.listBoxMaps.TabIndex = 13; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(4, 106); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(200, 30); + this.buttonAddMap.TabIndex = 11; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(4, 252); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(200, 30); + this.buttonDeleteMap.TabIndex = 12; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(4, 26); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(200, 27); + this.textBoxNewMapName.TabIndex = 11; + // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(9, 382); - this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonShowOnMap.Location = new System.Drawing.Point(10, 572); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(175, 22); + this.buttonShowOnMap.Size = new System.Drawing.Size(200, 30); this.buttonShowOnMap.TabIndex = 9; this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -80,10 +134,9 @@ // this.buttonLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(44, 525); - this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonLeft.Location = new System.Drawing.Point(50, 700); this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(35, 30); + this.buttonLeft.Size = new System.Drawing.Size(40, 40); this.buttonLeft.TabIndex = 8; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); @@ -93,10 +146,9 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(122, 525); - this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRight.Location = new System.Drawing.Point(140, 700); this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(35, 30); + this.buttonRight.Size = new System.Drawing.Size(40, 40); this.buttonRight.TabIndex = 7; this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); @@ -105,10 +157,9 @@ // this.buttonDown.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(83, 525); - this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDown.Location = new System.Drawing.Point(95, 700); this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(35, 30); + this.buttonDown.Size = new System.Drawing.Size(40, 40); this.buttonDown.TabIndex = 6; this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); @@ -118,20 +169,18 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(83, 491); - this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUp.Location = new System.Drawing.Point(95, 655); this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(35, 30); + this.buttonUp.Size = new System.Drawing.Size(40, 40); this.buttonUp.TabIndex = 5; this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(9, 298); - this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonShowStorage.Location = new System.Drawing.Point(10, 484); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(175, 22); + this.buttonShowStorage.Size = new System.Drawing.Size(200, 30); this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -139,10 +188,9 @@ // // buttonRemoveTanker // - this.buttonRemoveTanker.Location = new System.Drawing.Point(9, 262); - this.buttonRemoveTanker.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRemoveTanker.Location = new System.Drawing.Point(10, 436); this.buttonRemoveTanker.Name = "buttonRemoveTanker"; - this.buttonRemoveTanker.Size = new System.Drawing.Size(175, 22); + this.buttonRemoveTanker.Size = new System.Drawing.Size(200, 30); this.buttonRemoveTanker.TabIndex = 3; this.buttonRemoveTanker.Text = "Удалить грузовик"; this.buttonRemoveTanker.UseVisualStyleBackColor = true; @@ -150,19 +198,17 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(9, 238); - this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Location = new System.Drawing.Point(10, 403); this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(176, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(200, 27); this.maskedTextBoxPosition.TabIndex = 2; + this.maskedTextBoxPosition.Text = "_"; // // buttonAddTanker // - this.buttonAddTanker.Location = new System.Drawing.Point(9, 211); - this.buttonAddTanker.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonAddTanker.Location = new System.Drawing.Point(10, 367); this.buttonAddTanker.Name = "buttonAddTanker"; - this.buttonAddTanker.Size = new System.Drawing.Size(175, 22); + this.buttonAddTanker.Size = new System.Drawing.Size(200, 30); this.buttonAddTanker.TabIndex = 1; this.buttonAddTanker.Text = "Добавить грузовик"; this.buttonAddTanker.UseVisualStyleBackColor = true; @@ -176,36 +222,34 @@ "Простая карта", "Минное поле", "Лужайка"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(9, 26); - this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(4, 60); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(200, 28); this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); - this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(842, 565); + this.pictureBox.Size = new System.Drawing.Size(962, 753); this.pictureBox.TabIndex = 9; this.pictureBox.TabStop = false; // // FormMapWithSetTankers // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1034, 565); + this.ClientSize = new System.Drawing.Size(1182, 753); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTool); - this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.Name = "FormMapWithSetTankers"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Карта с набором объектов"; this.groupBoxTool.ResumeLayout(false); this.groupBoxTool.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -224,5 +268,10 @@ private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; private Button buttonShowOnMap; + private GroupBox groupBoxMaps; + private ListBox listBoxMaps; + private Button buttonAddMap; + private Button buttonDeleteMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs index 8d57655..f3f77da 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs @@ -13,42 +13,83 @@ namespace GasolineTanker { public partial class FormMapWithSetTankers : Form { - private MapWithSetTankersGeneric _mapTankerCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Минное поле", new MineField() }, + { "Лужайка", new FormLawn() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetTankers() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Минное поле": - map = new MineField(); - break; - case "Лужайка": - map = new FormLawn(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapTankerCollectionGeneric = new MapWithSetTankersGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapTankerCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; + } + } + + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); } } private void ButtonAddTanker_Click(object sender, EventArgs e) { - if (_mapTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -56,10 +97,10 @@ namespace GasolineTanker if (form.ShowDialog() == DialogResult.OK) { DrawningObjectTanker tanker = new(form.SelectedTanker); - if (_mapTankerCollectionGeneric + tanker) + if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + tanker != -1)) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapTankerCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -70,6 +111,10 @@ namespace GasolineTanker private void ButtonRemoveTanker_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -79,10 +124,10 @@ namespace GasolineTanker return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapTankerCollectionGeneric - pos) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapTankerCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -92,25 +137,25 @@ namespace GasolineTanker private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapTankerCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapTankerCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapTankerCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -131,7 +176,7 @@ namespace GasolineTanker dir = Direction.Right; break; } - pictureBox.Image = _mapTankerCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } diff --git a/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs b/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs index b7de623..adf904e 100644 --- a/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs +++ b/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs @@ -21,17 +21,13 @@ _map = map; } - public static bool operator +(MapWithSetTankersGeneric map, T tanker) + public static int operator +(MapWithSetTankersGeneric map, T tanker) { - if (map._setTankers.Insert(tanker) >= 0) - return true; - else return false; + return map._setTankers.Insert(tanker); } - public static bool operator -(MapWithSetTankersGeneric map, int position) + public static T operator -(MapWithSetTankersGeneric map, int position) { - if (map._setTankers.Remove(position) != null) - return true; - else return false; + return map._setTankers.Remove(position); } public Bitmap ShowSet() @@ -45,13 +41,9 @@ public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setTankers.Count; i++) + foreach (var tanker in _setTankers.GetTankers()) { - var Tanker = _setTankers.Get(i); - if (Tanker != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, Tanker); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, tanker); } return new(_pictureWidth, _pictureHeight); } @@ -70,11 +62,11 @@ int j = _setTankers.Count - 1; for (int i = 0; i < _setTankers.Count; i++) { - if (_setTankers.Get(i) == null) + if (_setTankers[i] == null) { for (; j > i; j--) { - var Tanker = _setTankers.Get(j); + var Tanker = _setTankers[j]; if (Tanker != null) { _setTankers.Insert(Tanker, i); @@ -110,10 +102,10 @@ int xPosition = 10; int yPosition = (_pictureHeight / _placeSizeHeight - 1) * _placeSizeHeight + 10; - for (int i = 0; i < _setTankers.Count; i++) + foreach (var tanker in _setTankers.GetTankers()) { - _setTankers.Get(i)?.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight); - _setTankers.Get(i)?.DrawningObject(g); + tanker.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight); + tanker.DrawningObject(g); xPosition += _placeSizeWidth; if (xPosition + _placeSizeWidth > _pictureWidth) @@ -123,6 +115,7 @@ } } } + } } diff --git a/GasolineTanker/GasolineTanker/MapsCollection.cs b/GasolineTanker/GasolineTanker/MapsCollection.cs new file mode 100644 index 0000000..86230d8 --- /dev/null +++ b/GasolineTanker/GasolineTanker/MapsCollection.cs @@ -0,0 +1,45 @@ +using System.Linq; +using System.Xml.Linq; + +namespace GasolineTanker +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); + + private readonly int _pictureWidth; + private readonly int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(string name, AbstractMap map) + { + if (!_mapStorages.ContainsKey(name)) + { + _mapStorages.Add(name, new MapWithSetTankersGeneric + (_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) + _mapStorages.Remove(name); + } + public MapWithSetTankersGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + return _mapStorages[ind]; + else + return null; + } + } + } +} diff --git a/GasolineTanker/GasolineTanker/SetTankersGeneric.cs b/GasolineTanker/GasolineTanker/SetTankersGeneric.cs index 19f77ef..e49a163 100644 --- a/GasolineTanker/GasolineTanker/SetTankersGeneric.cs +++ b/GasolineTanker/GasolineTanker/SetTankersGeneric.cs @@ -3,78 +3,69 @@ internal class SetTankersGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetTankersGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T tanker) { - bool freeSpace = false; - int firstFreeElement = -1; - for (int i = Count - 1; i >= 0; i--) - { - if (_places[i] == null) - { - freeSpace = true; - firstFreeElement = i; - } - } - if (!freeSpace) - return -1; - - for (int i = firstFreeElement - 1; i >= 0; i--) - { - _places[i + 1] = _places[i]; - } - _places[0] = tanker; - - return 0; + return Insert(tanker, 0); } public int Insert(T tanker, int position) { - if (_places[position] != null) + if (position <= _places.Count && _places.Count < _maxCount && position >= 0) { - bool freeSpace = false; - int firstFreeElement = -1; - for (int i = Count - 1; i < position; i--) - { - if (_places[i] == null) - { - freeSpace = true; - firstFreeElement = i; - } - } - if (!freeSpace) - return -1; - - for (int i = firstFreeElement - 1; i > position; i--) - { - _places[i + 1] = _places[i]; - } + _places.Insert(position, tanker); + return position; } - _places[position] = tanker; - return position; + else + return -1; } public T Remove(int position) { - if (_places[position] != null) + if (position < _places.Count && position >= 0) { - T result = _places[position]; - _places[position] = null; - return result; + var tanker = _places[position]; + _places.RemoveAt(position); + return tanker; } else return null; } - public T Get(int position) + + public T this[int position] { - if (_places[position] != null) - return _places[position]; - else return null; + get + { + if (position < _places.Count && position >= 0) + return _places[position]; + else + return null; + } + set + { + Insert(value, position); + } + } + public IEnumerable GetTankers() + { + foreach (var tanker in _places) + { + if (tanker != null) + { + yield return tanker; + } + else + { + yield break; + } + } } } }