Step 1: Define the Task Class

Our journey begins with the creation of the Task class, the cornerstone of our application. This class represents the tasks our users will manage:

public class Task {
    private String description;
    private boolean isCompleted;

    public Task(String description) {
        this.description = description;
        this.isCompleted = false;
    }

    public String getDescription() {
        return description;
    }

    public boolean isCompleted() {
        return isCompleted;
    }

    public void completeTask() {
        isCompleted = true;
    }

    @Override
    public String toString() {
        return (isCompleted ? "[X] " : "[ ] ") + description;
    }
}

Step 2: Task Logic

With our Task class defined, we introduce the TaskManager class to encapsulate our application’s logic, including adding, viewing, completing, and deleting tasks:

import java.util.ArrayList;
import java.util.Scanner;

public class TaskManager {
    private ArrayList<Task> tasks = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        boolean isRunning = true;

        while (isRunning) {
            System.out.println("Task Manager - Choose an action: (1) Add Task (2) View Tasks (3) Complete Task (4) Exit");
            String action = scanner.nextLine();

            switch (action) {
                case "1":
                    addTask();
                    break;
                case "2":
                    viewTasks();
                    break;
                case "3":
                    completeTask();
                    break;
                case "4":
                    isRunning = false;
                    break;
                default:
                    System.out.println("Invalid option. Please choose a valid action.");
            }
        }
    }

    private void addTask() {
        System.out.println("Enter task description:");
        String description = scanner.nextLine();
        tasks.add(new Task(description));
        System.out.println("Task added.");
    }

    private 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));
            }
        }
    }

    private void completeTask() {
        viewTasks();
        System.out.println("Enter the number of the task to complete:");
        int taskNumber = Integer.parseInt(scanner.nextLine());
        if (taskNumber >= 1 && taskNumber <= tasks.size()) {
            tasks.get(taskNumber - 1).completeTask();
            System.out.println("Task completed.");
        } else {
            System.out.println("Invalid task number.");
        }
    }
}

Step 3: Running the Task Manager

To breathe life into our Task Manager, we craft a Main class to serve as the entry point for our application:

public class Main {
    public static void main(String[] args) {
        TaskManager taskManager = new TaskManager();
        taskManager.start();
    }
}

How to Run

  1. Compile the Java Files: Use the javac command in your terminal within the directory containing your .java files.

    javac Task.java TaskManager.java Main.java
  2. Run the Application:

    java Main

Resources for the Java Adventurer