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 list with data and a contact object. I made a method to sort by first name and a method to sort by last name but I am unsure as to how to display the contacts sorted back to the user.
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':
LoadToFile();
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;
}
}
//Sorts the list by last name
public static void sortByLastName(List<Contact> contactList )
{
contactList.OrderBy(c => c.GetLastName);
}
//Sorts the list by first name
public static void sortByFirstName(List<Contact> contactList)
{
contactList.OrderBy(c => c.GetFirstName);
}
您为什么不尝试这种方法:
contactList.Sort((x, y) => string.Compare(x.GetLastName, y.GetLastName));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.