繁体   English   中英

使用 C# 的交付程序 - 在 switch case 语句后更改值

[英]Delivery Program using C# - Change values after switch case statement

首先我要稍微解释一下我的程序!

我编写了一个邮件投递程序,它允许用户开始投递包裹并保存诸如发件人姓名和发件人目的地等值,然后列出包裹,然后开始投递包裹。

在这里,我有一个名为 senderDestination 的公共类,我将值保存到列表中。

public class senderDestination
    {
        public string senderName { get; set; }
        public string destination { get; set; }

        public string delivered;

        public string pending;

        public string delivering;

        public senderDestination(string status, string pendingStatus, string startingDelivery)
        {
            delivered = status;
            pending = pendingStatus;
            delivering = startingDelivery;
        }
    }

这里我有一个 switch 语句,在这个 switch 语句中,我允许用户输入值,例如交付目的地名称和我们发送给的人的姓名:

           case ConsoleKey.D2:
                        Clear();

                        bool registeringSenderAndDestination = true;

                        do
                        {
                            senderDestination senderdestination = new senderDestination("Delivered", "Pending Delivery", "Starting Delivery");

                            Write("Sender: ");
                            senderdestination.senderName = ReadLine();

                            Write("Destination: ");
                            senderdestination.destination = ReadLine();

                            Clear();

                            WriteLine($"Sender: {senderdestination.senderName}");
                            WriteLine($"Destination:  {senderdestination.destination}");

                            allSenders.Add(senderdestination);

                                
                        } while (registeringSenderAndDestination);

                        Clear();

                        break;

在我的第三个 switch case 语句中,我允许用户列出我们刚刚保存的所有包:

          case ConsoleKey.D3:
                        Clear();

                        int id = 1;

                        WriteLine("ID    Destination                        Status");

                        WriteLine("------------------------------------------------------------------");

                        // List packages 
                        foreach (var sender in allSenders)
                        {
                            // write here all the information you want to display.
                            WriteLine($"{id++}     {sender.destination,-14}                     {sender.pending}");
                        }

                        ReadKey(true);

                        Clear();

                        break;

如果用户想开始交付这些包裹,则第四个 switch 语句是用户单击的位置:

         case ConsoleKey.D4:
                        Clear();

                        int ids = 1;

                        WriteLine("Starting delivery on pending packages...");

                        WriteLine("------------------------------------------------------------------");

                        WriteLine("ID    Destination                        Status");

                        WriteLine("------------------------------------------------------------------");

                        foreach (var sender in allSenders)
                        {
                            WriteLine($"{ids++}     {sender.destination,-14}                     {sender.delivering}");
                        }

                        Thread.Sleep(4000);

                        Clear();

                        break;

我想要做的是当用户单击菜单中的第四个 switch 语句时,我希望第三个 switch 语句从 {sender.pending} 更改为 {sender.delivered} 但我不知道如何执行此操作。 我什至可以做到吗? 对不起,如果我在这个问题中有很多代码,我不知道如何解释它。

首先,请注意要交付的每个项目的状态只有一个,因此您只需要一个属性来定义此状态,而不是三个。 在此上下文中,添加具有三种可能状态的枚举并更改 senderDestination 类,移除三个字符串并添加 Status 属性以定义列表中项目的当前状态

public enum SenderStatus
{
    delivered = 1,
    pending = 2,
    delivering = 3

}

public class senderDestination
{
    public string senderName { get; set; }
    public string destination { get; set; }
    public SenderStatus Status { get;set;}

    public senderDestination()
    {
        Status = SenderStatus.pending;
    }
}

现在您可以列出列表中每个元素的状态,只需打印出 Status 属性(WriteLine 会自动将枚举转换为正确的字符串)

    WriteLine("ID    Destination                        Status");
    WriteLine("------------------------------------------------------------------");

    // List packages 
    foreach (var sender in allSenders)
    {
        // write here all the information you want to display.
        WriteLine($"{id++}     {sender.destination,-14}, {sender.Status}");
    }

最后,当您交付所有项目时,您可以将 Status 属性更改为从枚举中获取的适当值

    foreach (var sender in allSenders)
    {
        sender.Status = SenderStatus.delivered
        WriteLine($"{ids++}     {sender.destination,-14}                     {sender.Status}");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM