コード例 #1
0
ファイル: PhoneBook.cs プロジェクト: Varbanov/TelerikAcademy
        public void Add(Record record)
        {
            var firstname = record.FirstName != null ? record.FirstName.ToLower() : string.Empty;
            var middleName = record.MiddleName != null ? record.MiddleName.ToLower() : string.Empty;
            var lastname = record.LastName != null ? record.LastName.ToLower() : string.Empty;
            var town = record.Town != null ? record.Town.ToLower() : string.Empty;
            var phone = record.Phone != null ? record.Phone.ToLower() : string.Empty;

            this.FirstNames.Add(firstname, record);
            this.MiddleNames.Add(middleName, record);
            this.LastNames.Add(lastname, record);
            this.Towns.Add(town, record);
        }
コード例 #2
0
        private static  void PopulatePhoneBookFromFile(PhoneBook phonebook, string path)
        {
            var reader = new StreamReader(path);

            using (reader)
            {
                var line = reader.ReadLine();
                while (line != null)
                {
                    var recordParts = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    var names = recordParts[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Record record;

                    if (names.Length == 1)
                    {
                        record = new Record(names[0], null, null, recordParts[1], recordParts[2]);
                        phonebook.Add(record);
                    }
                    else if (names.Length == 2)
                    {
                        record = new Record(names[0], names[1], null, recordParts[1], recordParts[2]);
                        phonebook.Add(record);
                    }
                    else if (names.Length == 3)
                    {
                        record = new Record(names[0], names[1], names[2], recordParts[1], recordParts[2]);
                        phonebook.Add(record);
                    }
                    else
                    {
                        throw new FormatException("The file database is not correct");
                    }

                    line = reader.ReadLine();
                }
            }
        }