Home > Mobile >  How to save/write Task with different attributes
How to save/write Task with different attributes

Time:11-04

I am trying to build a more advanced ToDoList-Console program. I am learning serialisation but what I do not seem to understand is how to:

  • Implement 2 kinds of tasks
  • saving them in a file(does not have to be json)
  • reading the file while taking the task type into consideration

My goal with that is having 2 Methods: task1 and task2. Task1 being the main task and task2 being a subtask of a task, which would be visualised with \t in the console. This is my current code that just saves tasks as a string, without any kind of complexity.

public class TodoItem
    {
        public string Description { get; set; }
        public DateTime? DueOn { get; set; }

        public override string ToString()
        {
            return $"{this.Description}";
        }
    }

    internal static class Program
    {
        static private readonly string _saveFileName = "todo.json";
        static void Main()
        {
            {
                // An example list containing 2 items
                List<TodoItem> items = new List<TodoItem> {
                new TodoItem { Description = "Feed the dog" },
                new TodoItem { Description = "Buy groceries" /*, DueOn = new DateTime(2021, 9, 30, 16, 0, 0)*/ }
            };
                // Serialize it to JSON
                string json = JsonSerializer.Serialize(items, new JsonSerializerOptions() { WriteIndented = true });

                // Save it to a file
                File.WriteAllText(_saveFileName, json);
            }

            // Loading list
            {
                string json = File.ReadAllText(_saveFileName);

                List<TodoItem> items = JsonSerializer.Deserialize<List<TodoItem>>(json);

                // Loading items
                foreach (var todo in items)
                    Console.WriteLine(todo);
            }

        }```

CodePudding user response:

You can use inheritance to implement two types of tasks.

The rest of your goals seem to work already :)

Here is an Example:

public abstract class TaskBase
{
    public string Description { get; set; }
    
    public DateTime? DueOn { get; set; }

    public override string ToString()
    {
        return $"{Description}";
    }
}

public class MainTask : TaskBase
{
    public SubTask? SubTask { get; set; }

    public override string ToString()
    {
        if (SubTask is not null)
        {
            return $"{Description}{Environment.NewLine}\t{SubTask}";
        }
        else
        {
            return base.ToString();
        }
    }
}

public class SubTask : TaskBase {}

static class Program
{
    private const string _saveFileName = "todo.json";

    static void Main()
    {
        {
            // An example list containing 2 items
            List<MainTask> items = new List<MainTask> 
            {
                new MainTask { Description = "Feed the dog" },
                new MainTask { Description = "Buy groceries", SubTask = new SubTask { Description = "Food" } }
            };

            // Serialize it to JSON
            string json = JsonSerializer.Serialize(items, new JsonSerializerOptions() { WriteIndented = true });

            // Save it to a file
            File.WriteAllText(_saveFileName, json);
        }

        // Loading list
        {
            string json = File.ReadAllText(_saveFileName);

            List<MainTask> items = JsonSerializer.Deserialize<List<MainTask>>(json);

            // Loading items
            foreach (var todo in items)
                Console.WriteLine(todo);
        }

    }
}
  • Related