ideas | discover | explore

Thursday, November 24, 2011

C# 13 - Collections - Todo List


1. Create a new console project. 
2. Edit the Program.cs to look like as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TodoList
{
    class Program
    {
        private static List<string> todolist = new List<string>();
 
        static void Main(string[] args)
        {            
            string menuOption;
            bool exitApplication = false;
 
            do
            {
                Console.WriteLine("===========================");
                Console.WriteLine("Welcome to your Todo List.");
                Console.WriteLine("===========================");                
                Console.WriteLine("[1] Show Todo List");
                Console.WriteLine("[2] Add a new Todo");
                Console.WriteLine("[3] Delete a Todo");
                Console.WriteLine("[4] Exit");
                Console.Write("What do you want to do? ");
                menuOption = Console.ReadLine();
 
                switch(menuOption)
                {
                    case "1":
                        Console.WriteLine("---------------------------");
                        ShowTodoList();
                        Console.WriteLine("---------------------------");
                        break;
                    case "2":
                        Console.WriteLine("---------------------------");
                        AddNewTodo();
                        Console.WriteLine("---------------------------");
                        break;
                    case "3":
                        Console.WriteLine("---------------------------");
                        DeleteATodo();
                        Console.WriteLine("---------------------------");
                        break;
                    case "4":
                        Console.WriteLine("---------------------------");
                        Console.WriteLine("Exit");
                        Console.WriteLine("---------------------------");
                        exitApplication = true;
                        break;
                    default:
                        Console.WriteLine("Invalid option");
                        break;
                }                
 
            } while (!exitApplication);
        }
 
        private static void DeleteATodo()
        {
            string todoNumberInput;
            int todoNumber;
 
            ShowTodoList();
            Console.Write("What Todo do you want to delete? ");
            todoNumberInput = Console.ReadLine();
 
            if (Int32.TryParse(todoNumberInput, out todoNumber))
            {
                if (todoNumber > todolist.Count)
                    Console.WriteLine("Todo does not exist.");
                else
                    todolist.RemoveAt(todoNumber);
            }
            else
            {
                Console.WriteLine("Input must be a number");
            }
        }

 
        private static void AddNewTodo()
        {
            string newTodo;
 
            Console.Write("What will you be doing? ");
            newTodo = Console.ReadLine();
            todolist.Add(newTodo);
        }
 
        private static void ShowTodoList()
        {
            int todoNumber = 1;
 
            if (todolist.Count == 0)
            {
                Console.WriteLine("Your Todo list is empty");
            }
            else
            {
                foreach (string todo in todolist)
                {
                    Console.WriteLine("[" + todoNumber + "] " + todo);
                    todoNumber++;
                }
            }
        }
        
    }
}

3. Press F5 to run and see the results.


4. Thank you.

Learn more about Collections.




No comments: