Example #1
0
        /// <summary>
        /// Creates new records with given parameters.
        /// </summary>
        /// <param name="transfer">Object to transfer parameters of new record.</param>
        /// <returns>ID of created record.</returns>
        /// <exception cref="ArgumentNullException">Throw when transfer object is null.</exception>
        /// <exception cref="ArgumentException">Thrown when transfer data is invalid.</exception>
        public int CreateRecord(RecordParametersTransfer transfer)
        {
            if (transfer is null)
            {
                throw new ArgumentNullException(nameof(transfer), Configurator.GetConstantString("NullTransfer"));
            }

            var validationResult = this.recordValidator.ValidateParameters(transfer.RecordSimulation());

            if (!validationResult.Item1)
            {
                throw new ArgumentException(validationResult.Item2);
            }

            int id;

            if (this.dictionaryIdOffset.Count is 0)
            {
                id = 1;
            }
            else
            {
                id = this.dictionaryIdOffset.Keys.Max() + 1;
            }

            this.dictionaryIdOffset.Add(id, this.currentOffset);
            this.currentOffset += this.sizeOfShort;
            this.binaryWriter.Seek(this.currentOffset, 0);
            this.binaryWriter.Write(id);
            this.currentOffset += this.sizeOfInt;
            this.binaryWriter.Write(transfer.FirstName);
            this.currentOffset += this.sizeOfString;
            this.binaryWriter.Seek(this.currentOffset, 0);
            this.binaryWriter.Write(transfer.LastName);
            this.currentOffset += this.sizeOfString;
            this.binaryWriter.Seek(this.currentOffset, 0);
            this.binaryWriter.Write(transfer.DateOfBirth.Day);
            this.currentOffset += this.sizeOfInt;
            this.binaryWriter.Write(transfer.DateOfBirth.Month);
            this.currentOffset += this.sizeOfInt;
            this.binaryWriter.Write(transfer.DateOfBirth.Year);
            this.currentOffset += this.sizeOfInt;
            this.binaryWriter.Write(transfer.PatronymicLetter);
            this.currentOffset += this.sizeOfChar;
            this.binaryWriter.Write(transfer.Income);
            this.currentOffset += this.sizeOfDecimal;
            this.binaryWriter.Write(transfer.Height);
            this.currentOffset += this.sizeOfShort;
            return(id);
        }
        /// <summary>
        /// Creates new records with given parameters.
        /// </summary>
        /// <param name="transfer">Object to transfer parameters of new record.</param>
        /// <returns>ID of created record.</returns>
        /// <exception cref="ArgumentNullException">Throw when transfer object is null.</exception>
        /// <exception cref="ArgumentException">Thrown when transfer data is invalid.</exception>
        public int CreateRecord(RecordParametersTransfer transfer)
        {
            if (transfer is null)
            {
                throw new ArgumentNullException(nameof(transfer), Configurator.GetConstantString("NullTransfer"));
            }

            var validationResult = this.recordValidator.ValidateParameters(transfer.RecordSimulation());

            if (!validationResult.Item1)
            {
                throw new ArgumentException(validationResult.Item2);
            }

            var record = new FileCabinetRecord
            {
                FirstName        = transfer.FirstName,
                LastName         = transfer.LastName,
                DateOfBirth      = transfer.DateOfBirth,
                Height           = transfer.Height,
                Income           = transfer.Income,
                PatronymicLetter = transfer.PatronymicLetter,
            };

            if (this.list.Count is 0)
            {
                record.Id = 1;
            }
            else
            {
                record.Id = this.ids.Max() + 1;
            }

            this.list.Add(record);
            this.ids.Add(record.Id);
            return(record.Id);
        }