public bool AddPhone(string name, IEnumerable<string> phoneNumbers) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Phone entry name cannot be null or empty."); } if (phoneNumbers == null) { throw new NullReferenceException("Phone numbers to add to phone entry cannot be null."); } string nameToLowerCase = name.ToLowerInvariant(); IPhoneEntry phoneEntry; bool isNewEntry = !this.entriesByName.TryGetValue(nameToLowerCase, out phoneEntry); if (isNewEntry) { phoneEntry = new PhoneEntry(name); this.entriesByName.Add(nameToLowerCase, phoneEntry); this.sortedEntries.Add(phoneEntry); } // TODO: Extract method foreach (var phoneNumber in phoneNumbers) { this.entriesByPhone.Add(phoneNumber, phoneEntry); } phoneEntry.PhoneNumbers.UnionWith(phoneNumbers); return isNewEntry; }
public PhoneEntry[] ListEntries(int startIndex, int count) { if (startIndex < 0 || startIndex + count > this.phoneByPerson.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } PhoneEntry[] sortedPhoneEntries = new PhoneEntry[count]; for (int i = startIndex; i <= startIndex + count - 1; i++) { PhoneEntry entry = this.sorted[i]; sortedPhoneEntries[i - startIndex] = entry; } return sortedPhoneEntries; }
public PhoneEntry[] ListEntries(int startIndex, int count) { if (startIndex < 0 || startIndex + count > this.entries.Count) { throw new ArgumentException("Invalid range"); } PhoneEntry[] list = new PhoneEntry[count]; for (int i = startIndex; i < startIndex + count; i++) { list[i - startIndex] = this.entries[i]; } return list; }
public PhoneEntry[] ListEntries(int start, int num) { if (start < 0 || start + num > this.phoneEntries.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } this.phoneEntries.Sort(); PhoneEntry[] ent = new PhoneEntry[num]; for (int i = start; i <= start + num - 1; i++) { PhoneEntry entry = this.phoneEntries[i]; ent[i - start] = entry; } return ent; }
public bool AddPhone(string personName, IEnumerable<string> phoneNumbers) { string name2 = personName.ToLowerInvariant(); PhoneEntry newPhoneEntry; bool flag = !this.phoneByPerson.TryGetValue(name2, out newPhoneEntry); if (flag) { newPhoneEntry = new PhoneEntry(); newPhoneEntry.PersonName = personName; newPhoneEntry.PhoneNumbers = new SortedSet<string>(); this.phoneByPerson.Add(name2, newPhoneEntry); this.sorted.Add(newPhoneEntry); } newPhoneEntry.PhoneNumbers.UnionWith(phoneNumbers); return flag; }
public bool AddPhone(string name, params string[] phones) { string nameToLower = name.ToLowerInvariant(); bool nameDoesNotExist = !this.phoneEntriesByName.ContainsKey(nameToLower); if (nameDoesNotExist) { var entryToAdd = new PhoneEntry(name, new SortedSet<string>()); this.phoneEntriesByName.Add(nameToLower, entryToAdd); this.entries.Add(entryToAdd); } var entry = this.phoneEntriesByName[nameToLower]; foreach (var phone in phones) { this.phoneEntriesByPhone.Add(phone, entry); } entry.Phones.UnionWith(phones); return nameDoesNotExist; }
public bool AddPhone(string personName, IEnumerable<string> phoneNumbers) { var oldPhoneEntry = from entry in this.phoneEntries where entry.PersonName.ToLowerInvariant() == personName.ToLowerInvariant() select entry; bool isFirstEntryForPerson; if (oldPhoneEntry.Count() == 0) { PhoneEntry newPhoneEntry = new PhoneEntry(); newPhoneEntry.PersonName = personName; newPhoneEntry.PhoneNumbers = new SortedSet<string>(); foreach (var number in phoneNumbers) { newPhoneEntry.PhoneNumbers.Add(number); } this.phoneEntries.Add(newPhoneEntry); isFirstEntryForPerson = true; } else if (oldPhoneEntry.Count() == 1) { PhoneEntry newPhoneEntry = oldPhoneEntry.First(); foreach (var num in phoneNumbers) { newPhoneEntry.PhoneNumbers.Add(num); } isFirstEntryForPerson = false; } else { Console.WriteLine("Duplicated name in the phonebook found: " + personName); return false; } return isFirstEntryForPerson; }
public bool AddPhone(string name, IEnumerable<string> phoneNumbers) { var old = from e in this.entries where e.Name.ToLowerInvariant() == name.ToLowerInvariant() select e; bool flag; if (old.Count() == 0) { var obj = new PhoneEntry(name); foreach (var num in phoneNumbers) { obj.PhoneNumbers.Add(num); } this.entries.Add(obj); flag = true; } else if (old.Count() == 1) { IPhoneEntry obj2 = old.First(); foreach (var num in phoneNumbers) { obj2.PhoneNumbers.Add(num); } flag = false; } else { Console.WriteLine("Duplicated name in the phonebook found: " + name); return false; } return flag; }