From 3fb85c16dd69f75b099814658470f4486edbe398 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Wed, 15 Nov 2023 11:34:08 +0400 Subject: [PATCH] FileSave --- Lab/CarsGenericStorage.cs | 75 +++++++++++++++-------------------- Lab/ExtentionDrawingTanker.cs | 4 +- Lab/SetGeneric.cs | 4 ++ 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/Lab/CarsGenericStorage.cs b/Lab/CarsGenericStorage.cs index ea2296b..a15aa7b 100644 --- a/Lab/CarsGenericStorage.cs +++ b/Lab/CarsGenericStorage.cs @@ -68,11 +68,13 @@ namespace Lab.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); - return true; + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.WriteLine("CarStorage"); + writer.Write(data.ToString()); + return true; + } + } public bool LoadData(string filename) @@ -81,52 +83,41 @@ namespace Lab.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader reader = new StreamReader(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string checker = reader.ReadLine(); + if (checker == null) + return false; + string strs = reader.ReadLine(); + if (!checker.StartsWith("CarStorage")) + return false; + _carStorages.Clear(); + foreach (string data in strs.Split(';')) { - bufferTextFromFile += temp.GetString(b); - } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("CarStorage")) - { - return false; - } - _carStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } - CarsGenericCollection collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - DrawTanker? car = - elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight); - if (car != null) + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - if (!(collection + car)) + continue; + } + CarsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawTanker? car = + elem?.CreateDrawTanker(_separatorForObject, _pictureWidth, _pictureHeight); + if (car != null) { - return false; + if (!(collection + car)) + { + return false; + } } } + _carStorages.Add(record[0], collection); } - _carStorages.Add(record[0], collection); + return true; } - return true; } - } } diff --git a/Lab/ExtentionDrawingTanker.cs b/Lab/ExtentionDrawingTanker.cs index 05a42b5..a94774d 100644 --- a/Lab/ExtentionDrawingTanker.cs +++ b/Lab/ExtentionDrawingTanker.cs @@ -38,12 +38,12 @@ namespace Lab { return string.Empty; } - var str = $"{Tanker.Speed}{separatorForObject}{Tanker.Weight}{separatorForObject}{Tanker.BodyColor.Name}"; + var str = $"{Tanker.Speed}{separatorForObject}{Tanker.Weight}{separatorForObject}{Tanker.BodyColor}"; if (Tanker is not GasolineTanker gasTanker) { return str; } - return $"{str}{separatorForObject}{gasTanker.AdditionalColor.Name}{separatorForObject}{gasTanker.BodyKit}{separatorForObject}{gasTanker.Wing}{separatorForObject}{gasTanker.SportLine}"; + return $"{str}{separatorForObject}{gasTanker.AdditionalColor}{separatorForObject}{gasTanker.BodyKit}{separatorForObject}{gasTanker.Wing}{separatorForObject}{gasTanker.SportLine}"; } } diff --git a/Lab/SetGeneric.cs b/Lab/SetGeneric.cs index 7e73e02..48cd770 100644 --- a/Lab/SetGeneric.cs +++ b/Lab/SetGeneric.cs @@ -49,12 +49,16 @@ namespace Lab.Generics { if (position < 0 || position > _maxCount) return null; + if (_places.Count <= position) + return null; return _places[position]; } set { if (position < 0 || position > _maxCount) return; + if (_places.Count <= position) + return; _places[position] = value; } }