Text Practice Mode
C# practice
created Jul 22nd, 15:40 by Zaphod Beeblebrox
1
641 words
2 completed
0
Rating visible after 3 or more votes
saving score / loading statistics ...
00:00
// This text is designed to help you practice touch typing common C# syntax
// within the context of a simple console application.
using System;
using System.Collections.Generic;
using System.Linq; // Used for LINQ methods like .Any() and .Where()
// 1. Define a Task class to represent individual tasks
public class Task
{
// Auto-implemented properties for task details
public int Id { get; } // Unique identifier for the task
public string Description { get; set; } // What the task is about
public bool IsCompleted { get; private set; } // Status of the task
public DateTime DueDate { get; set; } // When the task is due
// Static counter to generate unique IDs for new tasks
private static int _nextId = 1;
// Constructor to initialize a new Task object
public Task(string description, DateTime dueDate)
{
Id = _nextId++; // Assign current ID and then increment for the next task
Description = description;
IsCompleted = false; // New tasks are not completed by default
DueDate = dueDate;
}
// Method to mark a task as completed
public void MarkAsCompleted()
{
IsCompleted = true;
Console.WriteLine($"Task '{Description}' (ID: {Id}) marked as completed.");
}
// Method to display task information
public override string ToString()
{
// Format the output string for better readability
string status = IsCompleted ? "[COMPLETED]" : "[PENDING]";
return $"ID: {Id}, Description: {Description}, Due: {DueDate.ToShortDateString()}, Status: {status}";
}
}
// 2. Define a TaskManager class to handle task operations
public class TaskManager
{
// A list to store all the tasks
private List<Task> _tasks;
// Constructor to initialize the TaskManager
public TaskManager()
{
_tasks = new List<Task>();
}
// Method to add a new task to the manager
public void AddTask(string description, DateTime dueDate)
{
Task newTask = new Task(description, dueDate);
_tasks.Add(newTask);
Console.WriteLine($"Added new task: '{description}' (ID: {newTask.Id})");
}
// Method to view all tasks, optionally filtering by completion status
public void ViewTasks(bool showCompleted = true)
{
Console.WriteLine("\n--- Your Tasks ---");
if (!_tasks.Any()) // Check if there are any tasks using LINQ
{
Console.WriteLine("No tasks available.");
return;
}
// Filter tasks based on showCompleted flag
var tasksToDisplay = showCompleted ? _tasks : _tasks.Where(t => !t.IsCompleted);
foreach (var task in tasksToDisplay)
{
Console.WriteLine(task.ToString());
}
Console.WriteLine("------------------\n");
}
// Method to complete a task by its ID
public void CompleteTask(int taskId)
{
// Find the task using LINQ's FirstOrDefault
Task taskToComplete = _tasks.FirstOrDefault(t => t.Id == taskId);
if (taskToComplete != null) // Check if the task was found
{
if (!taskToComplete.IsCompleted)
{
taskToComplete.MarkAsCompleted();
}
else
{
Console.WriteLine($"Task ID {taskId} is already completed.");
}
}
else
{
Console.WriteLine($"Error: Task with ID {taskId} not found.");
}
}
// Method to remove a task by its ID
public void RemoveTask(int taskId)
{
// Find the task to remove
Task taskToRemove = _tasks.FirstOrDefault(t => t.Id == taskId);
if (taskToRemove != null)
{
_tasks.Remove(taskToRemove); // Remove the task from the list
Console.WriteLine($"Task '{taskToRemove.Description}' (ID: {taskId}) has been removed.");
}
else
{
Console.WriteLine($"Error: Task with ID {taskId} not found for removal.");
}
}
}
// 3. Main program entry point
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to the C# Task Manager!");
TaskManager manager = new TaskManager(); // Create an instance of the TaskManager
// Adding some initial tasks for demonstration
manager.AddTask("Learn C# basics", DateTime.Now.AddDays(7));
manager.AddTask("Build a console app", DateTime.Now.AddDays(14));
manager.AddTask("Refactor old code", DateTime.Now.AddDays(21));
manager.AddTask("Write unit tests", DateTime.Now.AddDays(30));
// Display all current tasks
manager.ViewTasks();
// Simulate completing a task
manager.CompleteTask(1); // Mark "Learn C# basics" as completed
// Display tasks again, showing the updated status
manager.ViewTasks();
// Try to complete an already completed task
manager.CompleteTask(1);
// Display only pending tasks
Console.WriteLine("\n--- Pending Tasks Only ---");
manager.ViewTasks(showCompleted: false);
// Simulate removing a task
manager.RemoveTask(3); // Remove "Refactor old code"
// Display tasks after removal
manager.ViewTasks();
// Try to remove a non-existent task
manager.RemoveTask(99);
Console.WriteLine("\nTask Manager demonstration complete. Keep practicing your C# typing!");
}
}
// within the context of a simple console application.
using System;
using System.Collections.Generic;
using System.Linq; // Used for LINQ methods like .Any() and .Where()
// 1. Define a Task class to represent individual tasks
public class Task
{
// Auto-implemented properties for task details
public int Id { get; } // Unique identifier for the task
public string Description { get; set; } // What the task is about
public bool IsCompleted { get; private set; } // Status of the task
public DateTime DueDate { get; set; } // When the task is due
// Static counter to generate unique IDs for new tasks
private static int _nextId = 1;
// Constructor to initialize a new Task object
public Task(string description, DateTime dueDate)
{
Id = _nextId++; // Assign current ID and then increment for the next task
Description = description;
IsCompleted = false; // New tasks are not completed by default
DueDate = dueDate;
}
// Method to mark a task as completed
public void MarkAsCompleted()
{
IsCompleted = true;
Console.WriteLine($"Task '{Description}' (ID: {Id}) marked as completed.");
}
// Method to display task information
public override string ToString()
{
// Format the output string for better readability
string status = IsCompleted ? "[COMPLETED]" : "[PENDING]";
return $"ID: {Id}, Description: {Description}, Due: {DueDate.ToShortDateString()}, Status: {status}";
}
}
// 2. Define a TaskManager class to handle task operations
public class TaskManager
{
// A list to store all the tasks
private List<Task> _tasks;
// Constructor to initialize the TaskManager
public TaskManager()
{
_tasks = new List<Task>();
}
// Method to add a new task to the manager
public void AddTask(string description, DateTime dueDate)
{
Task newTask = new Task(description, dueDate);
_tasks.Add(newTask);
Console.WriteLine($"Added new task: '{description}' (ID: {newTask.Id})");
}
// Method to view all tasks, optionally filtering by completion status
public void ViewTasks(bool showCompleted = true)
{
Console.WriteLine("\n--- Your Tasks ---");
if (!_tasks.Any()) // Check if there are any tasks using LINQ
{
Console.WriteLine("No tasks available.");
return;
}
// Filter tasks based on showCompleted flag
var tasksToDisplay = showCompleted ? _tasks : _tasks.Where(t => !t.IsCompleted);
foreach (var task in tasksToDisplay)
{
Console.WriteLine(task.ToString());
}
Console.WriteLine("------------------\n");
}
// Method to complete a task by its ID
public void CompleteTask(int taskId)
{
// Find the task using LINQ's FirstOrDefault
Task taskToComplete = _tasks.FirstOrDefault(t => t.Id == taskId);
if (taskToComplete != null) // Check if the task was found
{
if (!taskToComplete.IsCompleted)
{
taskToComplete.MarkAsCompleted();
}
else
{
Console.WriteLine($"Task ID {taskId} is already completed.");
}
}
else
{
Console.WriteLine($"Error: Task with ID {taskId} not found.");
}
}
// Method to remove a task by its ID
public void RemoveTask(int taskId)
{
// Find the task to remove
Task taskToRemove = _tasks.FirstOrDefault(t => t.Id == taskId);
if (taskToRemove != null)
{
_tasks.Remove(taskToRemove); // Remove the task from the list
Console.WriteLine($"Task '{taskToRemove.Description}' (ID: {taskId}) has been removed.");
}
else
{
Console.WriteLine($"Error: Task with ID {taskId} not found for removal.");
}
}
}
// 3. Main program entry point
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to the C# Task Manager!");
TaskManager manager = new TaskManager(); // Create an instance of the TaskManager
// Adding some initial tasks for demonstration
manager.AddTask("Learn C# basics", DateTime.Now.AddDays(7));
manager.AddTask("Build a console app", DateTime.Now.AddDays(14));
manager.AddTask("Refactor old code", DateTime.Now.AddDays(21));
manager.AddTask("Write unit tests", DateTime.Now.AddDays(30));
// Display all current tasks
manager.ViewTasks();
// Simulate completing a task
manager.CompleteTask(1); // Mark "Learn C# basics" as completed
// Display tasks again, showing the updated status
manager.ViewTasks();
// Try to complete an already completed task
manager.CompleteTask(1);
// Display only pending tasks
Console.WriteLine("\n--- Pending Tasks Only ---");
manager.ViewTasks(showCompleted: false);
// Simulate removing a task
manager.RemoveTask(3); // Remove "Refactor old code"
// Display tasks after removal
manager.ViewTasks();
// Try to remove a non-existent task
manager.RemoveTask(99);
Console.WriteLine("\nTask Manager demonstration complete. Keep practicing your C# typing!");
}
}
