// projects.java
10 Best Java Projects
Hand-picked and ordered easiest → hardest — each with complete code and expected output. Build these to turn lessons into a portfolio.
Back to Java course
// solution.code
java
import java.io.*;
import java.util.*;
// Complete Todo List CLI Application with file persistence
public class TodoListCLI {
private static final String FILE_NAME = "todos.txt";
private static List<String> todos = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
loadTodos();
while (true) {
System.out.println("\n=== TODO LIST MANAGER ===");
System.out.println("1. Add Task\n2. View Tasks\n3. Update Task\n4. Delete Task\n5. Exit");
System.out.print("Choose option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1: addTask(); break;
case 2: viewTasks(); break;
case 3: updateTask(); break;
case 4: deleteTask(); break;
case 5: saveTodos(); System.out.println("Goodbye!"); return;
default: System.out.println("Invalid option!");
}
}
}
private static void addTask() {
System.out.print("Enter task: ");
todos.add(scanner.nextLine());
System.out.println("Task added!");
}
private static void viewTasks() {
if (todos.isEmpty()) { System.out.println("No tasks!"); return; }
for (int i = 0; i < todos.size(); i++)
System.out.println((i + 1) + ". " + todos.get(i));
}
private static void updateTask() {
viewTasks();
if (todos.isEmpty()) return;
System.out.print("Task number to update: ");
int idx = scanner.nextInt() - 1;
scanner.nextLine();
if (idx >= 0 && idx < todos.size()) {
System.out.print("New task: ");
todos.set(idx, scanner.nextLine());
System.out.println("Task updated!");
} else System.out.println("Invalid task number!");
}
private static void deleteTask() {
viewTasks();
if (todos.isEmpty()) return;
System.out.print("Task number to delete: ");
int idx = scanner.nextInt() - 1;
if (idx >= 0 && idx < todos.size()) {
todos.remove(idx);
System.out.println("Task deleted!");
} else System.out.println("Invalid task number!");
}
private static void loadTodos() {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = br.readLine()) != null) todos.add(line);
} catch (IOException e) { /* File doesn't exist yet */ }
}
private static void saveTodos() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (String todo : todos) bw.write(todo + "\n");
} catch (IOException e) { System.err.println("Error saving: " + e.getMessage()); }
}
} output
=== TODO LIST MANAGER === 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Choose option: 1 Enter task: Buy groceries Task added! === TODO LIST MANAGER === 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Choose option: 2 1. Buy groceries === TODO LIST MANAGER === 1. Add Task 2. View Tasks 3. Update Task 4. Delete Task 5. Exit Choose option: 5 Goodbye!
