Exercise
Function Tasks Database
Objective
Write in C# an improved version of the "tasks database", splitting it into functions.
Example C# Exercise
Show C# Code
// Importing necessary namespaces
using System;
using System.Collections.Generic;
class Program
{
// List to store tasks
static List tasks = new List();
// Main method to drive the program
public static void Main()
{
// Display the menu and interact with the user
int choice;
do
{
choice = DisplayMenu();
HandleMenuChoice(choice);
}
while (choice != 4); // Exit the loop when user chooses to quit
}
// Display menu to the user
public static int DisplayMenu()
{
// Display available options
Console.WriteLine("\nTask Manager");
Console.WriteLine("1. Add a new task");
Console.WriteLine("2. View all tasks");
Console.WriteLine("3. Remove a task");
Console.WriteLine("4. Exit");
Console.Write("Enter your choice: ");
string input = Console.ReadLine();
// Try to parse user input to an integer
if (int.TryParse(input, out int choice))
{
return choice;
}
else
{
Console.WriteLine("Invalid choice, please try again.");
return 0; // Return invalid choice to keep the menu displayed
}
}
// Handle user's menu choice
public static void HandleMenuChoice(int choice)
{
switch (choice)
{
case 1:
AddTask(); // Call the AddTask function
break;
case 2:
ViewTasks(); // Call the ViewTasks function
break;
case 3:
RemoveTask(); // Call the RemoveTask function
break;
case 4:
Console.WriteLine("Exiting..."); // Exit message
break;
default:
Console.WriteLine("Invalid choice, please try again."); // Invalid choice handling
break;
}
}
// Add a new task to the list
public static void AddTask()
{
Console.Write("Enter the task description: ");
string task = Console.ReadLine();
if (!string.IsNullOrEmpty(task))
{
tasks.Add(task); // Add the task to the list
Console.WriteLine("Task added successfully.");
}
else
{
Console.WriteLine("Task description cannot be empty.");
}
}
// Display all tasks in the list
public static void ViewTasks()
{
if (tasks.Count == 0)
{
Console.WriteLine("No tasks available.");
}
else
{
Console.WriteLine("\nTasks:");
for (int i = 0; i < tasks.Count; i++)
{
Console.WriteLine($"{i + 1}. {tasks[i]}"); // Display task with its index
}
}
}
// Remove a task from the list
public static void RemoveTask()
{
Console.Write("Enter the number of the task to remove: ");
string input = Console.ReadLine();
if (int.TryParse(input, out int taskNumber) && taskNumber > 0 && taskNumber <= tasks.Count)
{
tasks.RemoveAt(taskNumber - 1); // Remove task by its index (1-based to 0-based index)
Console.WriteLine("Task removed successfully.");
}
else
{
Console.WriteLine("Invalid task number.");
}
}
}
Output
Task Manager
1. Add a new task
2. View all tasks
3. Remove a task
4. Exit
Enter your choice: 1
Enter the task description: Jupiter
Task added successfully.
Task Manager
1. Add a new task
2. View all tasks
3. Remove a task
4. Exit
Enter your choice: 2
Tasks:
1. Jupiter
Task Manager
1. Add a new task
2. View all tasks
3. Remove a task
4. Exit
Enter your choice: 4
Exiting...
More C# Programming Exercises of Functions
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...
In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...
In this exercise of C#, you will learn how to create a function named "WriteTitle" that writes a text centered on the screen, in uppercase, with extra spaces, ...
In this exercise of C#, you will learn how to create a program in which you can use the WriteTitle function to write a title that the user will specify ...
In this exercise of C#, you will learn how to create a function that calculates the amount of numeric digits and vowels a text string contains. The function...
In this exercise of C#, you will learn how to create a function that tells if a character is alphabetic (from A to Z) or not. This function should be us...