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