Exemplo n.º 1
0
 /// <inheritdoc/>
 public void EditRecord(int id, ValidateParametersData data)
 {
     this.stopwatch.Restart();
     this.fileCabinetService.EditRecord(id, data);
     this.stopwatch.Stop();
     Print(nameof(this.EditRecord), this.stopwatch.ElapsedTicks);
 }
        /// <inheritdoc/>
        public int CreateRecord(int id, ValidateParametersData data)
        {
            if (id < 1)
            {
                throw new InvalidOperationException($"Invalid id #{id} value.");
            }

            if (this.activeStorage.ContainsKey(id))
            {
                throw new InvalidOperationException($"Record with #{id} already exists.");
            }

            this.validator.ValidateParameters(data);
            FileCabinetRecord record = DataHelper.CreateRecordFromArgs(id, data);
            long position            = this.fileStream.Length;

            if (this.removedStorage.ContainsKey(id))
            {
                position = this.removedStorage[id];
                this.removedStorage.Remove(id);
            }

            this.RecordsToBytes(position, record);
            this.activeStorage.Add(id, position);
            return(record.Id);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Request data from user in console.
        /// </summary>
        /// <returns>Validate parameters data from user input.</returns>
        public static ValidateParametersData RequestData()
        {
            var data = new ValidateParametersData();

            Console.Write("First Name: ");
            data.FirstName = ReadInput <string>(Convert <string>, x => x.Length <FirstName.Min || x.Length> FirstName.Max
                                                                    ? new Tuple <bool, string>(false, nameof(data.FirstName))
                                                                    : new Tuple <bool, string>(true, nameof(data.FirstName)));
            Console.Write("Last Name: ");
            data.LastName = ReadInput <string>(Convert <string>, x => x.Length <LastName.Min || x.Length> LastName.Max
                                                                    ? new Tuple <bool, string>(false, nameof(data.LastName))
                                                                    : new Tuple <bool, string>(true, nameof(data.LastName)));
            Console.Write("Date of birth: ");
            data.DateOfBirth = ReadInput <DateTime>(Convert <DateTime>, x => x <DateOfBirth.From || x> DateOfBirth.To
                                                                    ? new Tuple <bool, string>(false, nameof(data.DateOfBirth))
                                                                    : new Tuple <bool, string>(true, nameof(data.DateOfBirth)));
            Console.Write("Bonuses: ");
            data.Bonuses = ReadInput <short>(Convert <short>, x => x <Bonuses.Min || x> Bonuses.Max
                                                                    ? new Tuple <bool, string>(false, nameof(data.Bonuses))
                                                                    : new Tuple <bool, string>(true, nameof(data.Bonuses)));
            Console.Write($"Salary(min: {Salary.Min} max: {Salary.Max}): ");
            data.Salary = ReadInput <decimal>(Convert <decimal>, x => x <Salary.Min || x> Salary.Max
                                                                    ? new Tuple <bool, string>(false, nameof(data.Salary))
                                                                    : new Tuple <bool, string>(true, nameof(data.Salary)));
            Console.Write("Account type: ");
            data.AccountType = ReadInput <char>(Convert <char>, x => char.IsLetter(x)
                                                                    ? new Tuple <bool, string>(true, nameof(data.AccountType))
                                                                    : new Tuple <bool, string>(false, nameof(data.AccountType)));
            return(data);
        }
        /// <inheritdoc/>
        public int CreateAndSetId(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            return(this.CreateRecord(this.GenerateId(), data));
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public int CreateRecord(int id, ValidateParametersData data)
        {
            this.stopwatch.Restart();
            var value = this.fileCabinetService.CreateRecord(id, data);

            this.stopwatch.Stop();
            Print(nameof(this.CreateAndSetId), this.stopwatch.ElapsedTicks);
            return(value);
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public void EditRecord(int id, ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            this.Print(nameof(this.EditRecord), $"{nameof(id)} = '{id.ToString(CultureInfo.InvariantCulture)}', {data.ToString()}");
            this.fileCabinetService.EditRecord(id, data);
        }
        /// <inheritdoc/>
        public void ValidateParameters(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException($"{nameof(data)} cannot be null.");
            }

            if (data.Salary < this.Min || data.Salary > this.Max)
            {
                throw new ArgumentException($"{nameof(data.Salary)} cannot be less than {this.Min} and more than {this.Max}");
            }
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public int CreateRecord(int id, ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var value = this.fileCabinetService.CreateRecord(id, data);

            this.Print(nameof(this.CreateRecord), value.ToString(CultureInfo.InvariantCulture));
            return(value);
        }
        /// <inheritdoc/>
        public void ValidateParameters(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException($"{nameof(data)} cannot be null.");
            }

            if (data.DateOfBirth < this.From || data.DateOfBirth > this.To)
            {
                throw new ArgumentException($"{nameof(data.DateOfBirth)} cannot be less then {this.From.Date} and more then {this.To.Date}.");
            }
        }
Exemplo n.º 10
0
        /// <inheritdoc/>
        public void ValidateParameters(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            foreach (var validator in this.validators)
            {
                validator.ValidateParameters(data);
            }
        }
        /// <inheritdoc/>
        public void ValidateParameters(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException($"{nameof(data)} cannot be null.");
            }

            if (!char.IsLetter(data.AccountType))
            {
                throw new ArgumentException($"{nameof(data.AccountType)} should be a letter.");
            }
        }
        /// <inheritdoc/>
        public void ValidateParameters(ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException($"{nameof(data)} cannot be null.");
            }

            if (string.IsNullOrWhiteSpace(data.LastName))
            {
                throw new ArgumentNullException($"{nameof(data.LastName)} cannot be null and contains only white spaces.");
            }

            if (data.LastName.Length < this.Min || data.LastName.Length > this.Max)
            {
                throw new ArgumentException($"{nameof(data.LastName)} cannot be less than {this.Min}, more than {this.Max}.");
            }
        }
        private ValidateParametersData TrimFields(FileCabinetRecord validArgs, FileCabinetRecord record)
        {
            var args = DataHelper.CreateValidateData(validArgs);
            var defaultValidateArgs = new ValidateParametersData();
            var current             = args.Clone();

            foreach (var item in ValidateParametersProperties)
            {
                if (Equals(item.GetValue(args), item.GetValue(defaultValidateArgs)))
                {
                    var recordProp = FileCabinetRecordProperties.FirstOrDefault(x => x.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase));
                    item.SetValue(current, recordProp.GetValue(record));
                }
            }

            return(current);
        }
        /// <inheritdoc/>
        public void EditRecord(int id, ValidateParametersData data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!this.activeStorage.ContainsKey(id))
            {
                throw new InvalidOperationException($"Record #{id} doesn't exists.");
            }

            this.validator.ValidateParameters(data);
            var current = DataHelper.CreateRecordFromArgs(id, data);

            DataHelper.UpdateRecordFromData(current.Id, data, current);
            this.RecordsToBytes(this.activeStorage[id], current);
        }
Exemplo n.º 15
0
 private void Insert(int id, ValidateParametersData data)
 {
     try
     {
         id = id == 0 ? this.Service.CreateAndSetId(data) : this.Service.CreateRecord(id, data);
         Console.WriteLine($"Record #{id} is created.");
     }
     catch (InvalidOperationException ioe)
     {
         Console.WriteLine(ioe.Message);
     }
     catch (FormatException fe)
     {
         Console.WriteLine(fe.Message);
     }
     catch (OverflowException oe)
     {
         Console.WriteLine(oe.Message);
     }
     catch (ArgumentException ae)
     {
         Console.WriteLine(ae.Message);
     }
 }