diff --git a/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs index cc4bbf8..e886814 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public abstract class AbstractStrategy { diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs index 5446a8e..a2add34 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.Entities +namespace AirplaneWithRadar.Entities { public class AirplaneEntity { diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs index 7b20c5b..c531730 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.Entities +namespace AirplaneWithRadar.Entities { public class AirplaneWithRadarEntity : AirplaneEntity { diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs index 5f6614c..04f5daf 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs @@ -1,9 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.PaintObjects; using AirplaneWithRadar.MovementStrategy; namespace AirplaneWithRadar.Generics @@ -38,7 +33,7 @@ namespace AirplaneWithRadar.Generics } public static bool operator -(AirplanesGenericCollection collect, int pos) { - T? obj = collect.collection.Get(pos); + T? obj = collect.collection[pos]; if (obj == null) { return false; @@ -48,7 +43,7 @@ namespace AirplaneWithRadar.Generics public U? GetU(int pos) { - return (U?)collection.Get(pos)?.GetMoveableObject; + return (U?)collection[pos]?.GetMoveableObject; } public Bitmap ShowAirplanes() @@ -80,9 +75,7 @@ namespace AirplaneWithRadar.Generics int j = 3; int k = 0; - for (int i = 0; i < collection.Count; i++) - { - PaintAirplane? airplane = collection.Get(i); + foreach (var airplane in collection.GetAirplanes()) { if (j < 0) { j += 4; diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs new file mode 100644 index 0000000..2eeb4b5 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericStorage.cs @@ -0,0 +1,37 @@ +using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.MovementStrategy; + +namespace AirplaneWithRadar.Generics +{ + internal class AirplanesGenericStorage + { + readonly Dictionary> airplaneStorages; + public List Keys => airplaneStorages.Keys.ToList(); + private readonly int pictWidth; + private readonly int pictHeight; + public AirplanesGenericStorage(int pictureWidth, int pictureHeight) + { + airplaneStorages = new Dictionary>(); + pictWidth = pictureWidth; + pictHeight = pictureHeight; + } + + public void AddSet(string name) + { + airplaneStorages.Add(name, new AirplanesGenericCollection(pictWidth, pictHeight)); + } + public void DelSet(string name) + { + airplaneStorages.Remove(name); + } + public AirplanesGenericCollection? this[string ind] + { + get + { + if (airplaneStorages.ContainsKey(ind)) return airplaneStorages[ind]; + return null; + } + } + } + +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs index 2c0f837..ca4c9ed 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -29,17 +29,24 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + buttonDelObject = new Button(); + buttonAddObject = new Button(); + textBoxStorageName = new TextBox(); + listBoxStorages = new ListBox(); maskedTextBoxNumber = new TextBox(); ButtonRefreshCollection = new Button(); ButtonDeleteAirplane = new Button(); ButtonAddAirplane = new Button(); pictureBoxCollection = new PictureBox(); groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // groupBox1 // + groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(ButtonRefreshCollection); groupBox1.Controls.Add(ButtonDeleteAirplane); @@ -51,16 +58,66 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // groupBox2 + // + groupBox2.Controls.Add(buttonDelObject); + groupBox2.Controls.Add(buttonAddObject); + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Controls.Add(listBoxStorages); + groupBox2.Location = new Point(13, 22); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(204, 209); + groupBox2.TabIndex = 5; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(17, 178); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(170, 25); + buttonDelObject.TabIndex = 7; + buttonDelObject.Text = "Удалить набор"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += buttonDelObject_Click; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(17, 51); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(170, 23); + buttonAddObject.TabIndex = 6; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += buttonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(10, 22); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(188, 23); + textBoxStorageName.TabIndex = 5; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(10, 93); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(188, 79); + listBoxStorages.TabIndex = 4; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(39, 148); + maskedTextBoxNumber.Location = new Point(39, 289); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(161, 23); maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(20, 229); + ButtonRefreshCollection.Location = new Point(20, 370); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(194, 46); ButtonRefreshCollection.TabIndex = 2; @@ -70,7 +127,7 @@ // // ButtonDeleteAirplane // - ButtonDeleteAirplane.Location = new Point(20, 177); + ButtonDeleteAirplane.Location = new Point(20, 318); ButtonDeleteAirplane.Name = "ButtonDeleteAirplane"; ButtonDeleteAirplane.Size = new Size(194, 46); ButtonDeleteAirplane.TabIndex = 1; @@ -80,7 +137,7 @@ // // ButtonAddAirplane // - ButtonAddAirplane.Location = new Point(20, 22); + ButtonAddAirplane.Location = new Point(23, 237); ButtonAddAirplane.Name = "ButtonAddAirplane"; ButtonAddAirplane.Size = new Size(194, 46); ButtonAddAirplane.TabIndex = 0; @@ -108,6 +165,8 @@ Text = "FormAirplaneCollection"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } @@ -120,5 +179,10 @@ private Button ButtonAddAirplane; private PictureBox pictureBoxCollection; private TextBox maskedTextBoxNumber; + private ListBox listBoxStorages; + private GroupBox groupBox2; + private Button buttonDelObject; + private Button buttonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs index e5b058a..d8e5a67 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.cs @@ -1,69 +1,119 @@ using AirplaneWithRadar.Generics; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using AirplaneWithRadar.PaintObjects; -using AirplaneWithRadar.MovementStrategy; - namespace AirplaneWithRadar { public partial class FormAirplaneCollection : Form { - private readonly AirplanesGenericCollection airplanes; + private readonly AirplanesGenericStorage storage; public FormAirplaneCollection() { InitializeComponent(); - airplanes = new AirplanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + storage = new AirplanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } + } + private void buttonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + + } + + private void buttonDelObject_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); + } private void ButtonAddAirplane_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } PlaneVisual form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (airplanes + form.SelectedAirplane != -1) + if (!(obj + form.SelectedAirplane == -1)) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { MessageBox.Show("Не удалось добавить объект"); } } - } private void ButtonDeleteAirplane_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (airplanes - pos != false) + if (obj - pos != false) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = airplanes.ShowAirplanes(); + pictureBoxCollection.Image = obj.ShowAirplanes(); } else { MessageBox.Show("Не удалось удалить объект"); } - } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = airplanes.ShowAirplanes(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAirplanes(); + } + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx index f298a7b..a395bff 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx +++ b/AirplaneWithRadar/AirplaneWithRadar/FormAirplaneCollection.resx @@ -1,4 +1,64 @@ - + + + diff --git a/AirplaneWithRadar/AirplaneWithRadar/GenericClass.cs b/AirplaneWithRadar/AirplaneWithRadar/GenericClass.cs deleted file mode 100644 index b6660b9..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/GenericClass.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar -{ - internal class GenericClass - where T : AirplaneWithRadar.PaintObjects.PaintAirplaneWithRadar - { - - } -} - diff --git a/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs index 7a91c94..29d8857 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public interface IMoveableObject { diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs index bb71a67..1598377 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public class MoveToBorder : AbstractStrategy { diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs index f220cfb..d3f01c0 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs @@ -1,11 +1,4 @@ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public class MoveToCenter : AbstractStrategy { diff --git a/AirplaneWithRadar/AirplaneWithRadar/Movement.cs b/AirplaneWithRadar/AirplaneWithRadar/Movement.cs index 797b628..9eb9585 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/Movement.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/Movement.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar +namespace AirplaneWithRadar { public enum Movement { diff --git a/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs index cf4c3de..01de238 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public class ObjectParameters { diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs index e57be41..4b1d36d 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing.Drawing2D; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing.Drawing2D; using AirplaneWithRadar.Entities; using AirplaneWithRadar.MovementStrategy; diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs index 6bb7691..31ac10d 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs @@ -1,12 +1,4 @@ - -using System.Collections.Generic; -using System.Drawing.Drawing2D; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using System.Threading.Tasks; - -using AirplaneWithRadar.Entities; +using AirplaneWithRadar.Entities; namespace AirplaneWithRadar.PaintObjects { diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs index 82666cf..5322b9e 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs @@ -1,10 +1,4 @@ -using AirplaneWithRadar.MovementStrategy; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.PaintObjects; namespace AirplaneWithRadar.MovementStrategy diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs index f7f03fb..4a310b5 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs @@ -1,22 +1,24 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.Generics; +namespace AirplaneWithRadar.Generics; internal class SetGeneric where T : class { - private readonly T?[] places; - public int Count => places.Length; + private readonly List places; + public int Count => places.Count; + public readonly int maxCount; public SetGeneric(int count) { - places = new T?[count]; + maxCount = count; + places = new List(count); } public int Insert(T airplane) { + if (Count == 0) + { + places.Add(airplane); + return 0; + } int emptyPosition = -1; for (int i = 0; i < Count; i++) { @@ -28,15 +30,10 @@ internal class SetGeneric } if (emptyPosition < 0) { - return -1; + places.Add(airplane); + return 0; } - - for (int i = emptyPosition; i > 0; i--) - { - places[i] = places[i - 1]; - } - - places[0] = airplane; + Insert(airplane, emptyPosition); return 0; } public int Insert(T airplane, int position) @@ -79,15 +76,41 @@ internal class SetGeneric { return false; } - places[position] = null; + places[position]=null; return true; } - public T? Get(int position) + + public T? this[int position] { - if (position >= Count && position < 0) + get { - return null; + if (position >= Count || position < 0) + { + return null; + } + return places[position]; + } + set + { + if (position >= Count || position < 0) + { + return; + } + if (Count <= maxCount) + { + Insert(value, position); + } + } + } + public IEnumerable GetAirplanes(int? maxAirplanes = null) + { + for (int i = 0; i < places.Count; ++i) + { + yield return places[i]; + if (maxAirplanes.HasValue && i == maxAirplanes.Value) + { + yield break; + } } - return places[position]; } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/Status.cs b/AirplaneWithRadar/AirplaneWithRadar/Status.cs index 98eb831..2e5b7b7 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/Status.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/Status.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AirplaneWithRadar.MovementStrategy +namespace AirplaneWithRadar.MovementStrategy { public enum Status {