-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
211 lines (185 loc) · 6.24 KB
/
Program.cs
File metadata and controls
211 lines (185 loc) · 6.24 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
namespace TaskFlow;
/// <summary>
/// The main entry point for the TaskFlow application.
/// </summary>
public class Program
{
/// <summary>
/// The main execution method of the application.
/// </summary>
/// <param name="args">Command line arguments.</param>
static void Main(string[] args)
{
TaskService taskService = new TaskService();
// Main loop
while (true)
{
Console.WriteLine(GetMenuPrompt());
string? input = GetInput("");
switch (input.Trim())
{
case "1":
string message = $"You have {taskService.GetAllTasks().Count()} tasks remaining.\n";
Console.WriteLine(DisplayAllTasks(taskService: taskService, message: message));
break;
case "2":
HandleAddTaskIO(taskService);
break;
case "3":
HandleCompleteATaskIO(taskService);
break;
case "4":
HandleDeleteATaskIO(taskService);
break;
case "5":
Exit();
break;
default:
Console.WriteLine(GetErrorMessage());
break;
}
}
}
/// <summary>
/// Gets the menu prompt for the main application loop.
/// </summary>
/// <returns>A string containing the menu options.</returns>
static string GetMenuPrompt()
{
return
@"TaskFlow task manager
What would you like to do:
1 -> View tasks
2 -> Add a task
3 -> Complete a task
4 -> Delete a task
5 -> Exit
";
}
/// <summary>
/// Displays all tasks currently in the task service.
/// </summary>
/// <param name="taskService">The task service instance.</param>
/// <param name="message">A message to display before the list of tasks.</param>
/// <returns>A formatted string of all tasks.</returns>
static string DisplayAllTasks(TaskService taskService, string message)
{
string text = message;
List<Task> tasks = taskService.GetAllTasks();
for (int i = 0; i < tasks.Count(); i++)
{
text += $"{i + 1} - {tasks[i]}\n";
}
return text;
}
/// <summary>
/// Handles the input/output for adding a new task.
/// </summary>
/// <param name="taskService">The task service instance.</param>
static void HandleAddTaskIO(TaskService taskService)
{
string title = GetInput("Title");
string description = GetInput("Description");
taskService.AddTask(title: title, description: description);
Console.WriteLine("Task added.");
}
/// <summary>
/// Selects a task and performs an action (delete or complete) based on user input.
/// </summary>
/// <param name="taskService">The task service instance.</param>
/// <param name="delete">If true, the selected task will be deleted; otherwise, it will be marked as complete.</param>
static void SelectAndProcessTask(TaskService taskService, bool delete = false)
{
string action = delete ? "delete" : "mark as complete";
List<Task> tasks = taskService.GetAllTasks();
if (tasks.Count == 0)
{
Console.WriteLine($"There are currently no tasks available to {action}.");
return;
}
int taskNumber;
Task task;
while (true)
{
string prompt = DisplayAllTasks(taskService: taskService, message: $"Choose a task to {action}\n");
Console.WriteLine(prompt);
string input = GetInput("");
try
{
taskNumber = Convert.ToInt32(input) - 1;
task = tasks[taskNumber];
break;
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine($"Task does not exist yet. Please choose a number from the provided list.");
}
catch (FormatException)
{
Console.WriteLine("Invalid input. Please choose a number from the provided list.");
}
catch (Exception)
{
Console.WriteLine("Something went wrong. Please try again.");
}
}
if (delete)
taskService.DeleteTask(task);
else
taskService.CompleteTask(task);
string message = delete ? $"You deleted '{task.Title}'." : $"Well done! You completed '{task.Title}'.";
Console.WriteLine(message);
}
/// <summary>
/// Handles the input/output for deleting a task.
/// </summary>
/// <param name="taskService">The task service instance.</param>
static void HandleDeleteATaskIO(TaskService taskService)
{
SelectAndProcessTask(taskService, delete: true);
}
/// <summary>
/// Handles the input/output for deleting a task
/// </summary>
/// <param name="taskService">The task service instance</param>
static void HandleCompleteATaskIO(TaskService taskService)
{
SelectAndProcessTask(taskService, delete: false);
}
/// <summary>
/// Gets input from the console with a prompt message.
/// </summary>
/// <param name="message">The prompt message to display.</param>
/// <returns>The user input string.</returns>
static string GetInput(string message)
{
string? input;
do
{
Console.Write(message.Trim() == "" ? "" : $"{message}: ");
input = Console.ReadLine();
if (input is null)
Exit();
else if (input == "")
Console.WriteLine(GetErrorMessage());
}
while (input == "" || input is null);
return input;
}
/// <summary>
/// Gets the standard error message for invalid input.
/// </summary>
/// <returns>The error message string.</returns>
static string GetErrorMessage()
{
return "Input cannot be empty. Please try again.\n";
}
/// <summary>
/// Exits the application cleanly.
/// </summary>
static void Exit()
{
Console.WriteLine("Thank you for using the program :)");
Environment.Exit(0);
}
}