-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_do.java
88 lines (73 loc) · 2.36 KB
/
to_do.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.Scanner;
import java.util.ArrayList;
public class to_do {
private ArrayList<String> tasks;
public to_do()
{
tasks = new ArrayList<>();
}
public void addTask(String task)
{
tasks.add(task);
System.out.println("Task added: " + task);
}
public void removeTask(int index)
{
if (index >= 0 && index < tasks.size()) {
String removedTask = tasks.remove(index);
System.out.println("Task removed: " + removedTask);
}
else {
System.out.println("Invalid index");
}
}
public void displayTasks()
{
if (tasks.isEmpty()) {
System.out.println("No tasks in list");
}
else {
System.out.println("Tasks:");
for(int i = 0; i < tasks.size(); i++)
{
System.out.println((i+1)+". "+ tasks.get(i));
}
}
}
public static void main(String[] args) {
to_do todoList = new to_do();
Scanner scanner= new Scanner(System.in);
int choice;
do{
System.out.println("\nTodo List Menu : ");
System.out.println("1. Add Task");
System.out.println("2. Remove Task");
System.out.println("3. Display Tasks");
System.out.println("4. Exit");
System.out.println("Enter your choice : ");
choice = scanner.nextInt();
switch (choice) {
case 1:
scanner.nextLine();
System.out.println("Enter task to add : ");
String taskToAdd = scanner.nextLine();
todoList.addTask(taskToAdd);
break;
case 2:
System.out.println("Enter index of task to remove : ");
int indexToRemove = scanner.nextInt();
todoList.removeTask(indexToRemove);
break;
case 3:
todoList.displayTasks();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice, please enter a number : ");
}
} while (choice != 4);
scanner.close();
}
}