This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
359 lines (315 loc) · 12.4 KB
/
Program.cs
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
// Austin Wheeler
// EXTRA CREDIT: Todo List
// Monday, May 1, 2023
// NOTES ----------------------------------------------------------------------/
/*
*
*/
// MAIN -----------------------------------------------------------------------/
// Read Todo List to Memory ---------------------------------------------------/
List<string> todoList = new List<string>(File.ReadAllLines("todo-list.txt"));
// Display Todo List & Menu ---------------------------------------------------/
bool error = false;
string message = null;
while (true) {
Console.Clear();
// Display Todo List
Console.WriteLine("Here is your Todo List:");
Console.ForegroundColor = ConsoleColor.DarkGray;
DisplayTodoList(-1);
Console.ResetColor();
// Display Any Messages
if (error) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
error = false;
message = null;
} else {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(message);
Console.ResetColor();
message = null;
}
// Display Instructions
Console.WriteLine("What would you like to do? (1) Add Todo, (2) Remove Todo, (3) Edit Todo, (4) Exit");
Console.Write("Enter a Number: ");
ConsoleKeyInfo key = Console.ReadKey();
switch (key.KeyChar) {
// Add Todo
case '1':
AddTodo();
break;
// Remove Todo
case '2':
RemoveTodo();
break;
// Edit Todo
case '3':
EditTodo();
break;
// Exit
case '4':
Console.Clear();
return;
default:
error = true;
message = "Invalid Input. Please Try Again.";
break;
}
}
// METHOD: Display Todo List --------------------------------------------------/
void DisplayTodoList(int position) {
Console.ForegroundColor = ConsoleColor.DarkGray;
// Display Entire List if Position is Not Specified
if (position == -1) {
// Check if Todo List is Empty
if (todoList.Count == 0) {
Console.WriteLine("Your Todo List is Empty!");
} else {
// Display Todo List
for (int i = 0; i < todoList.Count; i++) {
Console.WriteLine($"{i + 1}. {todoList[i]}");
}
}
} else {
// Display Todo at Specified Position
Console.WriteLine($"{position + 1}. {todoList[position]}");
}
Console.ResetColor();
Console.WriteLine();
}
// METHOD: Add Todo -----------------------------------------------------------/
void AddTodo() {
Console.Clear();
Console.WriteLine("What would you like to add to your Todo List?");
Console.Write("Enter a Todo: ");
string todo = Console.ReadLine();
// Check if Todo is Empty
if (todo == "") {
error = true;
message = "Todo Cannot Be Empty. Please Try Again.";
return;
}
// Add Status to Todo
todo = $"Incomplete: {todo}";
// Add Todo to Memory
todoList.Add(todo);
// Add Todo to File
File.AppendAllText("todo-list.txt", $"{todo}\n");
message = "Todo Added Successfully!";
return;
}
// METHOD: Remove Todo --------------------------------------------------------/
void RemoveTodo() {
// Display Todo List & Ask For Selection ----------------------------------/
bool removalError = false;
string removalMessage = null;
while (true) {
Console.Clear();
// Display Todo List
Console.WriteLine("Please Select a Todo to Remove:");
DisplayTodoList(-1);
// Display Any Error Messages
if (removalError) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(removalMessage);
Console.ResetColor();
removalError = false;
removalMessage = null;
}
// Ask For Selection
Console.Write("Enter a Number: ");
string selectionString = Console.ReadLine();
// Check For Valid Selection
if (selectionString != "") {
if (int.TryParse(selectionString, out int selection)) {
if (selection > 0 && selection <= todoList.Count) {
// Confirm Selection
Console.Clear();
Console.WriteLine("Are you sure you want to remove the following Todo?");
// Display Todo
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($"{selection}. {todoList[selection - 1]}");
Console.ResetColor();
// Ask For Confirmation
Console.WriteLine();
Console.Write("Enter Y/N: ");
ConsoleKeyInfo key = Console.ReadKey();
// Remove Todo
if (key.KeyChar == 'y' || key.KeyChar == 'Y') {
// Remove Todo From Memory
todoList = todoList.Where((source, index) => index != selection - 1).ToList();
// Remove Todo From File
File.WriteAllLines("todo-list.txt", todoList);
message = "Todo Removed Successfully!";
return;
} else if (key.KeyChar == 'n' || key.KeyChar == 'N') {
removalError = true;
removalMessage = "Todo Not Removed.";
return;
} else {
removalError = true;
removalMessage = "Invalid Input. Please Try Again.";
return;
}
} else {
removalError = true;
removalMessage = "Selection Must Be an Existing Todo. Please Try Again.";
}
} else {
removalError = true;
removalMessage = "Input Must Be a Number. Please Try Again.";
}
} else {
removalError = true;
removalMessage = "Selection Cannot Be Empty. Please Try Again.";
}
}
}
// METHOD: Edit Todo ----------------------------------------------------------/
void EditTodo() {
// Display Todo List & Ask For Selection ----------------------------------/
bool selectionError = false;
string selectionMessage = null;
while (true) {
Console.Clear();
// Display Todo List
Console.WriteLine("Please Select a Todo to Edit:");
DisplayTodoList(-1);
// Display Any Error Messages
if (selectionError) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(selectionMessage);
Console.ResetColor();
selectionError = false;
selectionMessage = null;
}
// Ask For Selection
Console.Write("Enter a Number: ");
string selectionString = Console.ReadLine();
// Check For Valid Selection
if (selectionString != "") {
if (int.TryParse(selectionString, out int selection)) {
if (selection > 0 && selection <= todoList.Count) {
// Display Todo & Edit Options
bool editError = false;
string editMessage = null;
while (true) {
Console.Clear();
// Display Todo
DisplayTodoList(selection - 1);
// Display Any Messages
if (editError) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(editMessage);
Console.ResetColor();
error = false;
message = null;
}
// Display Options
Console.WriteLine();
Console.WriteLine("What would you like to do? (1) Mark as Complete, (2) Mark as Incomplete, (3) Edit Todo, (4) Exit");
Console.Write("Enter a Number: ");
ConsoleKeyInfo key = Console.ReadKey();
switch (key.KeyChar) {
// Mark Complete
case '1':
markComplete(todoList[selection - 1], selection - 1);
return;
// Mark Incomplete
case '2':
markIncomplete(todoList[selection - 1] , selection - 1);
break;
// Edit Todo
case '3':
edit(todoList[selection - 1], selection - 1);
break;
// Exit
case '4':
Console.Clear();
return;
default:
editError = true;
editMessage = "Invalid Input. Please Try Again.";
break;
}
}
} else {
selectionError = true;
selectionMessage = "Selection Must Be an Existing Todo. Please Try Again.";
}
} else {
selectionError = true;
selectionMessage = "Input Must Be a Number. Please Try Again.";
}
} else {
selectionError = true;
selectionMessage = "Selection Cannot Be Empty. Please Try Again.";
}
}
}
// METHOD: Mark Complete ------------------------------------------------------/
void markComplete(string todo, int position) {
// Mark Complete in Memory
todo = todo.Replace("Incomplete", "Complete");
todoList[position] = todo;
// Mark Complete in File
File.WriteAllLines("todo-list.txt", todoList);
message = "Todo Marked Complete Successfully!";
return;
}
// METHOD: Mark Incomplete ----------------------------------------------------/
void markIncomplete(string todo, int position) {
// Mark Incomplete in Memory
todo = todo.Replace("Complete", "Incomplete");
todoList[position] = todo;
// Mark Incomplete in File
File.WriteAllLines("todo-list.txt", todoList);
message = "Todo Marked Incomplete Successfully!";
return;
}
// METHOD: Edit ---------------------------------------------------------------/
void edit(string todo, int position) {
// Seperate Status & Todo -------------------------------------------------/
string[] todoArray = todo.Split(": ");
string status = todoArray[0];
todo = todoArray[1];
// Display Todo & Ask For New Todo ----------------------------------------/
bool editError = false;
string editMessage = null;
while (true) {
Console.Clear();
// Display Todo
Console.Write($"Current Todo: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($"{todo}");
Console.ResetColor();
// Display Any Error Messages
if (editError) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(editMessage);
Console.ResetColor();
editError = false;
editMessage = null;
}
// Ask For New Todo
Console.WriteLine();
Console.WriteLine("What would you like to change your Todo to?");
Console.Write("Enter a Todo: ");
string newTodo = Console.ReadLine();
// Check if Todo is Empty
if (newTodo == "") {
editError = true;
editMessage = "Todo Cannot Be Empty. Please Try Again.";
} else {
// Add Status to Todo
newTodo = $"{status}: {newTodo}";
// Edit Todo in Memory
todoList[position] = newTodo;
// Edit Todo in File
File.WriteAllLines("todo-list.txt", todoList);
return;
}
}
}