/// <summary> /// Interactively creates a new email address. /// </summary> /// <returns>The new email address.</returns> public static EmailAddress Create() { string typeOfEmailAddressAsString = MainFunctions.ReadLine("Type: (Personal/work/school/other) ").ToLower(); char firstLetter = 'h'; EmailAddressType typeOfEmailAddress = EmailAddressType.Personal; try { firstLetter = typeOfEmailAddressAsString.ToCharArray()[0]; switch (firstLetter) { case 'w': typeOfEmailAddress = EmailAddressType.Work; break; case 's': typeOfEmailAddress = EmailAddressType.School; break; case 'o': typeOfEmailAddress = EmailAddressType.Other; break; default: Console.WriteLine("Keeping default of Personal"); break; } } catch (IndexOutOfRangeException) { Console.WriteLine("Keeping default of Personal"); } string address = ""; while (true) { string emailAddressString = MainFunctions.ReadLine("Email Address: "); try { MailAddress mailAddress = new MailAddress(emailAddressString); } catch (FormatException) { Console.Write("Invalid email address. Press any key to retry..."); Console.ReadKey(); Console.Clear(); continue; } catch (ArgumentException) { Console.Write("Invalid email address. Press any key to retry..."); Console.ReadKey(); Console.Clear(); continue; } address = emailAddressString; break; } EmailAddress newEmailAddress = new EmailAddress(typeOfEmailAddress, address); return(newEmailAddress); }
/// <summary> /// Interactively creates a new contact. /// </summary> /// <returns>The new contact.</returns> public static Contact Create() { string firstName = MainFunctions.GetAndValidateInput("First Name", RegexPatterns.Name); string lastName = MainFunctions.GetAndValidateInput("Last Name", RegexPatterns.Name); string fullName = string.Format("{0} {1}", firstName, lastName); Contact newContact = new Contact(fullName); bool addAddresses = !MainFunctions.InputStartsWith("Add addresses? (Y/n) ", "n"); if (addAddresses) { while (true) { Console.Clear(); Address newAddress = Address.Create(); Console.WriteLine(newAddress); bool addressOK = !MainFunctions.InputStartsWith("Is this address OK? (Y/n) ", "n"); if (addressOK) { newContact.Addresses.Add(newAddress); Console.WriteLine("Added new address to new contact's set of addresses."); } else { Console.WriteLine("Discarded new address."); } bool addAnother = !MainFunctions.InputStartsWith("Add another? (Y/n) ", "n"); if (!addAnother) { break; } } } bool addPhoneNumbers = !MainFunctions.InputStartsWith("Add phone numbers? (Y/n) ", "n"); if (addPhoneNumbers) { while (true) { Console.Clear(); PhoneNumber newPhoneNumber = PhoneNumber.Create(); Console.WriteLine(newPhoneNumber); bool addressOK = !MainFunctions.InputStartsWith("Is this phone number OK? (Y/n) ", "n"); if (addressOK) { newContact.PhoneNumbers.Add(newPhoneNumber); Console.WriteLine("Added new phone number to new contact's set of phone numbers."); } else { Console.WriteLine("Discarded new phone number."); } bool addAnother = !MainFunctions.InputStartsWith("Add another? (Y/n) ", "n"); if (!addAnother) { break; } } } bool addFaxNumbers = !MainFunctions.InputStartsWith("Add fax numbers? (Y/n) ", "n"); if (addFaxNumbers) { while (true) { Console.Clear(); FaxNumber newFaxNumber = FaxNumber.Create(); Console.WriteLine(newFaxNumber); bool addressOK = !MainFunctions.InputStartsWith("Is this fax number OK? (Y/n) ", "n"); if (addressOK) { newContact.FaxNumbers.Add(newFaxNumber); Console.WriteLine("Added new fax number to new contact's set of fax numbers."); } else { Console.WriteLine("Discarded new fax number."); } bool addAnother = !MainFunctions.InputStartsWith("Add another? (Y/n) ", "n"); if (!addAnother) { break; } } } bool addEmailAddresses = !MainFunctions.InputStartsWith("Add email addresses? (Y/n) ", "n"); if (addEmailAddresses) { while (true) { Console.Clear(); EmailAddress newEmailAddress = EmailAddress.Create(); Console.WriteLine(newEmailAddress); bool emailAddressOK = !MainFunctions.InputStartsWith("Is this email address OK? (Y/n) ", "n"); if (emailAddressOK) { newContact.EmailAddresses.Add(newEmailAddress); Console.WriteLine("Added new email address to new contact's set of email addresses."); } else { Console.WriteLine("Discarded new email address."); } bool addAnother = !MainFunctions.InputStartsWith("Add another? (Y/n) ", "n"); if (!addAnother) { break; } } } return(newContact); }