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
Mainmethod, display a message to the user welcoming them to the application - Under the welcome message, create an infinite
whileloop- This will allow the user to continue entering commands
- Within the body of the infinite
whileloop, 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
ifstatement to handle a command of "Add" - Create an
else ifto handle a command of "View" - Create another
else ifto handle a command of "Exit"
- Create an
- If the user enters a command of "Exit",
breakout 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.csfile, add ausingstatement that will importLists:using System.Collections.Generic; - At the top of the body of the
Mainmethod, declare a newListofstrings and initialize it to an empty list:List<string> toDoList = new List<string>();
Adding to the List
- Within the
Mainmethod, in the body of theifstatement 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
Programclass, 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
ViewListmethod, print some text to the console saying "To Do:" - Under the "To Do" message, create a
foreachloop that will loop through the listforeachkeyword- Initialize a new
stringvariable inkeyword- Use the parameter variable for the list
- Curly brackets
- In the body of the
foreachloop, print the list item, prefaced by "- " - In the
Mainmethod, in theelse iffor "View", call theViewListmethod withtoDoListas 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);
}
}
}
}