I am trying to build a contact managers program in a console application using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a contact and a contact object. I also have a method to delete a contact but I want to have the user to be able to pick a contact name and be able to delete the selected contact. However I am unsure how to do this.
Any guidance would be appreciated.
static void Main(string[] args)
{
//Declare the list
List<Contact> contactList = new List<Contact>();
//Main Driver
char menuItem;
Console.WriteLine("Contact List\n");
menuItem = GetMenuItem();
while (menuItem != 'Q')
{
ProcessMenuItem(menuItem, contactList);
menuItem = GetMenuItem();
}
Console.WriteLine("\nThank you, goodbye");
Console.ReadLine();
}
//Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
static char GetMenuItem()
{
char menuItem;
DisplayMenu();
menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
while (menuItem != 'C'
&& menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
{
Console.WriteLine("\nError - Invalid menu item");
DisplayMenu();
menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
}
return menuItem;
}
static void DisplayMenu()
{
Console.WriteLine("C-> Create Contacts");
Console.WriteLine("R-> Remove Contacts");
Console.WriteLine("U-> Update Contacts");
Console.WriteLine("D -> Load data from file");
Console.WriteLine("S-> Save data to file");
Console.WriteLine("L-> View sorted by last name");
Console.WriteLine("F-> View sorted by first name");
Console.WriteLine("P-> View by partial name search");
Console.WriteLine("T-> View by contact type");
Console.WriteLine("Q-> Quit");
}
//Routes to the appropriate process routine based on the user menu choice
static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
{
switch (menuItem)
{
case 'C':
createContact();
break;
case 'R':
removeContact(contactList);
break;
case 'U':
updateContact(contactList);
break;
case 'D':
LoadFromFile();
break;
case 'S':
saveToFile();
break;
case 'L':
sortByLastName(contactList);
break;
case 'F':
sortByFirstName(contactList);
break;
case 'P':
DisplayList(contactList);
break;
case 'T':
sortByContactType();
break;
case 'Q':
break;
}
}
//allows the user to remove a contact
public static void removeContact(List<Contact> contactList)
{
for (int i = 0; i < contactList.Count; i++)
if (i % 5 == 0)
contactList.RemoveAt(i);
}
There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.
You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.
Method #1 : Using remove() remove() generally removes the first occurrence of K string and we keep iterating this process until no K string is found in list.
You can do it easily using LINQ or Lambda Expression:
In the Model Class of the Contact make a field say the Phone Number itself.
While selecting the contact two parameters in the
removeContact(List<Contact> contactList,int selectedContactPhoneNo)
You can simply do then:
contactList.RemoveAll(x => x.PhoneNo == selectedContactPhoneNo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With