Java TODO App Using Classes & Serialization
(Absolute Beginner Friendly)

🔗 View Source on GitHub 🖥 View GUI Version (Paid)

In this lesson, we will build a complete Java TODO Application using Classes, Objects, Methods, ArrayList, File Handling, and Serialization. This is ideal for beginners and forms a strong base for future GUI or Android versions.

This TODO App Can:

💡 Understanding The Concepts

1. What is a Class?

A class is a blueprint. Example: "Car" is a class. A real car like "Maruti Swift" is an object.

2. What is an Object?

Objects are created from classes. In this app, each task is an object of the Task class.

3. What is an ArrayList?

ArrayList is a flexible container that grows automatically. We use it to store multiple Task objects.

4. What is Serialization?

Java cannot store objects directly into files. Serialization converts an object to bytes. These bytes can be saved to disk and loaded later. Our file will be called tasks.dat.

5. What is Scanner?

Scanner is used to take input from the keyboard (user types into console).

6. What is try–catch?

try–catch prevents the program from crashing when something goes wrong.


1️⃣ The Task Class




package live.learnwithchampak.basicjava.todo;
import java.io.Serializable;

public class Task implements Serializable {
    private static final long serialVersionUID = 1L;

    private int id;
    private String title;
    private boolean completed;

    public Task(int id, String title) {
        this.id = id;
        this.title = title;
        this.completed = false;
    }

    public int getId() { return id; }
    public String getTitle() { return title; }
    public boolean isCompleted() { return completed; }

    public void markCompleted() {
        this.completed = true;
    }

    @Override
    public String toString() {
        return id + ". " + title + " [" + (completed ? "DONE" : "PENDING") + "]";
    }
}



2️⃣ Saving & Loading Tasks (Serialization)


 
package live.learnwithchampak.basicjava.todo;
import java.io.*;
import java.util.ArrayList;

public class TaskStorage {

    private static final String FILE_NAME = "tasks.dat";

    public static void saveTasks(ArrayList tasks) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(tasks);
        } catch (Exception e) {
            System.out.println("Error saving tasks: " + e.getMessage());
        }
    }

    public static ArrayList loadTasks() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            return (ArrayList) ois.readObject();
        } catch (Exception e) {
            System.out.println("No saved tasks found, starting fresh.");
            return new ArrayList<>();
        }
    }
}


3️⃣ TodoManager – The Core Logic


   package live.learnwithchampak.basicjava.todo;
import java.util.ArrayList;

public class TodoManager {

    private ArrayList tasks;
    private int nextId = 1;

    public TodoManager() {
        tasks = TaskStorage.loadTasks();
        if (!tasks.isEmpty()) {
            nextId = tasks.get(tasks.size() - 1).getId() + 1; // continue ID
        }
    }

    public void addTask(String title) {
        Task t = new Task(nextId++, title);
        tasks.add(t);
        TaskStorage.saveTasks(tasks);
        System.out.println("Task added!");
    }

    public void listTasks() {
        if (tasks.isEmpty()) {
            System.out.println("No tasks yet.");
            return;
        }
        for (Task t : tasks) {
            System.out.println(t);
        }
    }

    public void markCompleted(int id) {
        for (Task t : tasks) {
            if (t.getId() == id) {
                t.markCompleted();
                TaskStorage.saveTasks(tasks);
                System.out.println("Task marked as DONE!");
                return;
            }
        }
        System.out.println("Task not found.");
    }

    public void deleteTask(int id) {
        tasks.removeIf(t -> t.getId() == id);
        TaskStorage.saveTasks(tasks);
        System.out.println("Task deleted!");
    }
}


4️⃣ Main Console Menu


import java.util.Scanner;

public class TodoApp {

    public static void main(String[] args) {

        TodoManager manager = new TodoManager();
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("\n=== TODO MENU ===");
            System.out.println("1. Add Task");
            System.out.println("2. List Tasks");
            System.out.println("3. Mark Completed");
            System.out.println("4. Delete Task");
            System.out.println("5. Exit");
            System.out.print("Choose: ");

            int choice = sc.nextInt();
            sc.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Enter task title: ");
                    String title = sc.nextLine();
                    manager.addTask(title);
                    break;

                case 2:
                    manager.listTasks();
                    break;

                case 3:
                    System.out.print("Task ID to mark complete: ");
                    int id1 = sc.nextInt();
                    manager.markCompleted(id1);
                    break;

                case 4:
                    System.out.print("Task ID to delete: ");
                    int id2 = sc.nextInt();
                    manager.deleteTask(id2);
                    break;

                case 5:
                    System.out.println("Goodbye!");
                    return;

                default:
                    System.out.println("Invalid choice.");
            }
        }
    }
}

▶️ How to Run

  1. Place all 4 files in one folder
  2. Compile using:
    javac *.java
  3. Run the program:
    java TodoApp

A file named tasks.ser will store your tasks permanently using Serialization.


🚀 Upgrade Ideas

Tell me what you want next, Champak, and I'll generate the full code + explanation.