Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions tdd-todo-list.CSharp.Main/Task.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
public enum TaskStatus
{
Incomplete,
Complete
}

public enum Priority
{
Low,
Medium,
High
}

public enum Category
{
Work,
Shopping,
Reading
}

public class Task
{
private static int _idCounter = 1;
public int Id { get; set; }
public string Title { get; set; }
public TaskStatus Status { get; set; }
public Priority Priority { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? CompletedAt { get; set; }
public Category Category { get; set; }

public Task(string title, Priority priority, Category category)
{
Id = _idCounter++;
Title = title;
Priority = priority;
Status = TaskStatus.Incomplete;
CreatedAt = DateTime.Now;
Category = category;
CompletedAt = null;
}

public void MarkComplete()
{
Status = TaskStatus.Complete;
CompletedAt = DateTime.Now;
}

public void MarkIncomplete()
{
Status = TaskStatus.Incomplete;
CompletedAt = null;
}

public static void ResetIdCounter()
{
_idCounter = 1;
}
}
213 changes: 206 additions & 7 deletions tdd-todo-list.CSharp.Main/ToDoList.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,211 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_todo_list.CSharp.Main
namespace tdd_todo_list.CSharp.Main
{

public class TodoList
{
private List<Task> ListOfTasks = new List<Task>();

public List<Task> GetAll()
{
return ListOfTasks;
}

public List<Task> SortAscending()
{
return ListOfTasks.OrderBy(t => t.Title).ToList();
}

public List<Task> SortDescending()
{
return ListOfTasks.OrderByDescending(t => t.Title).ToList();
}

public Task FindByTitle(string title)
{
foreach (var task in ListOfTasks)
{
if (task.Title.Equals(title, StringComparison.OrdinalIgnoreCase))
{
return task;
}
}
return null;

Check warning on line 32 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 32 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 32 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 32 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

// Extension
public Task FindById(int id)
{
foreach (var task in ListOfTasks)
{
if (task.Id == id)
{
return task;
}
}
return null;

Check warning on line 45 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 45 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

public bool Remove(int taskId)
{
var taskToRemove = ListOfTasks.FirstOrDefault(t => t.Id == taskId);
if (taskToRemove != null)
{
ListOfTasks.Remove(taskToRemove);
return true;
}
return false;
}

// Extension
public bool UpdateTitle(int id, string newTitle)
{
foreach (var task in ListOfTasks)
{
if (task.Id == id)
{
task.Title = newTitle;
return true;
}
}
return false;
}

// Extension
public bool ChangeStatus(int id, TaskStatus status)
{
foreach (var task in ListOfTasks)
{
if (task.Id == id)
{
task.Status = status;
return true;
}
}
return false;
}


public void Add(string title, Priority priority, Category category)
{
Task task = new Task(title, priority, category);
ListOfTasks.Add(task);
}

public List<Task> GetCompleted()
{
List<Task> completedTasks = new List<Task>();
foreach (var task in ListOfTasks)
{
if (task.Status == TaskStatus.Complete)
{
completedTasks.Add(task);
}
}
return completedTasks;
}

public List<Task> GetInComplete()
{
List<Task> incompleteTasks = new List<Task>();
foreach (var task in ListOfTasks)
{
if (task.Status == TaskStatus.Incomplete)
{
incompleteTasks.Add(task);
}
}
return incompleteTasks;
}

public List<Task> ListByPriority(Priority priority)
{
List<Task> priorityTasks = new List<Task>();
foreach (var task in ListOfTasks)
{
if (task.Priority == priority)
{
priorityTasks.Add(task);
}
}
return priorityTasks;
}


// Extension
public List<Task> ListByCategory(Category category)
{
List<Task> listByCategory = new List<Task>();
foreach (var task in ListOfTasks) {
if (task.Category == category)
{
listByCategory.Add(task);
}
}
return listByCategory;
}


// Extension
public Task LongestTask()
{
TimeSpan timeToCompleted;
TimeSpan currentLongest = TimeSpan.Zero;
Task longestTask = null;

Check warning on line 153 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 153 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
foreach (var task in ListOfTasks)
{
if (task.CompletedAt != null)
{
timeToCompleted = (TimeSpan)(task.CompletedAt - task.CreatedAt);
if (timeToCompleted > currentLongest)
{
currentLongest = timeToCompleted;
longestTask = task;
}
}

}
return longestTask;

Check warning on line 167 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 167 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

// Extension
public Task ShortestTask()
{
TimeSpan timeToCompleted;
TimeSpan currentLongest = TimeSpan.MaxValue;
Task longestTask = null;

Check warning on line 175 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 175 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
foreach (var task in ListOfTasks)
{
if (task.CompletedAt != null)
{
timeToCompleted = (TimeSpan)(task.CompletedAt - task.CreatedAt);
if (timeToCompleted < currentLongest)
{
currentLongest = timeToCompleted;
longestTask = task;
}
}

}
return longestTask;

Check warning on line 189 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 189 in tdd-todo-list.CSharp.Main/ToDoList.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

// Extension
public List<Task> TasksLongerThan(TimeSpan duration)
{
TimeSpan timeToCompleted;
List<Task> listTasksLongerThanDuration = new List<Task>();
foreach (var task in ListOfTasks)
{
if (task.CompletedAt != null)
{
timeToCompleted = (TimeSpan)(task.CompletedAt - task.CreatedAt);
if (timeToCompleted > duration)
{
listTasksLongerThanDuration.Add(task);
}
}
}
return listTasksLongerThanDuration;
}
}
}
27 changes: 27 additions & 0 deletions tdd-todo-list.CSharp.Main/domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
| **Class** | **Method** / Property | **Scenario** | **Output** / **Description** |
| ---------- | ----------------------------------------- | ----------------------------------------------------- | ---------------------------- |
| `ToDoList` | `Add(string title, Priority priority)` | Add a task with a title and priority | `void` |
| `ToDoList` | `Remove(int taskId)` | Remove a task from the list | `bool` (success/fail) |
| `ToDoList` | `GetAll()` | View all tasks | `List<Task>` |
| `ToDoList` | `GetCompleted()` | View only completed tasks | `List<Task>` |
| `ToDoList` | `GetIncomplete()` | View only incomplete tasks | `List<Task>` |
| `ToDoList` | `FindByTitle(string title)` | Search for a task by its title | `Task` or `null` |
| `ToDoList` | `FindById(int id)` | Search for a task by its unique ID | `Task` or `null` |
| `ToDoList` | `UpdateTitle(int id, string newTitle)` | Update the name of a task by providing its ID | `bool` (success/fail) |
| `ToDoList` | `ChangeStatus(int id, TaskStatus status)` | Change the status of a task by providing its ID | `bool` (success/fail) |
| `ToDoList` | `SortAscending()` | View all tasks sorted alphabetically A → Z | `List<Task>` |
| `ToDoList` | `SortDescending()` | View all tasks sorted alphabetically Z → A | `List<Task>` |
| `ToDoList` | `ListByPriority(Priority priority)` | View tasks filtered by priority | `List<Task>` |
| `ToDoList` | `ListByCategory(Category category)` | View tasks filtered by category | `List<Task>` |
| `ToDoList` | `LongestTask()` | Find the task that took the longest time to complete | `Task` |
| `ToDoList` | `ShortestTask()` | Find the task that took the shortest time to complete | `Task` |
| `ToDoList` | `TasksLongerThan(TimeSpan duration)` | Find tasks that took longer than a specified duration | `List<Task>` |
| `Task` | `Id` | Unique identifier for each task | `int` |
| `Task` | `Title` | The name/title of the task | `string` |
| `Task` | `Status` | Current status (Complete/Incomplete) | `TaskStatus` |
| `Task` | `Priority` | Priority level of the task | `Priority` |
| `Task` | `Category` | Category of the task (e.g., Study, Work, Admin) | `Category` |
| `Task` | `CreatedAt` | Date and time the task was created | `DateTime` |
| `Task` | `CompletedAt` | Date and time the task was completed | `DateTime?` (nullable) |
| `Task` | `MarkComplete()` | Mark a task as complete and record completion time | `void` |
| `Task` | `MarkIncomplete()` | Mark a task as incomplete and reset completion time | `void` |
Loading
Loading