To-Do List: Follow-Along Activity
Create a program that keep track of a user's to-do list.
Part 1 - Setup
Set up the menu for the application.
- In the
Main
method, display a message to the user welcoming them to the application - Under the welcome message, create an infinite
while
loop- This will allow the user to continue entering commands
- Within the body of the infinite
while
loop, ask the user for a command to execute, and store their input in a variable - The program should be able to handle 3 commands: "Add", "View", and "Exit"
- Create an
if
statement to handle a command of "Add" - Create an
else if
to handle a command of "View" - Create another
else if
to handle a command of "Exit"
- Create an
- If the user enters a command of "Exit",
break
out of the infinite loop
Code
Console.WriteLine("Welcome to the To-Do List!");
while (true)
{
Console.WriteLine("Enter a command:");
string command = Console.ReadLine();
if (command == "Add")
{
// Add
}
else if (command == "View")
{
// View
}
else if (command == "Exit")
{
break;
}
}
Part 2 - The List
The program needs a List
to keep track of what the user wants to do!
List Setup
- At the top of the
Program.cs
file, add ausing
statement that will importList
s:using System.Collections.Generic;
- At the top of the body of the
Main
method, declare a newList
ofstring
s and initialize it to an empty list:List<string> toDoList = new List<string>();
Adding to the List
- Within the
Main
method, in the body of theif
statement for "Add", ask the user what they want to do - Store the user's input in a variable
- Add the input item to the
toDoList
!
Code
Console.WriteLine("Enter an item:");
string item = Console.ReadLine();
toDoList.Add(item);
Part 3 - Viewing the List
Once the user has added some items to the list, they will want a way to view those items!
- In the
Program
class, define a method namedViewList
- The method should return
void
(nothing) - The method should take a
List<string>
as a parameter
- The method should return
- In the body of the
ViewList
method, print some text to the console saying "To Do:" - Under the "To Do" message, create a
foreach
loop that will loop through the listforeach
keyword- Initialize a new
string
variable in
keyword- Use the parameter variable for the list
- Curly brackets
- In the body of the
foreach
loop, print the list item, prefaced by "- " - In the
Main
method, in theelse if
for "View", call theViewList
method withtoDoList
as the argument
Now the program should work properly!
Code
using System;
using System.Collections.Generic;
namespace Week09
{
class Program
{
static void Main(string[] args)
{
List<string> toDoList = new List<string>();
Console.WriteLine("Welcome to the To-Do List!");
while (true)
{
Console.WriteLine("Enter a command:");
string command = Console.ReadLine();
if (command == "Add")
{
Console.WriteLine("Enter an item:");
string item = Console.ReadLine();
toDoList.Add(item);
}
else if (command == "View")
{
ViewList(toDoList);
}
else if (command == "Exit")
{
break;
}
}
}
public static void ViewList(List<string> stringList)
{
Console.WriteLine("To Do:");
foreach (string item in stringList)
{
Console.WriteLine("- " + item);
}
}
}
}