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

Ryan

I am trying to create a C# console app, where if a user presses a number it will ask them to add a task, view current task, delete a task. I have created my first class to "Add a task" just get the user input, and then in the main method call that class running the user input through it, any advice?

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();
Tony

To have this output: Screenshot

You can use this:

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);

        }


    }


}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading user input into a list through a class member

From Dev

C# - Display Countdown Timer in Console for User Input Through ReadLine()

From Dev

Passing user Input from a HTML page to a Perl script and displaying the results in webpage

From Dev

Displaying messages to users through class

From Dev

Displaying dynamic user input programmatically

From Dev

Displaying user input html with newlines

From Dev

Passing user through formFactory option

From Dev

Displaying array values through a submit input in PHP

From Dev

Securely passing user input to command

From Dev

Passing an object to a class through another class

From Dev

Immediately output user input to console

From Dev

Disabling User Input in a Console Application

From Dev

python user input without console

From Dev

python user input without console

From Dev

Disabling User Input in a Console Application

From Dev

User Input To End Close Console

From Dev

NodeJs handle user input in console

From Dev

Passing multiple values through J Query and displaying in them in a div

From Dev

How to stop displaying an input user guess with a countdown

From Dev

Displaying multiplication table based on user input

From Dev

Javascript- function displaying user input array

From Dev

passing an array as an argument; Setting up Array in Java with user input with Scanner Class

From Dev

Getting value of input and passing along through querystring

From Dev

Passing an input through a function on same page

From Dev

Passing values to input through g:each iteration

From Dev

Iterating through user input and a list

From Dev

Searching Dictionaries through User Input

From Dev

Iterating through user input and a list

From Dev

Example of Class with User Input

Related Related

  1. 1

    Reading user input into a list through a class member

  2. 2

    C# - Display Countdown Timer in Console for User Input Through ReadLine()

  3. 3

    Passing user Input from a HTML page to a Perl script and displaying the results in webpage

  4. 4

    Displaying messages to users through class

  5. 5

    Displaying dynamic user input programmatically

  6. 6

    Displaying user input html with newlines

  7. 7

    Passing user through formFactory option

  8. 8

    Displaying array values through a submit input in PHP

  9. 9

    Securely passing user input to command

  10. 10

    Passing an object to a class through another class

  11. 11

    Immediately output user input to console

  12. 12

    Disabling User Input in a Console Application

  13. 13

    python user input without console

  14. 14

    python user input without console

  15. 15

    Disabling User Input in a Console Application

  16. 16

    User Input To End Close Console

  17. 17

    NodeJs handle user input in console

  18. 18

    Passing multiple values through J Query and displaying in them in a div

  19. 19

    How to stop displaying an input user guess with a countdown

  20. 20

    Displaying multiplication table based on user input

  21. 21

    Javascript- function displaying user input array

  22. 22

    passing an array as an argument; Setting up Array in Java with user input with Scanner Class

  23. 23

    Getting value of input and passing along through querystring

  24. 24

    Passing an input through a function on same page

  25. 25

    Passing values to input through g:each iteration

  26. 26

    Iterating through user input and a list

  27. 27

    Searching Dictionaries through User Input

  28. 28

    Iterating through user input and a list

  29. 29

    Example of Class with User Input

HotTag

Archive