using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static List tasks = new List();
static void Main(string[] args)
{
LoadTasksFromFile();
while (true)
{
Console.Clear();
Console.WriteLine("To-Do List Application");
Console.WriteLine("1. Add a task");
Console.WriteLine("2. Remove a task");
Console.WriteLine("3. Display all tasks");
Console.WriteLine("4. Mark task as completed");
Console.WriteLine("5. Exit");
Console.Write("Select an option: ");
var choice = Console.ReadLine();
switch (choice)
{
case "1":
AddTask();
break;
case "2":
RemoveTask();
break;
case "3":
DisplayTasks();
break;
case "4":
MarkTaskAsCompleted();
break;
case "5":
SaveTasksToFile();
return;
default:
Console.WriteLine("Invalid option. Press Enter to try again.");
Console.ReadLine();
break;
}
}
}
static void AddTask()
{
Console.Write("Enter the task description: ");
string description = Console.ReadLine();
Task newTask = new Task(description);
tasks.Add(newTask);
Console.WriteLine("Task added. Press Enter to continue...");
Console.ReadLine();
}
static void RemoveTask()
{
DisplayTasks();
Console.Write("Enter the task number to remove: ");
if (int.TryParse(Console.ReadLine(), out int taskNumber) && taskNumber > 0 && taskNumber <= tasks.Count)
{
tasks.RemoveAt(taskNumber - 1);
Console.WriteLine("Task removed. Press Enter to continue...");
}
else
{
Console.WriteLine("Invalid task number. Press Enter to try again.");
}
Console.ReadLine();
}
static void DisplayTasks()
{
if (tasks.Count == 0)
{
Console.WriteLine("No tasks found.");
}
else
{
Console.WriteLine("To-Do List:");
for (int i = 0; i < tasks.Count; i++)
{
string status = tasks[i].IsCompleted ? "[Completed]" : "[Pending]";
Console.WriteLine($"{i + 1}. {tasks[i].Description} - {status}");
}
}
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
static void MarkTaskAsCompleted()
{
DisplayTasks();
Console.Write("Enter the task number to mark as completed: ");
if (int.TryParse(Console.ReadLine(), out int taskNumber) && taskNumber > 0 && taskNumber <= tasks.Count)
{
tasks[taskNumber - 1].IsCompleted = true;
Console.WriteLine("Task marked as completed. Press Enter to continue...");
}
else
{
Console.WriteLine("Invalid task number. Press Enter to try again.");
}
Console.ReadLine();
}
static void SaveTasksToFile()
{
using (StreamWriter writer = new StreamWriter("tasks.txt"))
{
foreach (var task in tasks)
{
writer.WriteLine($"{task.Description}|{task.IsCompleted}");
}
}
}
static void LoadTasksFromFile()
{
if (File.Exists("tasks.txt"))
{
foreach (var line in File.ReadLines("tasks.txt"))
{
var parts = line.Split('|');
if (parts.Length == 2)
{
tasks.Add(new Task(parts[0], bool.Parse(parts[1])));
}
}
}
}
}
class Task
{
public string Description { get; set; }
public bool IsCompleted { get; set; }
public Task(string description)
{
Description = description;
IsCompleted = false;
}
public Task(string description, bool isCompleted)
{
Description = description;
IsCompleted = isCompleted;
}
}