Skip to content

Commit ffd06b2

Browse files
authored
Create TaskManager.cs
1 parent f990c85 commit ffd06b2

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

TaskManager.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.Json;
5+
6+
class TaskItem
7+
{
8+
public int Id { get; set; }
9+
public string Description { get; set; }
10+
}
11+
12+
class TaskManager
13+
{
14+
private const string FileName = "tasks.json";
15+
private static List<TaskItem> tasks = new();
16+
17+
static void Main(string[] args)
18+
{
19+
LoadTasks();
20+
21+
if (args.Length == 0)
22+
{
23+
Console.WriteLine("Usage:");
24+
Console.WriteLine(" add <description>");
25+
Console.WriteLine(" list");
26+
Console.WriteLine(" remove <id>");
27+
return;
28+
}
29+
30+
var command = args[0];
31+
32+
switch (command)
33+
{
34+
case "add":
35+
AddTask(string.Join(" ", args[1..]));
36+
break;
37+
case "list":
38+
ListTasks();
39+
break;
40+
case "remove":
41+
if (args.Length < 2 || !int.TryParse(args[1], out int id))
42+
{
43+
Console.WriteLine("Please provide a valid task ID.");
44+
}
45+
else
46+
{
47+
RemoveTask(id);
48+
}
49+
break;
50+
default:
51+
Console.WriteLine("Unknown command.");
52+
break;
53+
}
54+
55+
SaveTasks();
56+
}
57+
58+
static void LoadTasks()
59+
{
60+
if (File.Exists(FileName))
61+
{
62+
string json = File.ReadAllText(FileName);
63+
tasks = JsonSerializer.Deserialize<List<TaskItem>>(json) ?? new();
64+
}
65+
}
66+
67+
static void SaveTasks()
68+
{
69+
string json = JsonSerializer.Serialize(tasks, new JsonSerializerOptions { WriteIndented = true });
70+
File.WriteAllText(FileName, json);
71+
}
72+
73+
static void AddTask(string description)
74+
{
75+
int nextId = tasks.Count == 0 ? 1 : tasks[^1].Id + 1;
76+
tasks.Add(new TaskItem { Id = nextId, Description = description });
77+
Console.WriteLine($"Added task #{nextId}: {description}");
78+
}
79+
80+
static void ListTasks()
81+
{
82+
if (tasks.Count == 0)
83+
{
84+
Console.WriteLine("No tasks found.");
85+
return;
86+
}
87+
88+
foreach (var task in tasks)
89+
{
90+
Console.WriteLine($"[{task.Id}] {task.Description}");
91+
}
92+
}
93+
94+
static void RemoveTask(int id)
95+
{
96+
var task = tasks.Find(t => t.Id == id);
97+
if (task == null)
98+
{
99+
Console.WriteLine("Task not found.");
100+
return;
101+
}
102+
103+
tasks.Remove(task);
104+
Console.WriteLine($"Removed task #{id}");
105+
}
106+
}

0 commit comments

Comments
 (0)