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