diff --git a/Todo-Challenge/Todo-Challenge/Menu.cs b/Todo-Challenge/Todo-Challenge/Menu.cs new file mode 100644 index 0000000..c5d4402 --- /dev/null +++ b/Todo-Challenge/Todo-Challenge/Menu.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Todo_Challenge +{ + class Menu + { + public static void ShowOptions() + { + Console.WriteLine("Here are the options you can select:"); + Console.WriteLine(); + Console.WriteLine("0 - exit"); + Console.WriteLine("1 - view TODO list"); + Console.WriteLine("2 - add new item into TODO list"); + Console.WriteLine("3 - remove item from TODO list\n"); + } + + public static int AskOption() + { + while (true) + { + Console.Write("Select Option: "); + string userInput = Console.ReadLine(); + + bool optionRes = int.TryParse(userInput, out int option); + + if (!optionRes) + { + Console.WriteLine("\nInput was not an integer :(\n"); + continue; + } + + if (option < 0 || option > 3) + { + Console.WriteLine($"\nOption {option} unavaliable :(\n"); + continue; + } + + return option; + } + } + + public static void OptionResponse(List todoList, int option) + { + switch (option) + { + case 0: + Environment.Exit(0); + break; + + case 1: + ShowTodoItems(todoList); + + AskToContinue(); + break; + + case 2: + Console.Write("\nEnter name of the Todo Item: "); + string name = Console.ReadLine(); + if (name.Length == 0) + { + Console.WriteLine("\nName cannot be empty :(\n"); + AskToContinue(); + break; + } + + Console.Write("Is this Todo completed? y/n: "); + string completedInput = Console.ReadLine(); + if (completedInput != "y" && completedInput != "n") + { + Console.WriteLine("\nInvalid completed field :(\n"); + AskToContinue(); + break; + } + + bool completed = completedInput == "y" ? true : false; + + todoList.Add(new TodoItem(name, completed)); + + Console.WriteLine("\nTodo Item have been added to the Todo List successfully :)\n"); + + AskToContinue(); + break; + + case 3: + ShowTodoItems(todoList); + + if (todoList.Count == 0) + { + AskToContinue(); + break; + } + + Console.Write("\nEnter name of the Todo Item which you would lite to remove: "); + string nameToRemove = Console.ReadLine(); + + if (todoList.Any(item => item.Name == nameToRemove)) + { + var itemToRemove = todoList.Single(item => item.Name == nameToRemove); + + todoList.Remove(itemToRemove); + + Console.WriteLine("\nTodo Item have been removed from the Todo List successfully :)\n"); + } + else + { + Console.WriteLine($"\nItem with name {nameToRemove} doesn't exist :(\n"); + } + + AskToContinue(); + break; + } + } + + private static void AskToContinue() + { + Console.Write("Press Enter to continue: "); + Console.ReadLine(); + Console.Clear(); + } + + private static void ShowTodoItems(List todoList) + { + if (todoList.Count > 0) + { + foreach (var item in todoList) + { + Console.WriteLine($"\nName: {item.Name}\n Completed: {item.Completed}\n"); + } + } + else + { + Console.WriteLine("\nYour TodoList is empty :(\n"); + } + } + + } +} \ No newline at end of file diff --git a/Todo-Challenge/Todo-Challenge/Program.cs b/Todo-Challenge/Todo-Challenge/Program.cs index 3751555..04c62f6 100644 --- a/Todo-Challenge/Todo-Challenge/Program.cs +++ b/Todo-Challenge/Todo-Challenge/Program.cs @@ -1,2 +1,21 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using System.ComponentModel.Design; +using System.Text; +using Todo_Challenge; + +class Program +{ + static void Main() + { + List todoList = new List(); + + while (true) + { + new Menu(); + Menu.ShowOptions(); + + int option = Menu.AskOption(); + + Menu.OptionResponse(todoList, option); + } + } +} \ No newline at end of file diff --git a/Todo-Challenge/Todo-Challenge/TodoItem.cs b/Todo-Challenge/Todo-Challenge/TodoItem.cs new file mode 100644 index 0000000..b061b3d --- /dev/null +++ b/Todo-Challenge/Todo-Challenge/TodoItem.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Todo_Challenge +{ + internal class TodoItem + { + public string Name { get; set; } + public bool Completed { get; set; } = false; + + public TodoItem(string name, bool completed) + { + Name = name; + Completed = completed; + } + + } +} \ No newline at end of file