Exemple #1
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> list = new List <FileCabinetRecord>();
            var    validator = new ValidatorBuilder().CreateDefault();
            string rec       = this.stream.ReadLine();

            if (string.IsNullOrEmpty(rec))
            {
                return(list);
            }

            do
            {
                string[] elements = rec.Split(", ");
                if (int.TryParse(elements[0], out int id) &&
                    DateTime.TryParse(elements[3], out DateTime dateOfBirth) &&
                    char.TryParse(elements[4], out char gender) &&
                    short.TryParse(elements[5], out short passportId) &&
                    decimal.TryParse(elements[6], out decimal salary))
                {
                    RecordData recordData = new RecordData(elements[1], elements[2], dateOfBirth, gender, passportId, salary);
                    try
                    {
                        validator.ValidateParameters(recordData);
                        var record = new FileCabinetRecord
                        {
                            Id          = id,
                            FirstName   = recordData.FirstName,
                            LastName    = recordData.LastName,
                            DateOfBirth = recordData.DateOfBirth,
                            Gender      = recordData.Gender,
                            PassportId  = recordData.PassportId,
                            Salary      = recordData.Salary,
                        };
                        list.Add(record);
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                rec = this.stream.ReadLine();
            }while (rec != null);

            return(list);
        }
Exemple #2
0
        /// <summary>
        /// Read all records from stream.
        /// </summary>
        /// <returns>List of records.</returns>
        public IList <FileCabinetRecord> ReadAll()
        {
            List <FileCabinetRecord> result = new List <FileCabinetRecord>();
            RecordForSerializer      list;

            var validator = new ValidatorBuilder().CreateDefault();

            XmlSerializer ser = new XmlSerializer(typeof(RecordForSerializer));

            try
            {
                list = (RecordForSerializer)ser.Deserialize(this.stream);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
                return(result);
            }

            foreach (var record in list.Record)
            {
                try
                {
                    RecordData data = new RecordData(record.Name.FirstName, record.Name.LastName, record.DateOfBirth, record.Gender, record.PassportId, record.Salary);
                    validator.ValidateParameters(data);
                    result.Add(new FileCabinetRecord()
                    {
                        Id          = record.Id,
                        FirstName   = record.Name.FirstName,
                        LastName    = record.Name.LastName,
                        DateOfBirth = record.DateOfBirth,
                        Gender      = record.Gender,
                        PassportId  = record.PassportId,
                        Salary      = record.Salary,
                    });
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(result);
        }