通过 class 传递用户输入并将其显示在控制台中

Passing user input through a class and displaying it in the console

我正在尝试创建一个 C# 控制台应用程序,如果用户按下一个数字,它将要求他们添加任务、查看当前任务、删除任务。我已经创建了我的第一个 class 到 "Add a task" 只是获取用户输入,然后在主方法调用中 class 运行 用户输入通过它,有什么建议吗?

class addTask
{
    public string UserInput { get; set; }
    public override string ToString() => UserInput;
}
...
System.Console.WriteLine("Please select what you would like to do");
System.Console.WriteLine("1. Add A Task");
System.Console.WriteLine("2. Remove A Task");
System.Console.WriteLine("3. Update A Task");
System.Console.WriteLine("4. View Task");
int num = Convert.ToInt32(Console.ReadLine());

if (num == 1)
{
    System.Console.WriteLine("Please enter a new Task");
    string input = Console.ReadLine();

    while (input != Console.ReadLine())
    {
        //  a.getUserInput.ToString();
        System.Console.WriteLine($"New Task: {a}");
        break;
    }
}
Console.ReadKey();

要有这个输出:

你可以使用这个:

using System;
using System.Collections.ObjectModel;

namespace C_TaskManager
{
    public class UserTask
    {
        public UserTask(string Description)
        {
            this.UserInput = Description;
        }
        public string UserInput { get; set; }
        public override string ToString()
        {
            return UserInput;
        }
    }
    public class TaskManager
    {
        public ObservableCollection<UserTask> UserTasks { get; set; } = new ObservableCollection<UserTask>();
    }
}

namespace C_TaskManager
{
    internal class Program
    {
        private static string ReadTextLineFromConsole()
        {
            return Console.ReadLine();
        }
        private static int ReadInt32FromConsole()
        {
            try
            {
                return Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception)
            {
                return -1;
            }
        }

        private static TaskManager tm = new TaskManager();
        private static void Main(string[] args)
        {
            int num = 0;
            do
            {
                Console.BackgroundColor = ConsoleColor.DarkBlue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();
                Console.WriteLine("Please select what you would like to do");
                Console.WriteLine("1. Add A Task");
                Console.WriteLine("2. Remove A Task");
                Console.WriteLine("3. Update A Task");
                Console.WriteLine("4. View Task");
                Console.WriteLine("5. List Tasks");
                Console.WriteLine("6. EXIT");
                Console.WriteLine("---------------------------------------");

                num = ReadInt32FromConsole();

                switch (num)
                {
                    case -1:
                        Console.WriteLine("Invalid entry. Try again.");
                        break;

                    case 1:
                        {
                            Console.WriteLine("Please enter a new Task:");
                            var input = ReadTextLineFromConsole();

                            var ut = new UserTask(input);    
                            tm.UserTasks.Add(ut);

                            Console.WriteLine($"New Task: {ut.ToString()}");
                        }
                        break;

                    case 2:
                        {
                            Console.WriteLine("Remove Task by index position:");
                            var input = ReadInt32FromConsole();

                            tm.UserTasks.RemoveAt(input);

                            Console.WriteLine("Task removed");
                        }
                        break;
                    case 3:
                        {
                            Console.WriteLine("Update Task by index position:");
                            var input = ReadInt32FromConsole();
                            // TODO: check if input is in range of UserTasks

                            var selectedTask = tm.UserTasks[input];

                            Console.WriteLine("Please enter a new Task Description:");
                            var NewDescription = ReadTextLineFromConsole();
                            selectedTask.UserInput = NewDescription;

                            Console.WriteLine("Task updated");
                        }
                        break;
                    case 4:
                        {
                            Console.WriteLine("View Task by index position:");
                            var input = ReadInt32FromConsole();

                            var selectedTask = tm.UserTasks[input];
                            Console.WriteLine(selectedTask.UserInput);
                        }
                        break;
                    case 5:
                        {
                            Console.WriteLine("List Tasks:");
                            foreach (var ut in tm.UserTasks)
                            {
                                Console.WriteLine(" * " + ut.UserInput);
                            }
                        }
                        break;
                }
                Console.WriteLine("");
                Console.WriteLine("////////////////////////////////////////////////////");
                Console.WriteLine("Press a key to return to main menu...");
                Console.ReadKey();
            } while (num != 6);

        }


    }


}