-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.cs
More file actions
535 lines (455 loc) · 17.1 KB
/
Copy pathTaskManager.cs
File metadata and controls
535 lines (455 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace TaskManagerCSharp
{
internal class TaskItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Priority { get; set; }
public int Difficulty { get; set; }
public bool IsDone { get; set; }
public DateTime CreatedAt { get; set; }
}
internal class SaveData
{
public List<TaskItem> Tasks { get; set; }
public int NextId { get; set; }
}
internal class TaskManager
{
private List<TaskItem> _tasks = new List<TaskItem>();
private List<string> _history = new List<string>();
private int _nextId = 1;
private string _saveFile = "tasks.json";
public TaskManager()
{
LoadFromFile();
}
public void AddTask(string titleFromCommand, string priorityFromCommand)
{
Console.WriteLine();
Console.WriteLine("--- New Task ---");
// title
string title = titleFromCommand;
if (title == "")
{
Console.Write("Title: ");
title = Console.ReadLine();
if (title == "")
{
Console.WriteLine("Title cannot be empty. Task cancelled.");
return;
}
}
else
{
Console.WriteLine("Title: " + title);
}
Console.Write("Description (press Enter to skip): ");
string description = Console.ReadLine();
string priority = priorityFromCommand.ToLower();
if (priority != "low" && priority != "normal" && priority != "high")
{
Console.Write("Priority (low / normal / high): ");
priority = Console.ReadLine().ToLower();
if (priority != "low" && priority != "normal" && priority != "high")
{
Console.WriteLine("Wrong priority. Using normal.");
priority = "normal";
}
}
else
{
Console.WriteLine("Priority: " + priority);
}
Console.Write("Difficulty (1-5): ");
string diffInput = Console.ReadLine();
int difficulty = 0;
bool diffParsed = int.TryParse(diffInput, out difficulty);
if (!diffParsed || difficulty < 1 || difficulty > 5)
{
Console.WriteLine("Wrong difficulty. Using 1.");
difficulty = 1;
}
Console.WriteLine("----------------");
TaskItem task = new TaskItem();
task.Id = _nextId;
task.Title = title;
task.Description = description;
task.Priority = priority;
task.Difficulty = difficulty;
task.IsDone = false;
task.CreatedAt = DateTime.Now;
_tasks.Add(task);
_nextId++;
Console.WriteLine("Task added --> [" + task.Id + "] " + task.Title);
SaveToFile();
}
public void ListTasks(string filter)
{
filter = filter.ToLower();
List<TaskItem> result = new List<TaskItem>();
if (filter == "all")
{
result = _tasks;
}
else if (filter == "done")
{
foreach (TaskItem t in _tasks)
{
if (t.IsDone)
result.Add(t);
}
}
else if (filter == "todo")
{
foreach (TaskItem t in _tasks)
{
if (!t.IsDone)
result.Add(t);
}
}
else if (filter == "low" || filter == "normal" || filter == "high")
{
foreach (TaskItem t in _tasks)
{
if (t.Priority == filter)
result.Add(t);
}
}
else
{
Console.WriteLine("Unknown filter. Use: all, done, todo, low, normal, high");
return;
}
if (result.Count == 0)
{
Console.WriteLine("No tasks found.");
return;
}
Console.WriteLine();
foreach (TaskItem t in result)
{
string status = "TODO";
if (t.IsDone)
status = "DONE";
string stars = "";
if (t.Difficulty == 0)
stars = "-----";
else
{
for (int i = 1; i <= 5; i++)
{
if (i <= t.Difficulty)
stars += "*";
else
stars += "-";
}
}
string prio = t.Priority.ToUpper().PadRight(6);
string id = ("#" + t.Id).PadRight(4);
if (t.IsDone)
Console.ForegroundColor = ConsoleColor.DarkGray;
else if (t.Priority == "high")
Console.ForegroundColor = ConsoleColor.Red;
else if (t.Priority == "low")
Console.ForegroundColor = ConsoleColor.Cyan;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(id + " [" + status + "] " + prio + " [" + stars + "] " + t.Title);
if (t.Description != null && t.Description != "")
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(" >> " + t.Description);
}
Console.ResetColor();
Console.WriteLine();
}
Console.WriteLine("Total: " + result.Count + " task(s)");
Console.WriteLine();
}
public void MarkDone(string idStr)
{
TaskItem task = FindTask(idStr);
if (task == null)
return;
if (task.IsDone)
{
Console.WriteLine("Already done --> [" + task.Id + "] " + task.Title);
return;
}
task.IsDone = true;
Console.WriteLine("Marked as done --> [" + task.Id + "] " + task.Title);
SaveToFile();
}
public void MarkUndone(string idStr)
{
TaskItem task = FindTask(idStr);
if (task == null)
return;
if (!task.IsDone)
{
Console.WriteLine("Already not done --> [" + task.Id + "] " + task.Title);
return;
}
task.IsDone = false;
Console.WriteLine("Marked as not done --> [" + task.Id + "] " + task.Title);
SaveToFile();
}
public void DeleteTask(string idStr)
{
TaskItem task = FindTask(idStr);
if (task == null)
return;
_tasks.Remove(task);
RecalculateNextId();
Console.WriteLine("Task deleted --> [" + task.Id + "] " + task.Title);
SaveToFile();
}
public void EditTask(string idStr, string newTitle)
{
TaskItem task = FindTask(idStr);
if (task == null)
return;
string oldTitle = task.Title;
task.Title = newTitle;
Console.WriteLine("Task updated --> " + oldTitle + " --> " + newTitle);
SaveToFile();
}
public void ChangePriority(string idStr, string priority)
{
TaskItem task = FindTask(idStr);
if (task == null)
return;
priority = priority.ToLower();
if (priority != "low" && priority != "normal" && priority != "high")
{
Console.WriteLine("Priority must be: low, normal or high");
return;
}
task.Priority = priority;
Console.WriteLine("Priority changed --> [" + task.Id + "] " + task.Title + " is now " + priority);
SaveToFile();
}
public void Search(string text)
{
Console.WriteLine();
Console.WriteLine("Searching --> " + text);
Console.WriteLine();
int found = 0;
foreach (TaskItem t in _tasks)
{
if (t.Title.ToLower().Contains(text.ToLower()))
{
string status = "TODO";
if (t.IsDone)
status = "DONE";
string stars = "";
if (t.Difficulty == 0)
stars = "-----";
else
{
for (int i = 1; i <= 5; i++)
{
if (i <= t.Difficulty)
stars += "*";
else
stars += "-";
}
}
string id = ("#" + t.Id).PadRight(4);
if (t.IsDone)
Console.ForegroundColor = ConsoleColor.DarkGray;
else if (t.Priority == "high")
Console.ForegroundColor = ConsoleColor.Red;
else if (t.Priority == "low")
Console.ForegroundColor = ConsoleColor.Cyan;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(id + " [" + status + "] " + t.Priority.ToUpper().PadRight(6) + " [" + stars + "] " + t.Title);
if (t.Description != null && t.Description != "")
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(" >> " + t.Description);
}
Console.ResetColor();
Console.WriteLine();
found++;
}
}
Console.WriteLine();
Console.WriteLine("Found " + found + " result(s)");
Console.WriteLine();
}
public void ClearDone()
{
int count = 0;
for (int i = _tasks.Count - 1; i >= 0; i--)
{
if (_tasks[i].IsDone)
{
_tasks.RemoveAt(i);
count++;
}
}
if (count == 0)
Console.WriteLine("No completed tasks to remove.");
else
{
RecalculateNextId();
Console.WriteLine("Removed " + count + " completed task(s).");
SaveToFile();
}
}
private void RecalculateNextId()
{
if (_tasks.Count == 0)
{
_nextId = 1;
return;
}
int maxId = 0;
foreach (TaskItem t in _tasks)
{
if (t.Id > maxId)
maxId = t.Id;
}
_nextId = maxId + 1;
}
private TaskItem FindTask(string idStr)
{
int id = 0;
bool parsed = int.TryParse(idStr, out id);
if (!parsed)
{
Console.WriteLine("Invalid ID --> " + idStr);
return null;
}
foreach (TaskItem t in _tasks)
{
if (t.Id == id)
return t;
}
Console.WriteLine("Task not found --> #" + id);
return null;
}
private void SaveToFile()
{
SaveData data = new SaveData();
data.Tasks = _tasks;
data.NextId = _nextId;
string json = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(_saveFile, json);
}
private void LoadFromFile()
{
if (!File.Exists(_saveFile))
return;
string json = File.ReadAllText(_saveFile);
SaveData data = JsonSerializer.Deserialize<SaveData>(json);
if (data == null)
return;
_tasks = data.Tasks;
RecalculateNextId();
Console.WriteLine("Loaded " + _tasks.Count + " task(s) from " + _saveFile);
}
public void AddHistory(string command)
{
_history.Add(command);
}
public void ShowHistory()
{
if (_history.Count == 0)
{
Console.WriteLine("History is empty.");
return;
}
Console.WriteLine();
for (int i = 0; i < _history.Count; i++)
{
Console.WriteLine((i + 1) + ". " + _history[i]);
}
Console.WriteLine();
}
public void ShowHelp()
{
Console.WriteLine();
Console.WriteLine("Available commands:");
Console.WriteLine("-------------------");
Console.WriteLine("add - add new task");
Console.WriteLine("list - show tasks");
Console.WriteLine("done - mark task as done");
Console.WriteLine("undone - mark task as not done");
Console.WriteLine("del - delete task");
Console.WriteLine("edit - edit task title");
Console.WriteLine("priority - change task priority");
Console.WriteLine("search - search tasks by text");
Console.WriteLine("clear - remove all completed tasks");
Console.WriteLine("history - show command history");
Console.WriteLine("cls - clear screen");
Console.WriteLine("help - show this help");
Console.WriteLine("exit - exit program");
Console.WriteLine();
}
public void ShowHelpDetail(string command)
{
Console.WriteLine();
switch (command.ToLower())
{
case "add":
Console.WriteLine("add - adds a new task");
Console.WriteLine("usage: add <title> [low/normal/high]");
Console.WriteLine("example: add \"Buy groceries\" high");
break;
case "list":
Console.WriteLine("list - shows tasks");
Console.WriteLine("usage: list [all/done/todo/low/normal/high]");
Console.WriteLine("example: list high");
break;
case "done":
Console.WriteLine("done - marks task as completed");
Console.WriteLine("usage: done <id>");
break;
case "undone":
Console.WriteLine("undone - marks task as not completed");
Console.WriteLine("usage: undone <id>");
break;
case "del":
Console.WriteLine("del - deletes task by id");
Console.WriteLine("usage: del <id>");
break;
case "edit":
Console.WriteLine("edit - changes task title");
Console.WriteLine("usage: edit <id> <new title>");
break;
case "priority":
Console.WriteLine("priority - changes task priority");
Console.WriteLine("usage: priority <id> <low/normal/high>");
break;
case "search":
Console.WriteLine("search - finds tasks that contain the text");
Console.WriteLine("usage: search <text>");
break;
case "clear":
Console.WriteLine("clear - removes all tasks marked as done");
break;
case "history":
Console.WriteLine("history - shows all commands typed this session");
break;
case "cls":
Console.WriteLine("cls - clears the screen");
break;
default:
Console.WriteLine("No help for --> " + command);
break;
}
Console.WriteLine();
}
}
}