Exercise Tasks - C# Programming Exercise

This exercise in C# consists of creating a program that can store up to 2000 "to-do tasks". Each task must store the following data: • Date (a set of 3 data: day, month, and year).
• Task description.
• Level of importance (from 1 to 10).
Category.
The program should allow the user to perform the following operations:

1 - Add a new task (the date must "seem correct": day from 1 to 31, month from 1 to 12, year between 1000 and 3000).
2 - Show tasks between two specific dates (day, month, and year). If the user presses Enter without specifying a 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 on the same line, separated by hyphens.
3 - Find tasks that contain a certain text (in description or category, not case sensitive). It will display the number, date, and description (only the first 50 letters, in case it is longer). If no results are found, the user should be notified.
4 - Update a record (it will ask for the number, display the previous value of each field, and the user can press Enter to not modify any data). The user should be warned (but not asked again) if an incorrect record number is entered. No validation of the fields is necessary.
5 - Delete some data between two positions indicated by the user. The user should be warned (but not asked again) if an incorrect record number is entered. Each record to be deleted must be displayed, and the user must confirm the deletion.
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).

 Category

Arrays, Structures and Strings

 Exercise

Exercise Tasks

 Objective

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 C# Exercise

 Copy C# 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
    }
}

 Output

Task Manager
1. Add Task
2. List Tasks
3. Delete Task
4. Sort Tasks
5. Find Duplicate Tasks
Q. Quit
Choose an option: 1
Enter task description:
John
Enter task date (DD/MM/YYYY):
01/01/1999
Task added.

 Share this C# Exercise

 More C# Programming Exercises of Arrays, Structures and Strings

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#.

  •  Household accounts

    This exercise in C# consists of creating a program that can store up to 10,000 costs and revenues, to create a small domestic accounting system. For eac...

  •  Reverse array

    In this C# exercise, you are asked to write a program that prompts the user for 5 numbers, stores them in an array, and displays them in reverse order. ...

  •  Search in array

    In this C# exercise, you are asked to write a program that checks if a given data belongs to a previously created list. The steps to follow are:

  •  Array of even numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 integer numbers and displays the even ones. This exercise w...

  •  Array of positive and negative numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 real numbers and displays the average of the positive ones and the average of t...

  •  Many numbers and sum

    In this C# exercise, you are asked to write a program that asks the user for several numbers (until they enter the word "end") and then displays the sum of the...