/// <summary> /// Lists the entries in Phone Book in given range. /// If out of range exception it throwed. /// </summary> /// <param name="startRange">The start range.</param> /// <param name="endRange">The end range.</param> /// <returns>Returns orderd list of Phone Book Entries</returns> public PhoneBookContactOutput[] ListEntries(int startRange, int endRange) { if (startRange < 0 || startRange + endRange > this.PhoneBookEntries.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } this.PhoneBookEntries.Sort(); PhoneBookContactOutput[] ent = new PhoneBookContactOutput[endRange]; for (int i = startRange; i <= startRange + endRange - 1; i++) { PhoneBookContactOutput entry = this.PhoneBookEntries[i]; ent[i - startRange] = entry; } return(ent); }
/// <summary> /// Adds contact to Phone Book. /// Method checks if there is allready existing contact in PhoneBookEntries. /// If contact name exists only its phone numbers are added. /// </summary> /// <param name="contactName">The contact name</param> /// <param name="contactNumbers">The numbers of contact</param> /// <returns>Returns bool. True is returned if contact is added to Phone Book</returns> public bool AddPhone(string contactName, IEnumerable <string> contactNumbers) { var old = from e in this.PhoneBookEntries where e.Name.ToLowerInvariant() == contactName.ToLowerInvariant() select e; bool isPhoneAdded = false; if (old.Count() == 0) { PhoneBookContactOutput obj = new PhoneBookContactOutput(); obj.Name = contactName; obj.contactPhones = new SortedSet <string>(); foreach (var phone in contactNumbers) { obj.contactPhones.Add(phone); } this.PhoneBookEntries.Add(obj); isPhoneAdded = true; } else if (old.Count() == 1) { PhoneBookContactOutput obj2 = old.First(); foreach (var phone in contactNumbers) { obj2.contactPhones.Add(phone); } isPhoneAdded = false; } else { throw new DuplicateWaitObjectException("Duplicated name in the phonebook found: " + contactName); } return(isPhoneAdded); }