forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoApp.java
85 lines (74 loc) · 2.57 KB
/
TodoApp.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
import java.util.ArrayList;
import java.util.Scanner;
class Task {
private String description;
public Task(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
class TodoList {
private ArrayList<Task> tasks = new ArrayList<>();
public void addTask(String description) {
Task task = new Task(description);
tasks.add(task);
System.out.println("Task added successfully!");
}
public void listTasks() {
if (tasks.isEmpty()) {
System.out.println("No tasks found.");
} else {
System.out.println("Task List:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i).getDescription());
}
}
}
public void removeTask(int index) {
if (index >= 0 && index < tasks.size()) {
tasks.remove(index);
System.out.println("Task removed successfully!");
} else {
System.out.println("Invalid task index.");
}
}
}
public class TodoApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TodoList todoList = new TodoList();
while (true) {
System.out.println("\nTo-Do List Application");
System.out.println("1. Add Task");
System.out.println("2. List Tasks");
System.out.println("3. Remove Task");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String description = scanner.nextLine();
todoList.addTask(description);
break;
case 2:
todoList.listTasks();
break;
case 3:
System.out.print("Enter the task index to remove: ");
int index = scanner.nextInt();
todoList.removeTask(index - 1);
break;
case 4:
System.out.println("Exiting the To-Do List Application. Goodbye!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}