-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTracker.java
More file actions
80 lines (71 loc) · 2.29 KB
/
TaskTracker.java
File metadata and controls
80 lines (71 loc) · 2.29 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
77
78
79
80
import java.util.ArrayList;
import java.util.Scanner;
public class TaskTracker {
static ArrayList<String> tasks = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
showMenu();
int choice = getChoice();
handleChoice(choice);
}
}
static void showMenu() {
System.out.println("\n===== Task Tracker =====");
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Delete Task");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
}
static int getChoice() {
try {
return Integer.parseInt(scanner.nextLine());
} catch (Exception e) {
return -1;
}
}
static void handleChoice(int choice) {
switch (choice) {
case 1 -> addTask();
case 2 -> viewTasks();
case 3 -> deleteTask();
case 4 -> {
System.out.println(" Exiting...");
System.exit(0);
}
default -> System.out.println(" Invalid choice. Try again.");
}
}
static void addTask() {
System.out.print("Enter task description: ");
String task = scanner.nextLine();
tasks.add(task);
System.out.println(" Task added!");
}
static void viewTasks() {
if (tasks.isEmpty()) {
System.out.println(" No tasks available.");
} else {
System.out.println("\n Your Tasks:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
static void deleteTask() {
viewTasks();
if (tasks.isEmpty()) return;
System.out.print("Enter task number to delete: ");
try {
int index = Integer.parseInt(scanner.nextLine()) - 1;
if (index >= 0 && index < tasks.size()) {
System.out.println(" Deleted: " + tasks.remove(index));
} else {
System.out.println(" Invalid task number.");
}
} catch (Exception e) {
System.out.println(" Please enter a valid number.");
}
}
}