-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskService.java
More file actions
90 lines (77 loc) · 2.98 KB
/
TaskService.java
File metadata and controls
90 lines (77 loc) · 2.98 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
import java.util.*;
public class TaskService {
static Scanner sc = new Scanner(System.in);
private List<Task> tasks;
private FileHandler fileHandler;
public TaskService() {
fileHandler = new FileHandler();
tasks = fileHandler.readTasks();
}
public void addTask(Task task) {
System.out.println("************** ADD TASK OPERATION **************");
tasks.add(task);
fileHandler.writeTasks(tasks);
System.out.println("✅ Task added successfully!");
}
public void viewTasks() {
System.out.println("************** VIEW TASK OPERATION **************");
if (tasks.isEmpty()) {
System.out.println("⚠️ No tasks found.");
return;
}
for (Task task : tasks) {
task.printTask();
}
}
public void updateTask() {
System.out.println("************** UPDATE TASK OPERATION **************");
System.out.print("Enter the ID of the task to update: ");
int id = sc.nextInt();
sc.nextLine(); // Consume newline
boolean found = false;
for (Task task : tasks) {
if (task.getId() == id) {
System.out.print("Enter new title: ");
String newTitle = sc.nextLine();
System.out.print("Enter new description: ");
String newDescription = sc.nextLine();
System.out.print("Enter new due date (DD-MM-YYYY): ");
String newDueDate = sc.nextLine();
// Set updated values using getters/setters
// Entity names remain unchanged per your request
task.setCompleted(false); // Assume reset to not completed
task = new Task(task.getId(), newTitle, newDescription, newDueDate, false);
tasks.set(tasks.indexOf(task), task);
fileHandler.writeTasks(tasks);
System.out.println("✅ Task updated successfully.");
found = true;
break;
}
}
if (!found) {
System.out.println("⚠️ Task not found with ID: " + id);
}
}
public void deleteTask() {
System.out.println("************** DELETE TASK OPERATION **************");
System.out.print("Enter the ID of the task to delete: ");
int id = sc.nextInt();
boolean removed = false;
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i).getId() == id) {
tasks.remove(i);
removed = true;
break;
}
}
if (removed) {
fileHandler.writeTasks(tasks);
System.out.println("🗑️ Task deleted successfully.");
} else {
System.out.println("⚠️ Task not found with ID: " + id);
}
}
public int generateTaskId() {
return tasks.size() + 1;
}
}