示例#1
0
        /// <summary>
        /// Edits a record.
        /// </summary>
        /// <param name="id">An ID of the editable record.</param>
        /// <param name="changedRecord">Changed record.</param>
        public void EditRecord(int id, FileCabinetRecordWithoutID changedRecord)
        {
            using (var reader = new BinaryReader(this.stream, Encoding.Unicode, true))
            {
                this.stream.Seek(SizeOfShort, SeekOrigin.Begin);
                int foundId = 0;

                while (reader.PeekChar() > -1)
                {
                    foundId = reader.ReadInt32();

                    if (foundId == id)
                    {
                        this.stream.Seek(-SizeOfInt, SeekOrigin.Current);
                        break;
                    }

                    this.stream.Seek(SizeOfRecord - SizeOfInt, SeekOrigin.Current);
                }

                if (foundId == 0)
                {
                    throw new ArgumentException($"A record with ID {id} was not found.");
                }
            }

            using (var writer = new BinaryWriter(this.stream, Encoding.Unicode, true))
            {
                this.WriteRecordWithoutID(writer, id, changedRecord);
                this.stream.Seek(0, SeekOrigin.Begin);
            }
        }
示例#2
0
        private void WriteRecordWithoutID(BinaryWriter writer, int id, FileCabinetRecordWithoutID record)
        {
            writer.Write(id);

            var firstName = new char[60];

            for (int i = 0; i < record.FirstName.Length && i < MaxLengthForFirstNameDefault; i++)
            {
                firstName[i] = record.FirstName[i];
            }

            writer.Write(firstName);

            var lastName = new char[60];

            for (int i = 0; i < record.LastName.Length && i < MaxLengthForLastNameDefault; i++)
            {
                lastName[i] = record.LastName[i];
            }

            writer.Write(lastName);

            writer.Write(record.Sex);
            writer.Write(record.DateOfBirth.Day);
            writer.Write(record.DateOfBirth.Month);
            writer.Write(record.DateOfBirth.Year);
            writer.Write(record.Weight);
            writer.Write(record.Balance);
        }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileCabinetRecord"/> class.
 /// </summary>
 /// <param name="record">A record without ID.</param>
 public FileCabinetRecord(FileCabinetRecordWithoutID record)
 {
     this.FirstName   = record.FirstName;
     this.LastName    = record.LastName;
     this.Sex         = record.Sex;
     this.DateOfBirth = record.DateOfBirth;
     this.Weight      = record.Weight;
     this.Balance     = record.Balance;
 }
        /// <summary>
        /// Validates a record.
        /// </summary>
        /// <param name="record">Record to validate.</param>
        public void ValidateParameters(FileCabinetRecordWithoutID record)
        {
            this.ValidateFirstName(record.FirstName);

            this.ValidateLastName(record.LastName);

            this.ValidateSex(record.Sex);

            this.ValidateDateOfBirth(record.DateOfBirth);

            this.ValidateWeight(record.Weight);

            this.ValidateBalance(record.Balance);
        }
示例#5
0
        private static void Edit(string parameters)
        {
            try
            {
                int id;

                if (!int.TryParse(parameters, out id))
                {
                    Console.WriteLine("invalid id");
                    return;
                }

                Console.Write("first name: ");
                string firstName = ReadInput(StringConverter, FirstNameValidator);

                Console.Write("last name: ");
                string lastName = ReadInput(StringConverter, LastNameValidator);

                Console.Write("sex: ");
                char sex = ReadInput(CharConverter, SexValidator);

                Console.Write("weight: ");
                short weight = ReadInput(ShortConverter, WeightValidator);

                Console.Write("date: ");
                DateTime dateOfBirth = ReadInput(DateConverter, DateOfBirthValidator);

                Console.Write("balance: ");
                decimal balance = ReadInput(DecimalConverter, BalanceValidator);

                var changedRecord = new FileCabinetRecordWithoutID
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Sex         = sex,
                    Weight      = weight,
                    DateOfBirth = dateOfBirth,
                    Balance     = balance,
                };

                fileCabinetService.EditRecord(id, changedRecord);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// Edits a record.
        /// </summary>
        /// <param name="id">An ID of the editable record.</param>
        /// <param name="changedRecord">Changed record.</param>
        public void EditRecord(int id, FileCabinetRecordWithoutID changedRecord)
        {
            this.Validator.ValidateParameters(changedRecord);

            for (int i = 0; i < this.records.Count; i++)
            {
                if (this.records[i].Id == id)
                {
                    this.firstNameDictionary[this.records[i].FirstName.ToLower(CultureInfo.CurrentCulture)].Remove(this.records[i]);
                    this.lastNameDictionary[this.records[i].LastName.ToLower(CultureInfo.CurrentCulture)].Remove(this.records[i]);
                    this.dateOfBirthDictionary[this.records[i].DateOfBirth].Remove(this.records[i]);

                    int idOfTheRecord = this.records[i].Id;
                    this.records[i]    = new FileCabinetRecord(changedRecord);
                    this.records[i].Id = idOfTheRecord;

                    if (!this.firstNameDictionary.ContainsKey(this.records[i].FirstName.ToLower(CultureInfo.CurrentCulture)))
                    {
                        this.firstNameDictionary[this.records[i].FirstName.ToLower(CultureInfo.CurrentCulture)] = new List <FileCabinetRecord>();
                    }

                    if (!this.lastNameDictionary.ContainsKey(this.records[i].LastName.ToLower(CultureInfo.CurrentCulture)))
                    {
                        this.lastNameDictionary[this.records[i].LastName.ToLower(CultureInfo.CurrentCulture)] = new List <FileCabinetRecord>();
                    }

                    if (!this.dateOfBirthDictionary.ContainsKey(this.records[i].DateOfBirth))
                    {
                        this.dateOfBirthDictionary[this.records[i].DateOfBirth] = new List <FileCabinetRecord>();
                    }

                    this.firstNameDictionary[this.records[i].FirstName.ToLower(CultureInfo.CurrentCulture)].Add(this.records[i]);
                    this.lastNameDictionary[this.records[i].LastName.ToLower(CultureInfo.CurrentCulture)].Add(this.records[i]);
                    this.dateOfBirthDictionary[this.records[i].DateOfBirth].Add(this.records[i]);

                    return;
                }
            }

            throw new ArgumentException($"A user with id {id} doesn't exist.");
        }
示例#7
0
        private static void Create(string parameters)
        {
            try
            {
                Console.Write(FirstNameOutput);
                string firstName = ReadInput(StringConverter, FirstNameValidator);

                Console.Write(LastNameOutput);
                string lastName = ReadInput(StringConverter, LastNameValidator);

                Console.Write(SexOutput);
                char sex = ReadInput(CharConverter, SexValidator);

                Console.Write(WeightOutput);
                short weight = ReadInput(ShortConverter, WeightValidator);

                Console.Write(DateOfBirthOutput);
                DateTime dateOfBirth = ReadInput(DateConverter, DateOfBirthValidator);

                Console.Write(BalanceOutput);
                decimal balance = ReadInput(DecimalConverter, BalanceValidator);

                var newRecord = new FileCabinetRecordWithoutID()
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Sex         = sex,
                    DateOfBirth = dateOfBirth,
                    Weight      = weight,
                    Balance     = balance,
                };

                Console.WriteLine($"record #{fileCabinetService.CreateRecord(newRecord)} is created.");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(TryAgainOutput);
            }
        }
示例#8
0
        /// <summary>
        /// Creates a record.
        /// </summary>
        /// <param name="record">A record that contains information about the new record.</param>
        /// <returns>An ID of the new record.</returns>
        public int CreateRecord(FileCabinetRecordWithoutID record)
        {
            if (record == null)
            {
                throw new ArgumentNullException($"{nameof(record)} can't be null.");
            }

            int id;

            using (var writer = new BinaryWriter(this.stream, Encoding.Unicode, true))
            {
                id = ++this.lastId;

                this.stream.Seek(SizeOfShort, SeekOrigin.End);

                this.WriteRecordWithoutID(writer, id, record);

                this.stream.Seek(0, SeekOrigin.Begin);
            }

            return(id);
        }
        /// <summary>
        /// Creates a record.
        /// </summary>
        /// <param name="record">A record that contains information about the new record.</param>
        /// <returns>An ID of the new record.</returns>
        public int CreateRecord(FileCabinetRecordWithoutID record)
        {
            this.Validator.ValidateParameters(record);

            var newRecord = new FileCabinetRecord(record);

            if (this.records.Count == 0)
            {
                newRecord.Id = this.records.Count + 1;
            }
            else
            {
                newRecord.Id = this.records[this.records.Count - 1].Id + 1;
            }

            this.records.Add(newRecord);

            if (!this.firstNameDictionary.ContainsKey(record.FirstName.ToLower(CultureInfo.CurrentCulture)))
            {
                this.firstNameDictionary[record.FirstName.ToLower(CultureInfo.CurrentCulture)] = new List <FileCabinetRecord>();
            }

            if (!this.lastNameDictionary.ContainsKey(record.LastName.ToLower(CultureInfo.CurrentCulture)))
            {
                this.lastNameDictionary[record.LastName.ToLower(CultureInfo.CurrentCulture)] = new List <FileCabinetRecord>();
            }

            if (!this.dateOfBirthDictionary.ContainsKey(record.DateOfBirth))
            {
                this.dateOfBirthDictionary[record.DateOfBirth] = new List <FileCabinetRecord>();
            }

            this.firstNameDictionary[record.FirstName.ToLower(CultureInfo.CurrentCulture)].Add(newRecord);
            this.lastNameDictionary[record.LastName.ToLower(CultureInfo.CurrentCulture)].Add(newRecord);
            this.dateOfBirthDictionary[record.DateOfBirth].Add(newRecord);

            return(newRecord.Id);
        }