0

I am trying to make a todo application in c#. What I want to do is to be able to mark a Task as done somehow. Does anyone know how to do this?

This is my program class where i print out the list in a foreach:

using System;

namespace ToDo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isRunning = true;

            var collection = new TodoCollection();


            while (isRunning)
            {
                var menu = new menu();
                menu.Title();
                menu.Options();
                int choices;
                choices = Convert.ToInt32(Console.ReadLine());
                switch (choices)
                {
                    case 1:
                        var inputNewTask = Console.ReadLine();
                        var task = new Task(inputNewTask, false);
                        collection.Add(task);
                        Console.Clear();
                        Console.ReadLine();
                        break;
                    case 2:
                        int counter = 0;
                        if (collection != null)
                        {
                            
                            Console.WriteLine("Dagens uppgifter listas här nedanför");
                            foreach (Task newTask in collection)
                            {
                                counter++;
                                Console.WriteLine($"({counter}) [] {newTask.ShowTask()}");
                            }
                            int userInput = Convert.ToInt32(Console.ReadLine());
                            Task selectedTask = collection.GetTask(userInput);
                            selectedTask.Done();
                            


                        }
                        break;
                }
            }

        }
    }
}

In my foreach loop there's a empty scuare brackets i wanna change to an "X" with user input.

Here is my class for my Todo collection containing my list:

using System.Collections;
using System.Collections.Generic;

namespace ToDo
{
    public class TodoCollection : IEnumerable<Task>
    {
        //Deklarerar mina fält
        List<Task> _CurrentTasks = new List<Task>();
        

   
        //Funktion som låter användaren läga till nya tasks i listan
        public void Add(Task NewTask)
        {
            _CurrentTasks.Add(NewTask);
        }
        public Task GetTask(int userInput)
        {
           return _CurrentTasks[userInput];

        }

        //Låter mig iterera igenomm listan _CurrentTasks
         public IEnumerator<Task> GetEnumerator()
        {
            return ((IEnumerable<Task>)_CurrentTasks).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_CurrentTasks).GetEnumerator();
        }
    }
}
``´
 

And I also have a class for my Tasks here:

namespace ToDo
{
    public class Task
    {
        string _Name;
        bool isDone = false;
        public Task(string name, bool done)
        {
            this._Name = name;
            this.isDone = done;
        }

        public string ShowTask()
        {
            return this._Name;
        }
        public bool Done()
        {
            isDone = true;
            return isDone;
        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • What about your current program does not work? You already read user input and process it to set a task to done. Note that a console application will not result in the userfriendliest of todo applications... – mennowo Dec 02 '21 at 11:35
  • yeah i know that. But i want to make so that if user press for exmple "1" when the current todos shows i want the bracket inside my consoole.WriteLine to be marked with an "X" – Marcus Rosberg Dec 02 '21 at 11:38
  • If you mean this in a kind of "interactive" way, a console app is not typically suited for this kind of user interaction. You can simulate this though, see: https://stackoverflow.com/questions/60767909/c-sharp-console-app-how-do-i-make-an-interactive-menu – mennowo Dec 02 '21 at 11:53

1 Answers1

0

I assume that option 2 is "mark task as done", and you print out the list of tasks, then ask the user which one to mark as done:

                        Console.WriteLine("Dagens uppgifter listas här nedanför");
                        foreach (Task newTask in collection)
                        {
                            counter++;
                            Console.WriteLine($"({counter}) [] {newTask.ShowTask()}");
                        }
                        int userInput = Convert.ToInt32(Console.ReadLine());
                        Task selectedTask = collection.GetTask(userInput);
                        selectedTask.Done();

Because "print the list of tasks" sounds like somethign we would want to do often, we should make it a method:


    static void PrintListOfTasks(ToDoCollection what){
        foreach (Task newTask in what)
        {
            counter++;
            Console.WriteLine($"({counter}) [] {newTask.ShowTask()}");
        }
    }

Let's also make it show an X if the task is done and a space if it isn't:

 Console.WriteLine($"({counter}) [{newTask.IsDone() ? 'X' : ' '}] {newTask.ShowTask()}");

But now there's another problem: Task doesn't have a way to tell if it's done or not, it only has a Done() method that marks it as done. You haven't used c# properties anywhere so I haven't used one in my recommendation here; instead we could make a method that just reports back the current Task state. CopyPaste the Done() method and change the pair so the existing single "mark it as done and report back the current state" (which is a little bit nonsensical, because we know what the state of a boolean must be if we just marked it as true) is instead two different methods, one that sets the value, and one that gets the value:

    public void MarkAsDone()
    {
        isDone = true;
    }
    public bool IsDone()
    {
        return isDone;
    }

Now you can use the get verison in your WriteLine display, and you can use your set version in the "mark task as done" menu option

Finally, you can adjust your loop that does option 2, so it's a logic of:

  • PrintList (pass in the collection)
  • Ask user which to mark as done
  • Mark it as done
  • Print List again to confirm to the user it was done
Caius Jard
  • 72,509
  • 5
  • 49
  • 80