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).
Write Your C# Exercise
C# Exercise Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public class Task
{
public DateTime Date { get; set; }
public string Description { get; set; }
public int Importance { get; set; }
public string Category { get; set; }
public Task(DateTime date, string description, int importance, string category)
{
Date = date;
Description = description;
Importance = importance;
Category = category;
}
public override string ToString()
{
return $"{Date:dd/MM/yyyy} - {Description.Substring(0, Math.Min(50, Description.Length))} - {Category} - {Importance}";
}
}
static List tasks = new List();
static void Main()
{
char option;
do
{
ShowMenu();
option = Console.ReadKey(true).KeyChar;
switch (option)
{
case '1':
AddTask();
break;
case '2':
ShowTasksBetweenDates();
break;
case '3':
FindTasks();
break;
case '4':
UpdateTask();
break;
case '5':
DeleteTask();
break;
case '6':
SortTasks();
break;
case '7':
FindDuplicates();
break;
case 'Q':
case 'q':
break;
default:
Console.WriteLine("Invalid option! Please try again.");
break;
}
} while (option != 'Q' && option != 'q');
}
static void ShowMenu()
{
Console.Clear();
Console.WriteLine("1 - Add a new task");
Console.WriteLine("2 - Show tasks between two dates");
Console.WriteLine("3 - Find tasks by description or category");
Console.WriteLine("4 - Update a task");
Console.WriteLine("5 - Delete tasks");
Console.WriteLine("6 - Sort tasks by date and description");
Console.WriteLine("7 - Find duplicate tasks by description");
Console.WriteLine("Q - Quit");
Console.Write("Enter your choice: ");
}
static void AddTask()
{
Console.Clear();
int day, month, year, importance;
string description, category;
Console.Write("Enter task date (Day, Month, Year): ");
string[] dateParts = Console.ReadLine().Split('/');
day = int.Parse(dateParts[0]);
month = int.Parse(dateParts[1]);
year = int.Parse(dateParts[2]);
if (!IsValidDate(day, month, year))
{
Console.WriteLine("Invalid date! Please try again.");
return;
}
DateTime taskDate = new DateTime(year, month, day);
Console.Write("Enter task description (max 100 chars): ");
description = Console.ReadLine();
if (description.Length > 100)
description = description.Substring(0, 100);
Console.Write("Enter task importance (1-10): ");
importance = int.Parse(Console.ReadLine());
Console.Write("Enter task category: ");
category = Console.ReadLine();
tasks.Add(new Task(taskDate, description, importance, category));
Console.WriteLine("Task added successfully.");
Console.ReadKey();
}
static bool IsValidDate(int day, int month, int year)
{
if (year < 1000 || year > 3000)
return false;
try
{
DateTime testDate = new DateTime(year, month, day);
return true;
}
catch
{
return false;
}
}
static void ShowTasksBetweenDates()
{
Console.Clear();
Console.Write("Enter start date (Day/Month/Year) or press Enter to use today: ");
string startDateInput = Console.ReadLine();
DateTime startDate = DateTime.Now;
if (!string.IsNullOrEmpty(startDateInput))
{
string[] startParts = startDateInput.Split('/');
int startDay = int.Parse(startParts[0]);
int startMonth = int.Parse(startParts[1]);
int startYear = int.Parse(startParts[2]);
startDate = new DateTime(startYear, startMonth, startDay);
}
Console.Write("Enter end date (Day/Month/Year) or press Enter to use today: ");
string endDateInput = Console.ReadLine();
DateTime endDate = DateTime.Now;
if (!string.IsNullOrEmpty(endDateInput))
{
string[] endParts = endDateInput.Split('/');
int endDay = int.Parse(endParts[0]);
int endMonth = int.Parse(endParts[1]);
int endYear = int.Parse(endParts[2]);
endDate = new DateTime(endYear, endMonth, endDay);
}
var filteredTasks = tasks.Where(t => t.Date >= startDate && t.Date <= endDate).ToList();
if (filteredTasks.Count == 0)
{
Console.WriteLine("No tasks found for the given dates.");
}
else
{
foreach (var task in filteredTasks)
{
Console.WriteLine(task);
}
}
Console.ReadKey();
}
static void FindTasks()
{
Console.Clear();
Console.Write("Enter text to search in description or category: ");
string searchText = Console.ReadLine().ToLower();
var matchedTasks = tasks.Where(t => t.Description.ToLower().Contains(searchText) || t.Category.ToLower().Contains(searchText)).ToList();
if (matchedTasks.Count == 0)
{
Console.WriteLine("No tasks found.");
}
else
{
foreach (var task in matchedTasks)
{
Console.WriteLine($"{task.Date:dd/MM/yyyy} - {task.Description.Substring(0, Math.Min(50, task.Description.Length))} - {task.Category}");
}
}
Console.ReadKey();
}
static void UpdateTask()
{
Console.Clear();
Console.Write("Enter task number to update: ");
int taskNum = int.Parse(Console.ReadLine()) - 1;
if (taskNum < 0 || taskNum >= tasks.Count)
{
Console.WriteLine("Invalid task number!");
Console.ReadKey();
return;
}
var task = tasks[taskNum];
Console.WriteLine($"Current task: {task.Date:dd/MM/yyyy} - {task.Description} - {task.Category} - {task.Importance}");
Console.Write("Enter new description (leave blank to keep the current): ");
string description = Console.ReadLine();
if (!string.IsNullOrEmpty(description)) task.Description = description;
Console.Write("Enter new importance (leave blank to keep the current): ");
string importanceInput = Console.ReadLine();
if (!string.IsNullOrEmpty(importanceInput)) task.Importance = int.Parse(importanceInput);
Console.Write("Enter new category (leave blank to keep the current): ");
string category = Console.ReadLine();
if (!string.IsNullOrEmpty(category)) task.Category = category;
Console.WriteLine("Task updated.");
Console.ReadKey();
}
static void DeleteTask()
{
Console.Clear();
Console.Write("Enter task number to delete: ");
int taskNum = int.Parse(Console.ReadLine()) - 1;
if (taskNum < 0 || taskNum >= tasks.Count)
{
Console.WriteLine("Invalid task number!");
Console.ReadKey();
return;
}
Console.WriteLine($"Deleting task: {tasks[taskNum]}");
Console.Write("Are you sure you want to delete? (Y/N): ");
char confirm = Console.ReadKey(true).KeyChar;
if (confirm == 'Y' || confirm == 'y')
{
tasks.RemoveAt(taskNum);
Console.WriteLine("Task deleted.");
}
Console.ReadKey();
}
static void SortTasks()
{
Console.Clear();
tasks = tasks.OrderBy(t => t.Date).ThenBy(t => t.Description).ToList();
Console.WriteLine("Tasks sorted.");
Console.ReadKey();
}
static void FindDuplicates()
{
Console.Clear();
var duplicateTasks = tasks.GroupBy(t => t.Description)
.Where(g => g.Count() > 1)
.SelectMany(g => g)
.ToList();
if (duplicateTasks.Count == 0)
{
Console.WriteLine("No duplicates found.");
}
else
{
foreach (var task in duplicateTasks)
{
Console.WriteLine(task);
}
}
Console.ReadKey();
}
}