-2

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();
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Ryan
  • 31
  • 9
  • Well, I'm not really sure what your asking us to do, but for starters, use int.tryparse to get the number. As it stands your application will crash if a user enters anything that isn't an int. – Hack Jul 01 '18 at 14:51
  • Sorry been working on this for a long time tired now haha. im just trying to give the user a couple options to choose from using the number keys, if they select 1, then they will be prompted to enter a "to-do" that "to-do" is in the class "addTask" that will add a task for the user to view later – Ryan Jul 01 '18 at 15:01
  • So where i 'am standing right now in it, when i press 1 on the keypad to add a task, i can write in something, but it doesn't get ran through the addTask class that prints out what the user typed in, when i hit enter, the class runs, but the user input isn't ran through it – Ryan Jul 01 '18 at 15:03
  • 1
    Please [edit] your question and take the time to make the code readable by properly indenting it. – Camilo Terevinto Jul 01 '18 at 15:23
  • @CamiloTerevinto happy? – Ryan Jul 01 '18 at 15:33
  • Ryan, I will help you in a few minutes. – Tony Jul 01 '18 at 15:51
  • You really shouldn't name your properties starting with "get" or "set". Reason is they are simply not functions or methods. If I was using code and I saw it called "getName" I'd instantly assume its a method and type "getName()". That won't work though (unless it was returning a delegate) so it's completely confusing. Also, what makes more sense `"getName = "Name"` or `Name = "Name"`? – AustinWBryan Jul 02 '18 at 06:25

1 Answers1

3

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

        }


    }


}
Tony
  • 16,527
  • 15
  • 80
  • 134