private static void Cmd(string cmd, string[] strings) { if (cmd == "Cmd3") { // first command string str0 = strings[0]; var str1 = strings.Skip(1).ToList(); for (int i = 0; i < str1.Count; i++) { str1[i] = conv(str1[i]); } var personData = new PersonInfo(str0, str1); bool flag = data.AddPhone(str0, str1); if (flag) { Print("Phone entry created."); } else { Print("Phone entry merged"); } } else if (cmd == "Cmd2") { // second command Print(string.Empty + data.ChangePhone(conv(strings[0]), conv(strings[1])) + " numbers changed"); } else { // third command try { IEnumerable<PersonInfo> entries = data.ListEntries(int.Parse(strings[0]), int.Parse(strings[1])); foreach (var entry in entries) { Print(entry.ToString()); } } catch (ArgumentOutOfRangeException) { Print("Invalid range"); } } }
public PersonInfoModel[] ListEntries(int start, int num) { if (start < 0 || start + num > this.entries.Count) { throw new ArgumentOutOfRangeException("start", "Invalid start index or count."); } this.entries.Sort(); var ent = new PersonInfo[num]; for (int i = start; i <= start + num - 1; i++) { PersonInfo entry = this.entries[i]; ent[i - start] = entry; } return ent; }
public PersonInfoModel[] ListEntries(int first, int num) { if (first < 0 || first + num > this.dict.Count) { Console.WriteLine("Invalid start index or count."); return null; } PersonInfo[] list = new PersonInfo[num]; for (int i = first; i <= first + num - 1; i++) { PersonInfo entry = this.sorted[i]; list[i - first] = entry; } return list; }
public bool AddPhone(PersonInfoModel model) { string name2 = model.Name.ToLowerInvariant(); PersonInfoModel entry; bool flag = !this.dict.TryGetValue(name2, out entry); if (flag) { entry = new PersonInfo(); entry.Name = model.Name; entry.Phones = new SortedSet<string>(); this.dict.Add(name2, entry); this.sorted.Add(entry); } foreach (var num in nums) { this.multidict.Add(num, entry); } entry.Phones.UnionWith(nums); return flag; }