diff --git a/Airbus/Airbus/ExtentionPlane.cs b/Airbus/Airbus/ExtentionPlane.cs index 4b0d251..3873e23 100644 --- a/Airbus/Airbus/ExtentionPlane.cs +++ b/Airbus/Airbus/ExtentionPlane.cs @@ -24,7 +24,7 @@ namespace Airbus } //если аэробус - if (strs.Length == 5) + if (strs.Length == 6) { return new DrawningSuperAirbus(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5])); diff --git a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs index e36d56d..cc0ccb2 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.Designer.cs @@ -72,7 +72,7 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(627, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 510); + this.groupBoxTools.Size = new System.Drawing.Size(200, 547); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -238,7 +238,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(627, 510); + this.pictureBox.Size = new System.Drawing.Size(627, 547); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -266,12 +266,14 @@ this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // // openFileDialog // @@ -286,7 +288,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(827, 534); + this.ClientSize = new System.Drawing.Size(827, 571); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip1); diff --git a/Airbus/Airbus/FormMapWithSetPlanes.cs b/Airbus/Airbus/FormMapWithSetPlanes.cs index 5809a98..9dd90b0 100644 --- a/Airbus/Airbus/FormMapWithSetPlanes.cs +++ b/Airbus/Airbus/FormMapWithSetPlanes.cs @@ -227,10 +227,11 @@ namespace Airbus //обработка нажатия загрузки private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { if (_mapsCollection.LoadData(openFileDialog.FileName)) { + ReloadMaps(); MessageBox.Show("Загрузка данных прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } diff --git a/Airbus/Airbus/MapsCollection.cs b/Airbus/Airbus/MapsCollection.cs index ad67658..240ca3f 100644 --- a/Airbus/Airbus/MapsCollection.cs +++ b/Airbus/Airbus/MapsCollection.cs @@ -38,24 +38,22 @@ namespace Airbus //добавление карты public void AddMap(string name, AbstractMap map) { - var NewElem = new MapWithSetPlanesGeneric( - _pictureWidth, _pictureHeight, map); - _mapStorage.Add(name, NewElem); + if (!_mapStorage.ContainsKey(name)) + { + _mapStorage.Add(name, new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map)); + } } //удаление карты public void DelMap(string name) { - _mapStorage.Remove(name); - } - // Метод записи информации в файл - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); + if (_mapStorage.ContainsKey(name)) + { + _mapStorage.Remove(name); + } } - // Сохранение информации по автомобилям в хранилище в файл + // сохранение информации по автомобилям в хранилище в файл public bool SaveData(string filename) { if (File.Exists(filename)) @@ -63,21 +61,21 @@ namespace Airbus File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.Write($"MapsCollection{Environment.NewLine}"); foreach (var storage in _mapStorage) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" + - $"{Environment.NewLine}", fs); + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}" + + $"{Environment.NewLine}"); } } return true; } - // Загрузка нформации по автомобилям на парковках из файла + // загрузка информации по автомобилям на парковках из файла public bool LoadData(string filename) { if (!File.Exists(filename)) @@ -85,61 +83,60 @@ namespace Airbus return false; } - string bufferTextFromFile = ""; - - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); + string str = ""; - while (fs.Read(b, 0, b.Length) > 0) + //если не содержит такую запись или пустой файл + if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) { - bufferTextFromFile += temp.GetString(b); - } - } - - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - - //очищаем записи - _mapStorage.Clear(); - - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - - switch (elem[1]) - { - case "SimpleMap": - map = new SimpleMap(); - break; - case "DesertMap": - map = new DesertMap(); - break; - case "SpaceMap": - map = new SpaceMap(); - break; + return false; } - _mapStorage.Add(elem[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorage[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + _mapStorage.Clear(); + + while ((str = sr.ReadLine()) != null) + { + var element = str.Split(separatorDict); + AbstractMap map = null; + + switch (element[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "DesertMap": + map = new DesertMap(); + break; + case "SpaceMap": + map = new SpaceMap(); + break; + } + + _mapStorage.Add(element[0], new MapWithSetPlanesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorage[element[0]].LoadData(element[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } } return true; } - //Доступ к аэродрому + //доступ к аэродрому public MapWithSetPlanesGeneric this[string ind] { get { - return _mapStorage[ind]; + if (ind != string.Empty) + { + MapWithSetPlanesGeneric value; + + if (_mapStorage.TryGetValue(ind, out value)) + { + return value; + } + } + + return null; } } }