|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +class Task { |
| 5 | + private String description; |
| 6 | + |
| 7 | + public Task(String description) { |
| 8 | + this.description = description; |
| 9 | + } |
| 10 | + |
| 11 | + public String getDescription() { |
| 12 | + return description; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class TodoList { |
| 17 | + private ArrayList<Task> tasks = new ArrayList<>(); |
| 18 | + |
| 19 | + public void addTask(String description) { |
| 20 | + Task task = new Task(description); |
| 21 | + tasks.add(task); |
| 22 | + System.out.println("Task added successfully!"); |
| 23 | + } |
| 24 | + |
| 25 | + public void listTasks() { |
| 26 | + if (tasks.isEmpty()) { |
| 27 | + System.out.println("No tasks found."); |
| 28 | + } else { |
| 29 | + System.out.println("Task List:"); |
| 30 | + for (int i = 0; i < tasks.size(); i++) { |
| 31 | + System.out.println((i + 1) + ". " + tasks.get(i).getDescription()); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public void removeTask(int index) { |
| 37 | + if (index >= 0 && index < tasks.size()) { |
| 38 | + tasks.remove(index); |
| 39 | + System.out.println("Task removed successfully!"); |
| 40 | + } else { |
| 41 | + System.out.println("Invalid task index."); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +public class TodoApp { |
| 47 | + public static void main(String[] args) { |
| 48 | + Scanner scanner = new Scanner(System.in); |
| 49 | + TodoList todoList = new TodoList(); |
| 50 | + |
| 51 | + while (true) { |
| 52 | + System.out.println("\nTo-Do List Application"); |
| 53 | + System.out.println("1. Add Task"); |
| 54 | + System.out.println("2. List Tasks"); |
| 55 | + System.out.println("3. Remove Task"); |
| 56 | + System.out.println("4. Exit"); |
| 57 | + System.out.print("Enter your choice: "); |
| 58 | + |
| 59 | + int choice = scanner.nextInt(); |
| 60 | + scanner.nextLine(); // Consume the newline character |
| 61 | + |
| 62 | + switch (choice) { |
| 63 | + case 1: |
| 64 | + System.out.print("Enter task description: "); |
| 65 | + String description = scanner.nextLine(); |
| 66 | + todoList.addTask(description); |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + todoList.listTasks(); |
| 70 | + break; |
| 71 | + case 3: |
| 72 | + System.out.print("Enter the task index to remove: "); |
| 73 | + int index = scanner.nextInt(); |
| 74 | + todoList.removeTask(index - 1); |
| 75 | + break; |
| 76 | + case 4: |
| 77 | + System.out.println("Exiting the To-Do List Application. Goodbye!"); |
| 78 | + scanner.close(); |
| 79 | + System.exit(0); |
| 80 | + default: |
| 81 | + System.out.println("Invalid choice. Please try again."); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments