您好我正在尝试使用列表构建联系人管理器程序来存储和显示数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个包含数据的列表,但我需要更改方法以允许用户创建一个新的联系人,其中包含他们的姓名,电话号码,电子邮件地址和类型。但我不知道该如何做到这一点。
任何指导都将不胜感激。
static void Main(string[] args)
{
//Declare the list
ArrayList list = new ArrayList();
Console.WriteLine("Contact List");
// display the menu to the user
Console.WriteLine("Enter option or M for menu:");
//Main Driver
char menuItem;
menuItem = GetMenuItem();
while (menuItem != 'X')
{
ProcessMenuItem(menuItem);
menuItem = GetMenuItem();
}
Console.WriteLine("\nThank you, goodbye");
Console.ReadLine();
}
static char GetMenuItem()
{
char menuItem;
DisplayMenu();
menuItem = IOConsole.GetChar((Console.ReadLine()));
while (menuItem != 'C'
&& menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
{
Console.WriteLine("\nError - Invalid menu item");
DisplayMenu();
//menuItem = IOConsole.GetChar((Console.ReadLine()));
}
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)
{
switch (menuItem)
{
case 'C':
createContact();
break;
case 'R':
removeContact();
break;
case 'U':
updateContact();
break;
case 'D':
LoadToFile();
break;
case 'S':
saveToFile();
break;
case 'L':
sortByLastName();
break;
case 'F':
sortByFirstName();
break;
case 'P':
break;
case 'T':
break;
case 'Q':
break;
}
}
public static void createContact()
{
Contact c1 = new Contact();
//c1.Number = 1; //Id?
c1.GetLastName = "Doe";
c1.GetFirstName = "John";
c1.GetEmailAddress = "johndoe@email.com";
c1.GetPhoneNumber = "12345678";
c1.ContactTypes = ContactTypesEnum.Friend;
//Create more contacts...
//Add all contacts here
ContactCollection contactList = new ContactCollection();
contactList.Add(c1);
//Loop through list
foreach( Contact c in contactList)
{
Console.WriteLine(c.GetFirstName); //Do something with fields
}
Console.ReadLine();
}
答案 0 :(得分:0)
向用户询问每个字段
public static void createContact()
{
Contact c1 = new Contact();
Console.WriteLine("GetFirstName");
c1.GetFirstName = Console.ReadLine();
Console.WriteLine("GetLastName");
c1.GetLastName = Console.ReadLine();
Console.WriteLine("GetEmailAddress");
c1.GetEmailAddress = Console.ReadLine();
Console.WriteLine("GetPhoneNumber");
c1.GetPhoneNumber = Console.ReadLine();
Console.WriteLine("ContactTypes");
c1.ContactTypes = Console.ReadLine();
//Create more contacts...
//Add all contacts here
ContactCollection contactList = new ContactCollection();
contactList.Add(c1);
//Loop through list
foreach (Contact c in contactList)
{
Console.WriteLine(c.GetFirstName); //Do something with fields
// Save using this foreach loop to some collection where you are storing contacts or may be directly save the list.
}
Console.ReadLine();
}
但是我怀疑你怎么能在该列表中找到所有联系人,因为每次它都会出现在这个方法中,它会创建一个情感列表。