コード例 #1
0
        // Add an object of todoNote and append it to the list of already made tasks.
        // Already made tasks are added to a list using the read Json function before appending a new task.
        public void AddTask(string whatToDo, string path)
        {
            List <TodoNote> todoList   = HandleJson.ReadFromJsonFile <List <TodoNote> >(path);
            List <TodoNote> updateList = new List <TodoNote>();

            TodoNote todo = new TodoNote
            {
                ID   = 0,
                task = whatToDo,
            };

            if (todoList == null)
            {
                todo.ID = 1;
                updateList.Add(todo);
            }
            else
            {
                todo.ID    = todoList.Count + 1;
                updateList = todoList;
                updateList.Add(todo);
            }

            Console.WriteLine("#{0}  {1}", todo.ID, todo.task);
            HandleJson.WriteToJsonFile <List <TodoNote> >(path, updateList);
        }
コード例 #2
0
        // Delete specified object if its present in the list.
        // New IDs are given when a task is completed.
        public void DeleteTask(int taskNumber, string path)
        {
            List <TodoNote> taskList = HandleJson.ReadFromJsonFile <List <TodoNote> >(path);

            if (taskList == null || taskList.Count == 0)
            {
                Console.WriteLine("TODO list is empty!");
                return;
            }

            TodoNote temp = new TodoNote();

            foreach (TodoNote p in taskList)
            {
                if (taskNumber == p.ID)
                {
                    temp = p;
                }

                if (p.ID > taskNumber)
                {
                    p.ID--;
                }
            }

            if (temp.ID == 0)
            {
                Console.WriteLine("Couldnt find task looking for!");
            }

            else
            {
                taskList.Remove(temp);
                Console.WriteLine("Completed #{0} {1}", temp.ID, temp.task);
            }

            if (taskList.Count == 0)
            {
                Console.WriteLine("\nGood job, you completed all tasks!\nType <Add \"MyTask\"> for creating new tasks");
            }

            HandleJson.WriteToJsonFile <List <TodoNote> >(path, taskList);
        }