根据用户输入删除联系人 [英] Remove a contact based on user input
问题描述
我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个联系人和一个联系人对象。我还有一种删除和更新列表中联系人的方法,但我希望用户能够选择联系人并能够删除该联系人。但是我不确定如何做到这一点。
任何指导都将不胜感激。
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 and update a contact in the list but I want have the user to be able to pick a contact and be able to delete the 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);
}
推荐答案
您需要为用户提供一种选择联系人的方式。最大的问题是 - 有一个长列表 - 合同可能已经滚出屏幕。
您可能希望将显示限制为N行并实现PgUp / PgDn关键功能。您必须在联系人列表中跟踪索引偏移量。
要选择特定行,最简单的方法是使用向上/向下箭头键突出显示特定行。突出显示一行可以通过更改文本背景或将文本放在括号中来完成[像这样]。风格由您自行决定。您还必须跟踪突出显示的行的索引。
修改DisplayList函数以接受另外两个参数 - 偏移量和行数。实现PgUp / PgDn和Up / Dn箭头键 - 加上删除菜单,你就完成了。
You need to offer the user a way to pick a contact. The biggest problem is - with a long list - the contract may have scrolled off the screen.
You may want to limit the display to N rows and implement PgUp / PgDn key functionality. You will have to keep track of an index offset into your contact list.
To select a specific row, the easy thing is to use the Up/Down arrow keys to highlight a specific row. Highlighting a row can be done by changing the text background or placing the text in brackets [like this]. The style is at your discretion. You will also have to keep track of the index of the highlighted row.
Modify your DisplayList function to accept two additional parameters - offset and number of rows. Implement the PgUp/PgDn and Up/Dn arrow keys - plus a delete menu, and you're done.
这篇关于根据用户输入删除联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!