Creating a To-Do List Application in Java: A Step-by-Step Guide
In this article, we'll walk through the process of creating a simple to-do list application in Java. The application will allow users to add, view, and delete tasks, providing a straightforward example of managing data in a list structure. This project illustrates key concepts such as object-oriented programming and basic data management in Java.
Understanding the Requirements
Before diving into the code, let's outline the requirements for our to-do list application:
- A task should have a title and an optional description.
- Users should be able to add new tasks.
- Users should be able to view all tasks.
- Users should be able to delete tasks.
Designing the Solution
The application will consist of the following components:
- Task Class: This class will represent a single to-do item with properties for the task's title and description.
- TaskManager Class: This class will manage a list of tasks and provide methods to add, display, and delete tasks.
- Main Application: This part of the application will handle user input and interaction, calling the appropriate methods on
TaskManagerto perform operations.
Implementing the Solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Task class to represent a task in the to-do list
class Task {
private String title;
private String description;
public Task(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Task: " + title + (description.isEmpty() ? "" : " - " + description);
}
}
// TaskManager class to manage tasks
class TaskManager {
private List<Task> tasks;
public TaskManager() {
this.tasks = new ArrayList<>();
}
public void addTask(String title, String description) {
tasks.add(new Task(title, description));
}
public void viewTasks() {
if (tasks.isEmpty()) {
System.out.println("No tasks available.");
} else {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
public void deleteTask(int index) {
if (index < 0 || index >= tasks.size()) {
throw new IndexOutOfBoundsException("Invalid task index");
}
tasks.remove(index);
}
}
// Main application class
public class TodoListApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TaskManager taskManager = new TaskManager();
boolean running = true;
while (running) {
System.out.println("\nTo-Do List Application");
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Delete Task");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter task title: ");
String title = scanner.nextLine();
System.out.print("Enter task description (optional): ");
String description = scanner.nextLine();
taskManager.addTask(title, description);
break;
case 2:
taskManager.viewTasks();
break;
case 3:
System.out.print("Enter the task number to delete: ");
int taskNumber = scanner.nextInt();
try {
taskManager.deleteTask(taskNumber - 1);
System.out.println("Task deleted.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid task number.");
}
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid option, please try again.");
}
}
scanner.close();
}
}
Conclusion
This article provided a basic example of how to create a to-do list application in Java. We covered the requirements analysis, design, and implementation of the solution. This program introduces fundamental Java programming concepts and demonstrates the application of object-oriented principles such as encapsulation and abstraction. This type of project serves as a strong starting point for more complex applications.