Exercise tasks C# Exercise - C# Programming Course

 Exercise

Exercise tasks

 Objetive

Write a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data:

• Date (a set of 3 data: day, month and year)
• Description of task
• Level of importance (1 to 10)
• Category

The program should allow the user the following operations:

1 - Add a new task (the date must "seem correct": day 1 to 31, month 1 to 12, year between 1000 and 3000).

2 - Show the tasks between two certain dates (day, month and year). If the user presses Enter without specifying date, it will be taken as "today". It must display the number of each record, the date (DD/ MM/YYYY), description, category and importance, all in the same line, separated with hyphens.

3 - Find tasks that contain a certain text (in description or category, not case sensitive). It will display number, date and description (only 50 letters, in case it was be longer). The user should be notified if none is found.

4 - Update a record (it will ask for the number, will display the previous value of each field and the user can press Enter not to modify any of the data). The user should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.

5 - Delete some data, between two positions indicated by the user. The user should be warned (but not asked again) if he enters any incorrect record number. Each record to be deleted must be displayed, and the user must be asked for confirmation.

6 - Sort the data alphabetically by date and (if two dates are the same) by description.

7 - Find Duplicates: If two records have the same description, both will be displayed on-screen.

Q - Quit (end the application; as we do not store the information, it will be lost).

(Hint: you can know the current date using DateTime.Now.Day, DateTime.Now.Month and DateTime.Now.Year).

 Example Code

using System;
using System.Collections.Generic;
using System.Linq;

class Task
{
    public string Description { get; set; }
    public DateTime Date { get; set; }

    // Constructor to initialize a new task with description and date
    public Task(string description, DateTime date)
    {
        Description = description;
        Date = date;
    }

    // Override ToString() to display task details
    public override string ToString()
    {
        return $"{Description} - {Date.ToShortDateString()}"; 
    }
}

class Program
{
    static void Main()
    {
        List tasks = new List(); // List to store tasks
        string option;

        do
        {
            Console.WriteLine("Task Manager");
            Console.WriteLine("1. Add Task");
            Console.WriteLine("2. List Tasks");
            Console.WriteLine("3. Delete Task");
            Console.WriteLine("4. Sort Tasks");
            Console.WriteLine("5. Find Duplicate Tasks");
            Console.WriteLine("Q. Quit");
            Console.Write("Choose an option: ");
            option = Console.ReadLine().ToUpper(); // Read and convert input to uppercase

            switch (option)
            {
                case "1":
                    // Add new task
                    Console.WriteLine("Enter task description:");
                    string description = Console.ReadLine();

                    Console.WriteLine("Enter task date (DD/MM/YYYY):");
                    string dateInput = Console.ReadLine();

                    if (DateTime.TryParse(dateInput, out DateTime date)) // Parse the date input
                    {
                        tasks.Add(new Task(description, date));
                        Console.WriteLine("Task added.");
                    }
                    else
                    {
                        Console.WriteLine("Invalid date format.");
                    }
                    Console.ReadKey();
                    break;

                case "2":
                    // List all tasks
                    Console.WriteLine("Tasks:");
                    foreach (var task in tasks)
                    {
                        Console.WriteLine(task); // Display each task
                    }
                    Console.ReadKey();
                    break;

                case "3":
                    // Delete a task
                    Console.WriteLine("Enter the number of the task to delete:");
                    int taskNum;
                    if (int.TryParse(Console.ReadLine(), out taskNum) && taskNum > 0 && taskNum <= tasks.Count)
                    {
                        tasks.RemoveAt(taskNum - 1); // Remove the selected task
                        Console.WriteLine("Task deleted.");
                    }
                    else
                    {
                        Console.WriteLine("Invalid task number.");
                    }
                    Console.ReadKey();
                    break;

                case "4":
                    // Sort tasks by date and description
                    Console.WriteLine("Sorted Tasks:");
                    var sortedTasks = tasks.OrderBy(t => t.Date).ThenBy(t => t.Description).ToList();
                    foreach (var task in sortedTasks)
                    {
                        Console.WriteLine(task); // Display sorted tasks
                    }
                    Console.ReadKey();
                    break;

                case "5":
                    // Find duplicate tasks by description
                    var duplicateTasks = tasks.GroupBy(t => t.Description)
                        .Where(g => g.Count() > 1)
                        .SelectMany(g => g)
                        .ToList();

                    if (duplicateTasks.Count == 0)
                    {
                        Console.WriteLine("No duplicate tasks found.");
                    }
                    else
                    {
                        foreach (var task in duplicateTasks)
                        {
                            Console.WriteLine(task); // Display duplicate tasks
                        }
                    }
                    Console.ReadKey();
                    break;

                case "Q":
                    // Exit the program
                    Console.WriteLine("Exiting...");
                    break;

                default:
                    Console.WriteLine("Invalid choice. Please try again.");
                    Console.ReadKey();
                    break;
            }

        } while (option != "Q"); // Loop until the user selects 'Q' to quit
    }
}

More C# Exercises of Arrays, Structures and Strings

 Reverse array
Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he ...
 Array of even numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Write a C# program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must...
 Two dimensional array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Statistics V2
Write a C# statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been en...
 Struct
Write a C# Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Write a C# program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the use...
 Array of struct and menu
Write a C# program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for...
 Books database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
 Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
 Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
 Centered triangle
Write a C# program that Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Write a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Displ...
 Nested structs
Write a C# Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month an...
 Sort data
Write a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Write a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the...
 Two dimensional array 2: circunference on screen
Write a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it o...
 Computer programs
Write a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
 Household accounts
Write a C# program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), sh...

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.