public void DuplicateEntriesNotAdd() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0744596866"); Assert.AreEqual(1, phoneBook.Count()); }
public void Add() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "5555"); Assert.AreEqual(2, phoneBook.Count()); }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); var maximumParameters = parameters.ParametersLength(); if (consoleInput.Parameters.Length >= minimumParameters && consoleInput.Parameters.Length <= maximumParameters) { var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); string index = consoleInput[0]; if (phoneBook.ValidIndex(index)) { KeyValuePair<string, Contact> currentItem = phoneBook.GetElementTo(index); ////Console.WriteLine("{0} {1} {2}", new object[] { index, currentItem.Value.Name, currentItem.Value.PhoneNumber }); message = string.Format("{0} {1} {2} {3}", index, currentItem.Value.Name, currentItem.Value.PhoneNumber, currentItem.Value.BirthDate); } else message = string.Format("Your input '{0}' is not valid.", index); } else { message = string.Format("This command requires 1 mandatory parameter"); message += System.Environment.NewLine; message += string.Format("{0}", string.Join(" ", parameters.Mandatory()[0].Name)); } Console.WriteLine(Message()); } }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); var maximumParameters = parameters.ParametersLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); if (consoleInput.Parameters.Length >= minimumParameters && consoleInput.Parameters.Length <= maximumParameters) { var name = consoleInput[0]; var phone = consoleInput[1]; var born = ValidateDate(mandatoryAndOptional); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); if (phoneBook.Add(name: name, phone: phone, birthdate: born)) { fileIO.WriteFile(ref phoneBook); if (born != string.Empty) { var bornT = new BirthDate(born); bornT.IsValid(new string[] { "yyyyMMdd" }); born = bornT.Format("yyyy-MM-dd"); } message = string.Format("Added: {0} => {1} => {2}", name, phone, born); } else { message = string.Format("Cannot add." + System.Environment.NewLine + "Reason: Duplicate name/phone for {0} => {1}", name, phone); } ////Console.WriteLine("Total phone numbers: {0}", phoneBook.Count()); message += System.Environment.NewLine; message += string.Format("Total phone numbers: {0}", phoneBook.Count()); } else { message += string.Format("This command requires:"); message += System.Environment.NewLine; message += string.Format("- mandatory parameters: {0}, {1}", parameters.Mandatory()[0].Name, parameters.Mandatory()[1].Name); message += System.Environment.NewLine; message += string.Format("- optional parameters: {0}", parameters.Optional()[0].Name); message += System.Environment.NewLine; message += string.Format("Command format: add {0}", string.Join(" ", parameters.Original)); } Console.WriteLine(Message()); } }
public void FilterByPartialPhone_OneResult() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516", "3gd"); phoneBook.Add("Samuel", "444"); string partialName = "55"; string pattern = @"(" + partialName + @")"; phoneBook.FilterBy(pattern, "phone"); Assert.AreEqual(1, phoneBook.Count()); }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); int counted = phoneBook.Count(); ////Console.WriteLine("Found {0} results.", counted); message = string.Format("Found {0} results.", counted); } Console.WriteLine(Message()); }
public void WriteFile(ref PhoneBook phoneBook) { try { using (StreamWriter sw = new StreamWriter(fileName)) { foreach (KeyValuePair<string, Contact> currentItem in phoneBook) { string[] dataToSave = { currentItem.Key, currentItem.Value.Name, currentItem.Value.PhoneNumber, currentItem.Value.BirthDate }; sw.WriteLine(SingleLine(string.Join(dataSeparator, dataToSave))); } } } catch (Exception e) { //Console.WriteLine("The file could not be write:"); //Console.WriteLine(e.Message); } }
public void StubReadFile(ref PhoneBook phoneBook) { try { using (StreamReader sr = new StreamReader(memoryStream)) { string line; while ((line = sr.ReadLine()) != null) { string[] splittedLine = Regex.Split(MultipleLines(line.ToString()), dataSeparator); phoneBook.Add(splittedLine[0], splittedLine[1]); } } } catch (Exception) { // WriteFile(ref phoneBook); } }
public void ReadFile(ref PhoneBook phoneBook) { try { using (StreamReader sr = new StreamReader(fileName)) { string line; while ((line = sr.ReadLine()) != null) { string[] splittedLine = Regex.Split(MultipleLines(line), dataSeparator); if (splittedLine.Length == 3) Array.Resize(ref splittedLine, 4); phoneBook.Add(name: splittedLine[1], phone: splittedLine[2], birthdate: splittedLine[3], defaultKey: splittedLine[0]); } } } catch (Exception) { WriteFile(ref phoneBook); //Console.WriteLine("A new file has been created."); } }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); var maximumParameters = parameters.ParametersLength(); if (consoleInput.Parameters.Length >= minimumParameters && consoleInput.Parameters.Length <= maximumParameters) { var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); string index = consoleInput[0]; var currentItem = new KeyValuePair<string, Contact>(); if (phoneBook.RemoveAt(index, out currentItem)) { // KeyValuePair<string, string> currentItem = phoneBook.GetElementTo(index); // phoneBook.Remove(currentItem); fileIO.WriteFile(ref phoneBook); ////Console.WriteLine("The contact '{0} => {1}: {2}' has been deleted.", new object[] { currentItem.Key, currentItem.Value.Name, currentItem.Value.PhoneNumber }); message = string.Format("The contact '{0} => {1}: {2}' has been deleted.", currentItem.Key, currentItem.Value.Name, currentItem.Value.PhoneNumber); } else ////Console.WriteLine("Your input must be an integer between 0 and {0}", new object[] { phoneBook.Count() }); message = string.Format("Your input '{0}' is not valid.", index); } else { message = string.Format("This command requires 1 mandatory parameter"); message += System.Environment.NewLine; message += string.Format("{0}", string.Join(" ", parameters.Mandatory()[0].Name)); } Console.WriteLine(Message()); } }
public ListConsole(PhoneBook phoneBook, string filterType) { if (filterType != string.Empty) Console.WriteLine("Contacts filtered by birthday " + filterType + "." + Environment.NewLine); var design = new TableDesign(); Console.WriteLine(string.Format("INDEX" + design.SpaceLength(8, 5) + "| NAME" + design.SpaceLength(30, 4) + "| PHONE" + design.SpaceLength(20, 5) + "| BIRTHDATE")); Console.WriteLine(string.Format(design.SpaceLength(90, 0, "-"))); foreach (KeyValuePair<string, Contact> currentItem in phoneBook) { string index = currentItem.Key; string name = currentItem.Value.Name; string phone = currentItem.Value.PhoneNumber; string born = MakeDateLength(currentItem.Value.BirthDate); Console.WriteLine(string.Format( "{0}" + design.SpaceLength(8, index.ToString().Length) + "| {1}" + design.SpaceLength(30, name.Length) + "| {2}" + design.SpaceLength(20, phone.Length) + "| {3}", index, name, phone, born.Substring(0, 4) + "-" + born.Substring(4, 2) + "-" + born.Substring(6, 2) + AddBornMessageIfExist(born))); } }
private void ModifyContactData() { phoneBook = new PhoneBook(); fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); message += System.Environment.NewLine; if (!ModifyContact()) message += string.Format("The ID '{0}' does not exist!", inputParameters[0].Value); message += Environment.NewLine; }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var minimumParameters = parameters.MandatoryLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); inputParameters = mandatoryAndOptional.Mandatory(); if (inputParameters.Length >= minimumParameters) { string findWord = inputParameters[0].Value; string pattern = @"(" + findWord + @")"; var design = new TableDesign(); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); phoneBook.Backup(); phoneBook.FilterBy(pattern, "phone"); int counted = phoneBook.Count(); if (counted == 0) { Console.WriteLine(string.Format("No result.")); } else { Console.WriteLine(string.Format("INDEX" + design.SpaceLength(8, 5) + "| NAME" + design.SpaceLength(30, 4) + "| PHONE")); Console.WriteLine(string.Format(design.SpaceLength(50, 0, "-"))); var contactNo = 0; foreach (KeyValuePair<string, Contact> currentItem in phoneBook) { if (contactNo > 0) { Console.WriteLine(); } string index = currentItem.Key; string name = currentItem.Value.Name; string phone = currentItem.Value.PhoneNumber; string lineString = string.Format("{0}" + design.SpaceLength(8, index.ToString().Length) + "| {1}" + design.SpaceLength(30, name.Length) + "| {2}", index, name, phone); string[] splittedLine = lineString.Split(new string[] { findWord }, StringSplitOptions.None); var wordNo = 0; foreach (string split in splittedLine) { if (wordNo > 0) { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.White; Console.Write(findWord); Console.ResetColor(); } Console.Write(split); wordNo++; } contactNo++; } } phoneBook.Restore(); } else Console.WriteLine(Help()); message += "it's done"; } }
public void Count() { PhoneBook phoneBook = new PhoneBook(); Assert.AreEqual(0, phoneBook.Count()); }
public void RemoveAt() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516", "3gd"); phoneBook.Add("Samuel", "444"); phoneBook.RemoveAt("3gd"); Assert.AreEqual(2, phoneBook.Count()); }
public void List() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516"); phoneBook.Add("Samuel", "444"); int countedContacts = 0; foreach (KeyValuePair<string, Contact> currentItem in phoneBook) { countedContacts++; } Assert.AreEqual(countedContacts, phoneBook.Count()); }
public void ModifyNameWhenKeyDoesNotExist() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516", "3gd"); KeyValuePair<string, Contact> contactToCompare = new KeyValuePair<string, Contact>("3gd", new Contact("Johnny 2", "0745516")); KeyValuePair<string, Contact> contactToModify = phoneBook.Modify(id: "3gd", name: "Johnny"); Assert.AreNotEqual(contactToCompare, contactToModify); }
public bool StubWriteFile(ref PhoneBook phoneBook) { return true; }
public void ListBirthdayTomorrow() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("John Smith", "0745516", "20120327", "rty"); phoneBook.Add("Nick", "0744596866", "20120326", "gyu"); phoneBook.Add("Samuel", "444", "20120327", "fgh"); phoneBook.Backup(); DateTime today = new DateTime(2016, 03, 26); var startRange = today.AddDays(1); var endRange = today.AddDays(1); DateTime[] birthdayRange; birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); Assert.AreEqual(2, phoneBook.Count()); }
public void ListBirthdayThisWeek() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("John Smith", "0745516", "20160329", "rty"); // Tuesday phoneBook.Add("Nick", "0744596866", "20120328", "gyu"); // Monday phoneBook.Add("Samuel", "444", "20100326", "iop"); // Sunday phoneBook.Backup(); DateTime today = new DateTime(2016, 04, 02); var dayOfWeek = (int)today.DayOfWeek; var startRange = today.AddDays(0 - dayOfWeek); var endRange = today.AddDays(6 - dayOfWeek); DateTime[] birthdayRange; birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); Assert.AreEqual(2, phoneBook.Count()); }
public void AccessWithoutOrEmptyOrNullArguments() { /*PhoneBook phoneBook = new PhoneBook(); PhoneBook phoneBook2 = new PhoneBook(null);*/ PhoneBook phoneBook3 = new PhoneBook(new string[] { }); }
public void ListBirthdayNextWeek() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("John Smith 2", "0745516", "20160329", "bnm"); // Tuesday phoneBook.Add("Nickk", "0744596866", "20120328", "bbb"); // Monday phoneBook.Add("Samuelo", "44334", "20120403", "vdf"); // Sunday => next week phoneBook.Backup(); DateTime today = new DateTime(2016, 03, 28); var dayOfWeek = (int)today.DayOfWeek; var startRange = today.AddDays(0 - dayOfWeek + 7); var endRange = today.AddDays(6 - dayOfWeek + 7); DateTime[] birthdayRange; birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); Assert.AreEqual(1, phoneBook.Count()); }
public void Execute(string[] args) { ConsoleInput consoleInput = new ConsoleInput(args, name); if (consoleInput.IsValidCommandName()) { ActivateParameters(); var phoneBook = new PhoneBook(); var fileIO = new InputOutput("PhoneBook.txt"); fileIO.ReadFile(ref phoneBook); var maximumParameters = parameters.OptionalLength(); Parameters mandatoryAndOptional = new Parameters(consoleInput.Parameters); inputParameters = mandatoryAndOptional.Mandatory(); if (HasMaximumRequiredParameters(maximumParameters)) { var po = parameters.ParametersAreValid("optional", inputParameters); if (!po) Console.WriteLine("OPTIONAL parameters are not ok!"); if (!po) { Console.WriteLine("Command usage: " + Help()); } else { phoneBook.Backup(); string filterType = string.Empty; if (inputParameters.Length > 0) { DateTime[] birthdayRange; DateTime today = DateTime.UtcNow.ToLocalTime(); var dayOfWeek = (int)today.DayOfWeek; var startRange = today; var endRange = today; filterType = inputParameters[0].Value; ////Console.WriteLine("UTC: " + today); ////Console.WriteLine("Local: " + today.ToLocalTime()); switch (filterType) { case "tomorrow": startRange = today.AddDays(1); endRange = today.AddDays(1); break; case "thisweek": startRange = today.AddDays(0 - dayOfWeek); endRange = today.AddDays(6 - dayOfWeek); break; case "nextweek": startRange = today.AddDays(0 - dayOfWeek + 7); endRange = today.AddDays(6 - dayOfWeek + 7); break; default: filterType = "today"; break; } birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); } if (phoneBook.Count() == 0) { if (filterType != string.Empty) Console.WriteLine(string.Format("There is not contact which has the birthday {0}.", new object[] { filterType })); else Console.WriteLine("The phonebook is empty."); } else new ListConsole(phoneBook, filterType); phoneBook.Restore(); } } message = "done"; } }
public void ModifyPhone() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516", "3gd"); KeyValuePair<string, Contact> contactToCompare = new KeyValuePair<string, Contact>("3gd", new Contact("John Smith", "0744578999")); KeyValuePair<string, Contact> contactToModify = phoneBook.Modify(id: "3gd", phoneNumber: "0744578999"); Assert.AreEqual(contactToCompare, contactToModify); }
public void ListBirthdayToday() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("John Smith", "0745516", "20120327", "rty"); phoneBook.Add("Nick", "0744596866", "20120326", "gyu"); phoneBook.Add("Samuel", "444"); ////phoneBook.Backup(); DateTime today = new DateTime(2016, 03, 26); var startRange = today; var endRange = today; DateTime[] birthdayRange; birthdayRange = new DateTime[] { startRange, endRange }; phoneBook.BirthdayRange(birthdayRange); ////DateTime today2; ////Assert.AreEqual(true, phoneBook.DateIsValid("20120326", out today2)); ////Assert.AreEqual(true, phoneBook.DayInRange(today, startRange, endRange)); Assert.AreEqual(1, phoneBook.Count()); }
public void GetElementToFalse() { PhoneBook phoneBook = new PhoneBook(); phoneBook.Add("Nick", "0744596866"); phoneBook.Add("John Smith", "0745516", "3gd"); phoneBook.Add("Samuel", "444"); KeyValuePair<string, Contact> contactToCompare = new KeyValuePair<string, Contact>("3gd", new Contact("John Smith 2", "0745516")); KeyValuePair<string, Contact> contactToRetrieve = phoneBook.GetElementTo("3gd"); Assert.AreNotEqual(contactToCompare, contactToRetrieve); }