|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class TodoList { |
| 6 | + private List<String> tasks; |
| 7 | + |
| 8 | + public TodoList() { |
| 9 | + tasks = new ArrayList<>(); |
| 10 | + } |
| 11 | + |
| 12 | + public void addTask(String task) { |
| 13 | + tasks.add(task); |
| 14 | + } |
| 15 | + |
| 16 | + public void removeTask(int index) { |
| 17 | + if (index >= 0 && index < tasks.size()) { |
| 18 | + tasks.remove(index); |
| 19 | + } else { |
| 20 | + System.out.println("Invalid index"); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public void listTasks() { |
| 25 | + System.out.println("Task List:"); |
| 26 | + for (int i = 0; i < tasks.size(); i++) { |
| 27 | + System.out.println(i + 1 + ". " + tasks.get(i)); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + TodoList todoList = new TodoList(); |
| 33 | + Scanner scanner = new Scanner(System.in); |
| 34 | + |
| 35 | + while (true) { |
| 36 | + System.out.println("\nChoose an action:"); |
| 37 | + System.out.println("1. Add task"); |
| 38 | + System.out.println("2. Remove task"); |
| 39 | + System.out.println("3. List tasks"); |
| 40 | + System.out.println("4. Exit"); |
| 41 | + |
| 42 | + int choice = scanner.nextInt(); |
| 43 | + scanner.nextLine(); // Consume the newline character |
| 44 | + |
| 45 | + switch (choice) { |
| 46 | + case 1: |
| 47 | + System.out.print("Enter the task: "); |
| 48 | + String task = scanner.nextLine(); |
| 49 | + todoList.addTask(task); |
| 50 | + break; |
| 51 | + case 2: |
| 52 | + System.out.print("Enter the index of the task to remove: "); |
| 53 | + int index = scanner.nextInt(); |
| 54 | + scanner.nextLine(); // Consume the newline character |
| 55 | + todoList.removeTask(index - 1); |
| 56 | + break; |
| 57 | + case 3: |
| 58 | + todoList.listTasks(); |
| 59 | + break; |
| 60 | + case 4: |
| 61 | + System.out.println("Exiting..."); |
| 62 | + System.exit(0); |
| 63 | + break; |
| 64 | + default: |
| 65 | + System.out.println("Invalid option"); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments