How to Code a Java Todo List Manager Fast

Written by

in

How to Code a Java Todo List Manager Fast Building a command-line Todo List Manager is the fastest way to master the core fundamentals of Java. This project grounds your understanding of Object-Oriented Programming (OOP), data structures, and user input handling.

By following this guide, you will have a fully functional task management application running in under ten minutes. Step 1: Set Up the Task Model

Every todo list needs a blueprint for an individual item. Create a file named Task.java to represent your data model. This class stores the description of the task and tracks whether it is completed.

public class Task { private String description; private boolean isDone; public Task(String description) { this.description = description; this.isDone = false; } public String getDescription() { return description; } public boolean isDone() { return isDone; } public void markAsDone() { this.isDone = true; } @Override public String toString() { return (isDone ? “[X] ” : “[ ] “) + description; } } Use code with caution. Step 2: Build the Core Application Logic

Next, create a file named TodoListManager.java. This class manages the tasks using an ArrayList and handles the interactive command-line loop. We use the Scanner class to capture keyboard input dynamically.

import java.util.ArrayList; import java.util.Scanner; public class TodoListManager { public static void main(String[] args) { ArrayList todoList = new ArrayList<>(); Scanner scanner = new Scanner(System.in); boolean running = true; System.out.println(”=== Quick Java Todo List ===“); while (running) { System.out.println(” Options: 1. Add | 2. View | 3. Complete | 4. Exit”); System.out.print(“Choose an option: “); if (!scanner.hasNextInt()) { System.out.println(“Please enter a valid number.”); scanner.next(); continue; } int choice = scanner.nextInt(); scanner.nextLine(); // Clear the buffer switch (choice) { case 1: System.out.print(“Enter task description: “); String text = scanner.nextLine(); todoList.add(new Task(text)); System.out.println(“Task added successfully.”); break; case 2: if (todoList.isEmpty()) { System.out.println(“Your todo list is empty.”); } else { System.out.println(” — Your Tasks —“); for (int i = 0; i < todoList.size(); i++) { System.out.println((i + 1) + “. ” + todoList.get(i)); } } break; case 3: if (todoList.isEmpty()) { System.out.println(“No tasks to complete.”); break; } System.out.print(“Enter task number to complete: “); int taskNum = scanner.nextInt(); if (taskNum > 0 && taskNum <= todoList.size()) { todoList.get(taskNum - 1).markAsDone(); System.out.println(“Task marked as done!”); } else { System.out.println(“Invalid task number.”); } break; case 4: running = false; System.out.println(“Goodbye!”); break; default: System.out.println(“Invalid choice. Try again.”); } } scanner.close(); } } Use code with caution. Step 3: Run and Test

To test your application, open your terminal or command prompt in the folder containing your two files and execute the following commands:

# Compile both Java classes javac Task.java TodoListManager.java # Run the application java TodoListManager Use code with caution. Key Takeaways

ArrayList provides a resizable array, which is perfect for dynamic collections like a shopping or task list.

Scanner.nextLine() cleaning prevents input bugs by consuming the leftover newline character after reading integers.

Encapsulation keeps the data safe by making the properties private and modifying them only through public methods like markAsDone().

Now that you have the foundation, you can easily expand this application by adding file storage (to save tasks when you close the app) or building a Graphical User Interface (GUI) using JavaFX. If you want to take this project further, Add a feature to delete tasks entirely from the list. Implement a due date feature for each task.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *