This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTaskManager.java
More file actions
111 lines (98 loc) · 3.25 KB
/
TaskManager.java
File metadata and controls
111 lines (98 loc) · 3.25 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.ArrayList;
import java.util.Scanner;
class Task {
private String description;
private boolean isCompleted;
public Task(String description) {
this.description = description;
this.isCompleted = false;
}
public void markAsCompleted() {
this.isCompleted = true;
}
public boolean isCompleted() {
return isCompleted;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return (isCompleted ? "[Completed] " : "[Pending] ") + description;
}
}
public class TaskManager {
private static ArrayList<Task> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int choice;
do {
System.out.println("\nTask Manager");
System.out.println("1. Add Task");
System.out.println("2. Delete Task");
System.out.println("3. Mark Task as Completed");
System.out.println("4. List All Tasks");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addTask();
break;
case 2:
deleteTask();
break;
case 3:
markTaskAsCompleted();
break;
case 4:
listAllTasks();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
private static void addTask() {
System.out.print("Enter task description: ");
String description = scanner.nextLine();
tasks.add(new Task(description));
System.out.println("Task added successfully.");
}
private static void deleteTask() {
listAllTasks();
System.out.print("Enter task number to delete: ");
int taskNumber = scanner.nextInt();
if (taskNumber > 0 && taskNumber <= tasks.size()) {
tasks.remove(taskNumber - 1);
System.out.println("Task deleted successfully.");
} else {
System.out.println("Invalid task number.");
}
}
private static void markTaskAsCompleted() {
listAllTasks();
System.out.print("Enter task number to mark as completed: ");
int taskNumber = scanner.nextInt();
if (taskNumber > 0 && taskNumber <= tasks.size()) {
tasks.get(taskNumber - 1).markAsCompleted();
System.out.println("Task marked as completed.");
} else {
System.out.println("Invalid task number.");
}
}
private static void listAllTasks() {
if (tasks.isEmpty()) {
System.out.println("No tasks available.");
} else {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
}
}