/// <summary> /// Allows the user to enter new contacts. /// </summary> /// <returns>The enter contacts.</returns> public static void UserEnterContacts(List <Contact> contacts) { // continue entering new contacts as long as user wishes // prompt if user wants to enter another contact string anotherUser = ""; // used for entering another conatct after successful entry do { Console.Clear(); Contact newContact = new Contact(); /// <COPY> /// create deep copy of contact in case of bad input /// </COPY> Contact tempCopy = newContact.DeepCopy(); /// <SEARCH ID> /// find available ID /// </SEARCH ID> // edited by Garrett Rathke on 10/05/17 per Assignment 4 instructions // automatically calculate ID // here I assume that the contact list should be able to grow beyond 50,000 // I also assume that the contact list will not need to be reorganized if ever a contact // is deleted, freeing up space in the list // In the case where there is an available ID somewhere in the list that is not at the end of the list // the new contact will just be assigned that available slot int startingID = 1; tempCopy.ID = startingID; bool IDisAvailable = false; // reassure user the program has not crashed Console.WriteLine("Working on Finding Available ID"); Console.WriteLine("This may take a few minuties...Please Wait..."); while (IDisAvailable == false) { // loops through contact list & checks if any ID is available // if the loop reaches the end of the contact list & no ID is available // then the new contact is put at the end of the list...now the list has increased in size by 1 if (ContactQuery.FindByID(contacts, startingID) != null) { startingID++; } else { IDisAvailable = true; } } // now we have an available ID tempCopy.ID = startingID; Console.WriteLine("Enter contacts. Leave name blank to stop entering..."); Console.WriteLine("You have {0} contacts in your list." , contacts.Count.ToString(Display.INT_DISPLAY_FORMAT)); // don't allow user to enter new contact info with duplicate names & phone #s // don't allow user to enter new contact info with bad phone, email, or url data string doAgain = ""; // if bad data, and user wants to reeneter data do { string input = ""; Console.Write("First Name: "); tempCopy.FirstName = Console.ReadLine(); if (tempCopy.FirstName.Trim().Length == 0) { //stop entering contacts break; } // continue entering rest of fields... Console.Write("Last Name: "); tempCopy.LastName = Console.ReadLine(); Console.Write("Street Address: "); tempCopy.Address = Console.ReadLine(); Console.Write("City: "); tempCopy.City = Console.ReadLine(); Console.Write("State: "); tempCopy.State = Console.ReadLine(); Console.Write("Zip: "); tempCopy.Zip = Console.ReadLine(); Console.Write("County: "); tempCopy.County = Console.ReadLine(); Console.Write("Company: "); tempCopy.CompanyName = Console.ReadLine(); do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Phone1: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUSPhoneNumber(input)) { tempCopy.phone1IsValid = true; tempCopy.Phone1 = input; doAgain = "N"; // just in case } else { tempCopy.phone1IsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Phone Number Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter phone number 1 again? Y/N \n"); } } else { tempCopy.phone1IsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check phone 1 valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.phone1IsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Phone2: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUSPhoneNumber(input)) { tempCopy.phone2IsValid = true; tempCopy.Phone2 = input; doAgain = "N"; // just in case } else { tempCopy.phone2IsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Phone Number Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter phone number 2 again? Y/N \n"); } } else { tempCopy.phone2IsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check phone 2 valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.phone2IsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Email: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidEmail(input)) { tempCopy.emailIsValid = true; tempCopy.Email = input; doAgain = "N"; // just in case } else { tempCopy.emailIsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Email Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter email again? Y/N \n"); } } else { tempCopy.emailIsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check email valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.emailIsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Web Page: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUrl(input)) { tempCopy.urlIsValid = true; tempCopy.Web = input; doAgain = "N"; // just in case } else { tempCopy.urlIsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid URL Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter URL again? Y/N \n"); } } else if (input.Trim().Length <= 0) { tempCopy.urlIsValid = true; // user doesn't want contact to have a website doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check URL valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.urlIsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } // check if user already exists with same first & last names, & same phone1 & phone 2 List <Contact> duplicateNames = ContactQuery.FindByFirstAndLastName(contacts, tempCopy.FirstName, tempCopy.LastName); List <Contact> duplicatePhones = ContactQuery.FindByPhoneNumbers1And2(contacts, tempCopy.Phone1, tempCopy.Phone2); // queries found duplicate names and phone numbers if (duplicateNames.Count > 0 && duplicatePhones.Count > 0) { Console.WriteLine("Duplicate Names and Phone Numbers NOT ALLOWED...Cannot Create Contact"); doAgain = Display.GetUserInput("Would you like to reenter the names and phone numbers?"); } else { break; // no duplicates } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // no duplicates & correct data format // save edits to original if ((tempCopy.dataIsValid == true) && (!(doAgain.Contains("Y")) || (doAgain.Contains("y")))) { newContact.ID = tempCopy.ID; newContact.FirstName = tempCopy.FirstName; newContact.LastName = tempCopy.LastName; newContact.Address = tempCopy.Address; newContact.City = tempCopy.City; newContact.State = tempCopy.State; newContact.Zip = tempCopy.Zip; newContact.County = tempCopy.County; newContact.CompanyName = tempCopy.CompanyName; newContact.Phone1 = tempCopy.Phone1; newContact.Phone2 = tempCopy.Phone2; newContact.Email = tempCopy.Email; newContact.Web = tempCopy.Web; contacts.RemoveAt(tempCopy.ID); // delete copy Console.Clear(); Console.WriteLine("New Contact Info: "); // display new contact to the user Display.DisplayContact(newContact); Display.Pause(); contacts.Add(newContact); } else { contacts.RemoveAt(tempCopy.ID); // delete copy } anotherUser = Display.Pause("Do you want to enter another new contact? Y/N \n"); } while (anotherUser.Contains("Y") || anotherUser.Contains("y")); } // end of public static void UserEnterContacts method
/// <summary> /// Switches to user query menu. /// </summary> public static void SwitchToUserQueryMenu() { string selection = ""; try { // Stay in this menu until user wants // to go back to main menu while (selection != "r") { // display the query menu options to the user selection = Display.DisplayQueryMenu(contacts.Count).Trim().ToLower(); switch (selection) { case "1": // find by last name //let user type name to search for string val = Display.GetUserInput("Enter last name to search: ").Trim(); if (val.Length > 0) { // search for and display results List <Contact> results = ContactQuery.SearchLastName(contacts, val); DisplayQueryResults(results); } break; case "2": //Find contacts with ID between (starting and ending number) //let user type name to search for string idVal1 = " "; string idVal2 = " "; int id1 = 0; int id2 = 0; int tries = 0; //get starting id while (tries <= 5 && (!Validation.IsNumeric(idVal1, ref id1) || idVal1 == "")) { tries++; idVal1 = Display.GetUserInput("Enter STARTING ID: ").Trim(); } if (tries <= 5) { tries = 0; // get ending id (only if starting id is good) while (tries <= 5 && (!Validation.IsNumeric(idVal2, ref id2) || idVal2 == "")) { idVal2 = Display.GetUserInput("Enter ENDING ID: ").Trim(); tries++; } } if (id2 <= id1) { Display.Pause("The first ID value must be less than the second ID value."); break; } else { // search for and display results List <Contact> results = ContactQuery.SearchIdRange(contacts, id1, id2); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "3": //Find contacts with common email domain (like: .aol, .gmail, .whatever) // trim removes leading &/or trailing whitespace string emailDomain = Display.GetUserInput("Enter email domain to search: ").Trim(); if (!Validation.IsValidUrl(emailDomain)) { Console.WriteLine("Invalid URL"); } else { List <Contact> results = ContactQuery.FindByEmailDomain(contacts, emailDomain); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "4": // Find contacts within zip string zip = Display.GetUserInput("Enter zip code to search: ").Trim(); if (zip.Length > 5 || zip.Length < 0) { Console.WriteLine("Invalid Zip Code"); } else { List <Contact> results = ContactQuery.FindByZip(contacts, zip); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "5": // Find contacts within state string state = Display.GetUserInput("Enter state to search: ").Trim(); if (state.Length > 2 || state.Length < 0) { Console.WriteLine("Invalid State Format"); } else { List <Contact> results = ContactQuery.FindByState(contacts, state); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "6": //Find contacts with first and last name string firstName = Display.GetUserInput("Enter first name to search: ").Trim(); string lastName = Display.GetUserInput("Enter last name to search: ").Trim(); if (!(firstName.Length > 0 || lastName.Length > 0)) { Console.WriteLine("Invalid Name Format"); } else { List <Contact> results = ContactQuery.FindByFirstAndLastName(contacts, firstName, lastName); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "7": //Find contacts with (first name, last name, and city) Console.WriteLine("Search for substring within first & last names..."); firstName = Display.GetUserInput("Enter first name to search: ").Trim(); lastName = Display.GetUserInput("Enter last name to search: ").Trim(); string city = Display.GetUserInput("Enter city to search: ").Trim(); if (!(firstName.Length > 0 || lastName.Length > 0 || city.Length > 0)) { Console.WriteLine("Invalid Name Format"); } else { List <Contact> results = ContactQuery.FindByFirstLastNameAndCity(contacts, firstName, lastName, city); DisplayQueryResults(results); } break; // edited by Garrett Rathke on 10/03/17 per assignment 4 instructions case "8": //Find contacts with substrings in (first name, last name) firstName = Display.GetUserInput("Enter first name to search: ").Trim(); lastName = Display.GetUserInput("Enter last name to search: ").Trim(); if (!(firstName.Length > 0 || lastName.Length > 0)) { Console.WriteLine("Invalid Name Format"); } else { List <Contact> results = ContactQuery.FindBySubStringInFirstAndLastName(contacts, firstName, lastName); DisplayQueryResults(results); } break; } } } catch (Exception e) { Display.Pause("Error: " + e.Message); } }
/// <summary> /// Users the edit contacts. /// </summary> /// <param name="contacts">Contacts.</param> public static void UserEditContacts(List <Contact> contacts) { // Continue to allow user to edit contacts // until user enters nothing for an ID while (true) { Console.Clear(); Contact contact = null; //let user type ID of contact to edit Console.WriteLine("Edit contacts. Leave ID blank to stop editing..."); string val = Display.GetUserInput("Enter ID to edit: ").Trim(); string doAgain = ""; if (val.Length > 0) { int id = 0; // search for and display results if (Validation.IsNumeric(val, ref id)) { contact = ContactQuery.FindByID(contacts, id); if (contact != null) // we have found the contact { Display.DisplayContact(contact); } else { Display.Pause("Contact with that ID was not found."); } } else { Display.Pause("Please enter a valid ID."); } } else { return; } // Now, allow user to edit the contact if (contact != null) { /// <COPY> /// create deep copy of contact in case of bad input /// </COPY> Contact tempCopy = contact.DeepCopy(); Console.WriteLine("Edit fields. Leave a blank in any field that you do not wish to change."); Console.WriteLine(""); // Ask user for new values for each field. // Assign the new value ONLY if one was provided. // Otherwise, leave the old value in its place. string input = Display.GetUserInput("First Name: ", false); if (input.Trim().Length > 0) { tempCopy.FirstName = input; } input = Display.GetUserInput("Last Name: ", false); if (input.Trim().Length > 0) { tempCopy.LastName = input; } input = Display.GetUserInput("Street Address: ", false); if (input.Trim().Length > 0) { tempCopy.Address = input; } input = Display.GetUserInput("City: ", false); if (input.Trim().Length > 0) { tempCopy.City = input; } input = Display.GetUserInput("State: ", false); if (input.Trim().Length > 0) { tempCopy.State = input; } input = Display.GetUserInput("Zip: ", false); if (input.Trim().Length > 0) { tempCopy.Zip = input; } input = Display.GetUserInput("County: ", false); if (input.Trim().Length > 0) { tempCopy.County = input; } input = Display.GetUserInput("Company: ", false); if (input.Trim().Length > 0) { tempCopy.CompanyName = input; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Phone1: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUSPhoneNumber(input)) { tempCopy.phone1IsValid = true; tempCopy.Phone1 = input; doAgain = "N"; // just in case } else { tempCopy.phone1IsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Phone Number Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter phone number 1 again? Y/N \n"); } } else { tempCopy.phone1IsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check phone 1 valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.phone1IsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Phone2: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUSPhoneNumber(input)) { tempCopy.phone2IsValid = true; tempCopy.Phone2 = input; doAgain = "N"; // just in case } else { tempCopy.phone2IsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Phone Number Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter phone number 2 again? Y/N \n"); } } else { tempCopy.phone2IsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check phone 2 valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.phone2IsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Email: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidEmail(input)) { tempCopy.emailIsValid = true; tempCopy.Email = input; doAgain = "N"; // just in case } else { tempCopy.emailIsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid Email Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter email again? Y/N \n"); } } else { tempCopy.emailIsValid = true; // user doesn't want contact to have a phone # doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check email valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.emailIsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } do { // edited by Garrett Rathke on 10/03/17 per Assignment 4 instructions input = Display.GetUserInput("Web Page: ", false); if (input.Trim().Length > 0) { if (Validation.IsValidUrl(input)) { tempCopy.urlIsValid = true; tempCopy.Web = input; doAgain = "N"; // just in case } else { tempCopy.urlIsValid = false; tempCopy.dataIsValid = false; Console.WriteLine("Invalid URL Format...Contact Cannot be Entered"); doAgain = Display.GetUserInput("Do you want to enter URL again? Y/N \n"); } } else if (input.Trim().Length <= 0) { tempCopy.urlIsValid = true; // user doesn't want contact to have a website doAgain = "N"; // just in case } } while (doAgain.Contains("Y") || doAgain.Contains("y")); // check URL valid format...if not do loop // if user entered incorrect data but does not want to retry // then break out of data entry and return to menu if ((tempCopy.urlIsValid == false) && (!(doAgain.Contains("y") || doAgain.Contains("Y")))) { contacts.RemoveAt(tempCopy.ID); // remove object that contained user's bad data break; } // check for all data valid if (tempCopy.phone1IsValid == true && tempCopy.Phone2IsValid == true && tempCopy.emailIsValid == true && tempCopy.urlIsValid == true) { tempCopy.dataIsValid = true; // save edits to original contact.ID = tempCopy.ID; contact.FirstName = tempCopy.FirstName; contact.LastName = tempCopy.LastName; contact.Address = tempCopy.Address; contact.City = tempCopy.City; contact.State = tempCopy.State; contact.Zip = tempCopy.Zip; contact.County = tempCopy.County; contact.CompanyName = tempCopy.CompanyName; contact.Phone1 = tempCopy.Phone1; contact.Phone2 = tempCopy.Phone2; contact.Email = tempCopy.Email; contact.Web = tempCopy.Web; contacts.RemoveAt(tempCopy.ID); // delete copy Console.Clear(); Console.WriteLine("Review your changes: "); // display final changes to the user Display.DisplayContact(contact); Display.Pause(); } } } }