我有一个小问题。如何将 Person 类中的方法调用到 Menu 类中的 Switch-case。不管我做什么。我感谢您的帮助。
课程菜单
static class Menu
{
public static void MainText()
{
Console.WriteLine("1. Add new person");
Console.WriteLine("2. Print all");
Console.WriteLine("3. Find item by name");
Console.WriteLine("4. Delete item by ID");
Console.WriteLine("5. Exit");
Console.WriteLine("\n");
}
public static void MainStart()
{
string choice = "";
do
{
MainText();
Console.WriteLine("Enter your choice: ");
choice = Console.ReadLine();
switch (choice)
{
case "1":
break;
}
} while (choice != "5");
}
}
类人
public class Person
{
private int ID = 0;
private string FirstName;
private string LastName;
private string PhoneNumber;
Person p = new Person();
List<Person> H = new List<Person>();
public Person(string fName, string lName, string phoneNum)
{
FirstName = fName;
LastName = lName;
PhoneNumber = phoneNum;
}
public Person()
{
}
public void PrintAll()
{
foreach (Person item in H)
{
Console.WriteLine("First name: {0}", FirstName);
Console.WriteLine("Last name: {0}", LastName);
Console.WriteLine("Phone number: {0}", PhoneNumber);
Console.WriteLine("ID: {0}", ID);
}
}
public override string ToString()
{
return "First name: " + FirstName + "\n" + "Last name: " + LastName + "\n" + "Phone number: " + PhoneNumber + "\n" + "ID: " + ID + "\n";
}
public void AddPerson()
{
string fn = "";
do
{
Console.WriteLine("Enter first name: ");
fn = Console.ReadLine();
if (fn == "")
{
Console.WriteLine("First name can't be blank");
}
}
while (fn == "");
p.FirstName = fn;
Console.WriteLine("Enter last name: ");
p.LastName = Console.ReadLine();
Console.WriteLine("Enter phone number: ");
p.PhoneNumber = Console.ReadLine();
p.ID++;
H.Add(p);
}
public void FindPerson()
{
Console.WriteLine("Please enter first or last name: ");
string search = Console.ReadLine();
foreach (Person item in H)
{
if (search == item.FirstName || search == item.LastName)
{
p.ToString();
Console.WriteLine("*****************");
}
}
}
public void DeletePerson()
{
H.ToString();
Console.WriteLine("Enter an ID to delete: ");
int id = int.Parse(Console.ReadLine());
H.RemoveAt(id);
Console.WriteLine("Person deleted successfully");
}
}