-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
202 lines (192 loc) · 5.96 KB
/
Program.cs
File metadata and controls
202 lines (192 loc) · 5.96 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
using System.Collections.Generic;
using System.Threading.Tasks.Dataflow;
using System.IO;
using System.Text.Json;
public struct Todo
{
public string Name { get; set; }
public Boolean Completion { get; set; }
public Todo(string name, Boolean completion)
{
Name = name;
Completion = completion;
}
public override string ToString()
{
return $"Task: {Name}, Completed: {Completion}";
}
}
public class Program
{
public static bool IsTodos(List<Todo> Todos)
{
if (Todos.Count == 0)
{
Console.WriteLine("No todos right now.");
return false;
}
return true;
}
public static int ChooseTodo(List<Todo> Todos)
{
for (int i = 0; i < Todos.Count; i++)
{
Console.WriteLine($"{i + 1}. {Todos[i]}");
}
string? toEditStr = "1";
if (Todos.Count > 1)
{
Console.WriteLine($"\nNumber (1-{Todos.Count}):");
toEditStr = Console.ReadLine();
}
bool isInt = int.TryParse(toEditStr, out int toEdit);
bool withinRange = toEdit > 0 && toEdit <= Todos.Count;
while (!isInt || !withinRange)
{
Console.WriteLine($"Please enter a postive whole number (1-{Todos.Count}):");
toEditStr = Console.ReadLine();
isInt = int.TryParse(toEditStr, out toEdit);
withinRange = toEdit > 0 && toEdit <= Todos.Count;
}
return toEdit - 1;
}
public static List<Todo> EditTodos(List<Todo> Todos)
{
if (!IsTodos(Todos)) return Todos;
Console.WriteLine("Choose a todo to edit.");
int ToEdit = ChooseTodo(Todos);
Todo todo = Todos[ToEdit];
Console.WriteLine($"You chose '{todo.Name}'.");
Console.WriteLine();
Console.WriteLine("What would you like to do? (toggle, remove, rename, exit)");
string? command = Console.ReadLine();
switch (command)
{
case "toggle":
if (todo.Completion)
{
todo.Completion = false;
Console.WriteLine($"'{todo.Name}' is now toggled to false.");
}
else
{
todo.Completion = true;
Console.WriteLine($"'{todo.Name}' is now toggled to true.");
}
Todos[ToEdit] = todo;
break;
case "remove":
Console.WriteLine($"Are you sure you want to remove '{todo.Name}'? (Confirm with 'yes')");
string? confirmation = Console.ReadLine();
if (confirmation == "yes")
{
Console.WriteLine($"The todo, '{todo.Name}', is now removed.");
Todos.Remove(todo);
}
else
{
Console.WriteLine($"The todo, '{todo.Name}', was not deleted.");
}
break;
case "rename":
Console.WriteLine($"Write a new name for '{todo.Name}':");
string? newName = Console.ReadLine();
if (newName == "" || newName == null)
{
Console.WriteLine($"No new name was chosen for '{todo.Name}'");
}
else
{
Console.WriteLine($"'{todo.Name}' was successfully changed to '{newName}");
todo.Name = newName;
Todos[ToEdit] = todo;
}
break;
case "exit":
Console.WriteLine("Returning to main interface.");
break;
default:
Console.WriteLine("Unknown command.");
break;
}
return Todos;
}
public static Todo MakeTodo(string? name)
{
if (name == "" || name == null)
{
name = "No name";
}
Todo newTodo = new Todo(name, false);
return newTodo;
}
public static void PrintTodos(List<Todo> Todos)
{
if (!IsTodos(Todos)) return;
for (int i = 0; i < Todos.Count(); i++)
{
Console.WriteLine($"{i + 1}. {Todos[i]}");
}
}
public static void saveTodos(List<Todo> Todos)
{
string path = @".\todos.json";
try
{
string jsonString = JsonSerializer.Serialize<List<Todo>>(Todos);
File.WriteAllText(path, jsonString);
}
catch (IOException e)
{
Console.WriteLine(e);
}
}
public static List<Todo> loadTodos()
{
string path = @".\todos.json";
try
{
string jsonString = File.ReadAllText(path);
List<Todo>? NewTodos = JsonSerializer.Deserialize<List<Todo>>(jsonString);
if (NewTodos != null)
{
return NewTodos;
}
return [];
}
catch (Exception)
{
saveTodos([]);
return [];
}
}
public static void Main()
{
List<Todo> Todos = loadTodos();
Console.WriteLine("Let's get some stuff done!");
string? input = "none";
while (input != "exit")
{
Console.WriteLine();
Console.WriteLine("Write a new todo or choose a command (print, edit, exit).");
input = Console.ReadLine();
switch (input)
{
case "print":
PrintTodos(Todos);
break;
case "edit":
Todos = EditTodos(Todos);
break;
case "exit":
Console.WriteLine("Goodbye!");
break;
default:
Todos.Add(MakeTodo(input));
Console.WriteLine($"Added new todo, '{input}'!");
break;
}
saveTodos(Todos);
}
}
}