菜单驱动程序的删除功能

本文关键字:功能 删除 驱动程序 菜单 | 更新日期: 2023-09-27 18:29:42

我在实现delete函数时遇到问题,如有任何帮助,不胜感激。我有一个名为memberlist的列表,它存储人员的姓名和号码。除了删除功能外,我什么都能用。如有任何帮助,我们将不胜感激。

public class Person
{
    private string name;
    private string phoneNumber;
    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    public string PhoneNumber
    {
        set { phoneNumber = value; }
        get { return phoneNumber; }
    }
    public void PrintInfo()
    {
        Console.WriteLine();
        Console.WriteLine(" Name: {0}", name);
        Console.WriteLine(" Phone Number: {0}", phoneNumber);
        Console.WriteLine();
    }
    public void SaveASCII(ref StreamWriter output)
    {
        output.WriteLine(name);
        output.WriteLine(phoneNumber);
    }
    public void LoadASCII(ref StreamReader input)
    {
        name = input.ReadLine();
        phoneNumber = input.ReadLine();
    }
}

public class membershipList
{
    public Person[] ML = null;
    public void AddMember(Person p)
    {
        if (ML == null)
        {
            ML = new Person[1];
            ML[0] = p;
        }
        else
        {
            Person[] temp = ML;
            ML = new Person[temp.Length + 1];
            for (int i = 0; i < temp.Length; ++i)
            {
                ML[i] = new Person();
                ML[i] = temp[i];
            }
            ML[temp.Length] = new Person();
            ML[temp.Length] = p;
            temp = null;
        }
    }
        public void DeleteMember(string name)
    {
        if (ML == null)
        {
            Console.WriteLine(name + " had not been added before.");
        }
        else
        {
            DeleteMember(name);
            }
        }

    public void PrintAll()
    {
        if (ML != null)
            foreach (Person pers in ML)
                pers.PrintInfo();
        else Console.WriteLine("Then list is empty");
    }
    public void Search(string p)
    {
        if (ML != null)
        {
            foreach (Person pers in ML)
            {
                if (pers.Name.ToLower().CompareTo(p.ToLower()) == 0)
                {
                    Console.WriteLine("1 Record Found:");
                    pers.PrintInfo();
                    break;
                }
            }
            Console.WriteLine("Record not found.");
        }
        else Console.WriteLine("Then list is empty.");
    }
    public void ReadASCIIFile()
    {
        StreamReader input = new StreamReader("memberlist.dat"); ;
        try
        {
            int num = Convert.ToInt32(input.ReadLine());
            ML = new Person[num];
            for (int i = 0; i < num; ++i)
            {
                ML[i] = new Person();
                ML[i].LoadASCII(ref input);
            }
            input.Close();
        }
        catch (FormatException e)
        {
            Console.WriteLine(e.Message);
            input.Close();
        }

    }
    public void SaveASCIIFile()
    {
        StreamWriter output = new StreamWriter("memberlist.dat");
        output.WriteLine(ML.Length);
        foreach (Person pers in ML)
        {
            pers.SaveASCII(ref output);
        }
        output.Close();

    }
}

class Program
{
    static void Main(string[] args)
    {
        membershipList ML = new membershipList();
        ML.ReadASCIIFile();
        string option;
        do
        {
            // Console.Clear();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("MemberShip List MENU");
            Console.WriteLine();
            Console.WriteLine("     a. Add");
            Console.WriteLine("     b. Seach");
            Console.WriteLine("     c. Delete");
            Console.WriteLine("     d. Print All");
            Console.WriteLine("     e. Exit");
            Console.WriteLine();
            Console.Write("option: ");
            option = Console.ReadLine().ToLower();
            switch (option)
            {
                case "a":
                    Person np = new Person();
                    Console.Write("Enter Name: ");
                    np.Name = Console.ReadLine();
                    Console.Write("Enter PhoneNumber: ");
                    np.PhoneNumber = Console.ReadLine();
                    ML.AddMember(np);
                    break;
                case "b":
                    Console.Write("Enter Name: ");
                    string name = Console.ReadLine();
                    ML.Search(name);
                    break;
                case "c":
                    Console.Write("Enter Name to be Deleted:");
                    ML.DeleteMember(name);
                    break;
                case "d":
                    ML.PrintAll();
                    break;
                case "e":
                    ML.SaveASCIIFile();
                    Console.WriteLine("BYE...... ");
                    break;
                default:
                    Console.WriteLine("Invalid Option");
                    break;
            }

        } while (option.ToLower() != "d");
    }
}

菜单驱动程序的删除功能

如果按照@Wiktor Zychla的建议将Person[]更改为List,则此代码应该可以很好地工作:

public static List<Person> ML = new List<Person>();
public static void DeleteMember(string name)
{        
    var deleteMe = ML.Find(p => p.Name == name);
    if (deleteMe == null)
    {
        Console.WriteLine(name + " had not been added before.");
    }
    else
    {
        ML.Remove(deleteMe);
    }
}

删除与添加类似。改为使用List的建议是好的,但它可以本地化为您的DeleteMember()例程。

试试这样的东西(编辑):

public void DeleteMember(string name)
{
    if (ML == null)
    {
        Console.WriteLine(name + " had not been added before.");
    }
    else
    {
        int memberIndex = ML.ToList().FindIndex(p => p.Name == name);
        if (memberIndex == -1)
        {
            Console.WriteLine(name + " had not been added before.");
            return;
        }
        List<Person> tmp = new List<Person>(ML);
        tmp.RemoveAt(memberIndex);
        ML = tmp.ToArray();
    }
}