-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtodolist.java
More file actions
76 lines (65 loc) · 2.3 KB
/
todolist.java
File metadata and controls
76 lines (65 loc) · 2.3 KB
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
import java.util.ArrayList;
import java.util.Scanner;
public class ToDoList {
private static ArrayList<String> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean running = true;
while (running) {
System.out.println("To-Do List Application");
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Remove Task");
System.out.println("4. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
addTask();
break;
case 2:
viewTasks();
break;
case 3:
removeTask();
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
System.out.println("Goodbye!");
scanner.close();
}
private static void addTask() {
System.out.print("Enter the task to add: ");
String task = scanner.nextLine();
tasks.add(task);
System.out.println("Task added: " + task);
}
private static void viewTasks() {
System.out.println("Tasks in the to-do list:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
private static void removeTask() {
viewTasks();
if (tasks.isEmpty()) {
System.out.println("The to-do list is empty.");
return;
}
System.out.print("Enter the number of the task to remove: ");
int index = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
if (index >= 1 && index <= tasks.size()) {
String removedTask = tasks.remove(index - 1);
System.out.println("Task removed: " + removedTask);
} else {
System.out.println("Invalid task number. Please try again.");
}
}
}