Building an Todo List Application in TypeScript
- Published on
- • 4 mins read•––– views
To-Do List Adventure
Greetings, fellow procrastinators! Today, we embark on a Java journey to conquer the chaos of our lives with the power of... a To-Do List! 📝
The Cast of Characters Task: Meet our little minions of productivity. Each task has a title (like "Conquer the world") and a secret mission: to be completed or not to be completed, that is the question! 🤨
Category: The taskmaster! Categories are like the generals of our to-do army. They organize tasks into armies, uh, I mean categories, with names like "Personal" or "Work."
ToDoList: The grand organizer. This is our commander-in-chief, managing our categories and ensuring that no task is left behind!
import java.util.ArrayList;
import java.util.List;
// Task class to represent individual tasks
class Task {
private String title;
private boolean completed;
public Task(String title) {
this.title = title;
this.completed = false;
}
public String getTitle() {
return title;
}
public boolean isCompleted() {
return completed;
}
public void markCompleted() {
completed = true;
}
@Override
public String toString() {
return title + (completed ? " (Completed)" : "");
}
}
// Category class to group tasks
class Category {
private String name;
private List<Task> tasks;
public Category(String name) {
this.name = name;
this.tasks = new ArrayList<>();
}
public String getName() {
return name;
}
public void addTask(Task task) {
tasks.add(task);
}
public List<Task> getTasks() {
return tasks;
}
@Override
public String toString() {
return name + " (" + tasks.size() + " tasks)";
}
}
// ToDoList class to manage categories and tasks
class ToDoList {
private List<Category> categories;
public ToDoList() {
this.categories = new ArrayList<>();
}
public void addCategory(Category category) {
categories.add(category);
}
public List<Category> getCategories() {
return categories;
}
public static void main(String[] args) {
// Create a ToDoList
ToDoList myToDoList = new ToDoList();
// Create categories
Category personal = new Category("Personal");
Category work = new Category("Work");
// Create tasks and add them to categories
Task task1 = new Task("Buy groceries");
Task task2 = new Task("Finish report");
Task task3 = new Task("Go to the gym");
personal.addTask(task1);
work.addTask(task2);
personal.addTask(task3);
// Add categories to the ToDoList
myToDoList.addCategory(personal);
myToDoList.addCategory(work);
// Display tasks and categories
for (Category category : myToDoList.getCategories()) {
System.out.println(category);
for (Task task : category.getTasks()) {
System.out.println(" - " + task);
}
}
}
}
In this Java example:
Task represents individual tasks with a title and completion status. Category represents task categories and contains a list of tasks. ToDoList manages categories and provides a way to add categories and retrieve them. In the main method, we create a ToDoList, add categories (personal and work), create tasks, and associate them with the respective categories. Finally, we display the tasks grouped by categories.
You can further extend this To-Do List Application to include features like task due dates, task priority, and persistence to store tasks in a file or database.