Java Mini Project - To-Do List Application
Objective: Develop a simple to-do list application using Java with an emphasis on functions and data structures.
Key Features:
- Method to add a task
- Method to delete a task
- Method to display the list of tasks
- Method to mark a task as complete
Data Structure Used: ArrayList
Code:
         Import java.util.ArrayList;
         Import java.util.Scanner;
         Public class ToDoList {
           Static class Task {
             String description;
             Boolean isCompleted;
               Task(String description) {
                 This.description = description;
                 This.isCompleted = false;
               }
               Public String toString() {
                   Return (isCompleted ? “[✓] “ : “[ ] “) + description;
               }
           }
           Static ArrayList<Task> tasks = new ArrayList<>();
           Public static void addTask(String description) {
             Tasks.add(new Task(description));
             System.out.println(“Task added successfully!”);
           }
Public static void deleteTask(int index) {
  If (index >= 0 && index < tasks.size()) {
     Tasks.remove(index);
     System.out.println(“Task deleted successfully!”);
  } else {
     System.out.println(“Invalid task number.”);
  }
}
Public static void markAsComplete(int index) {
  If (index >= 0 && index < tasks.size()) {
     Tasks.get(index).isCompleted = true;
      System.out.println(“Task marked as complete!”);
    } else {
      System.out.println(“Invalid task number.”);
    }
}
Public static void displayTasks() {
  If (tasks.isEmpty()) {
     System.out.println(“Your to-do list is empty!”);
  } else {
     System.out.println(“To-Do List:”);
     For (int I = 0; I < tasks.size(); i++) {
        System.out.println((I + 1) + “. “ + tasks.get(i));
     }
  }
}
Public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  Int choice;
    Do {
     System.out.println(“\nTo-Do List Application:”);
     System.out.println(“1. Add Task”);
     System.out.println(“2. Delete Task”);
     System.out.println(“3. Display Tasks”);
     System.out.println(“4. Mark Task as Complete”);
          System.out.println(“5. Exit”);
          System.out.print(“Enter your choice: “);
          Choice = sc.nextInt();
          Sc.nextLine(); // consume newline
          Switch (choice) {
           Case 1:
              System.out.print(“Enter task description: “);
              String desc = sc.nextLine();
              addTask(desc);
              break;
           case 2:
              displayTasks();
              System.out.print(“Enter task number to delete: “);
              Int delIndex = sc.nextInt() – 1;
              deleteTask(delIndex);
              break;
            case 3:
              displayTasks();
              break;
            case 4:
              displayTasks();
              System.out.print(“Enter task number to mark as complete: “);
              Int compIndex = sc.nextInt() – 1;
              markAsComplete(compIndex);
              break;
            case 5:
              System.out.println(“Exiting the application…”);
              Break;
            Default:
              System.out.println(“Invalid choice. Try again.”);
          }
        } while (choice != 5);
        Sc.close();
    }
}
Output Screenshot :