Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions Todo-Challenge/Todo-Challenge/Menu.cs
Original file line number Diff line number Diff line change
@@ -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<TodoItem> 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<TodoItem> 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");
}
}

}
}
23 changes: 21 additions & 2 deletions Todo-Challenge/Todo-Challenge/Program.cs
Original file line number Diff line number Diff line change
@@ -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<TodoItem> todoList = new List<TodoItem>();

while (true)
{
new Menu();
Menu.ShowOptions();

int option = Menu.AskOption();

Menu.OptionResponse(todoList, option);
}
}
}
21 changes: 21 additions & 0 deletions Todo-Challenge/Todo-Challenge/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}