-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
101 lines (85 loc) · 3.03 KB
/
Main.java
File metadata and controls
101 lines (85 loc) · 3.03 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
import java.util.*;
public class Main {
private final Scanner scanner = new Scanner(System.in);
private final Map<String, Runnable> menuActions = new HashMap<>();
private final List<Task> taskList = new ArrayList<>();
private final TaskRepository repository = new TaskRepository("tasks.txt");
public static void main(String[] args) {
new Main().run();
}
private void run() {
taskList.addAll(repository.load());
initMenu();
while (true) {
System.out.print("\nChoice > ");
String choice = scanner.nextLine();
System.out.println();
if (menuActions.containsKey(choice)) {
menuActions.get(choice).run();
repository.save(taskList);
} else {
System.out.println("Invalid option.");
}
}
}
private void handleAddTask() {
System.out.print("Enter task title: ");
String title = scanner.nextLine();
taskList.add(new BasicTask(title));
System.out.println("Task added!");
}
private void handleViewTasks() {
if (taskList.isEmpty()) {
System.out.println("No tasks found.");
return;
}
for (int i = 0; i < taskList.size(); i++) {
System.out.println((i + 1) + ". " + taskList.get(i).getDetails());
}
}
private void handleCompleteTask() {
handleViewTasks();
if (taskList.isEmpty()) {
return;
}
System.out.print("Enter task number to complete: ");
try {
int index = Integer.parseInt(scanner.nextLine()) - 1;
taskList.get(index).complete();
System.out.println("Task marked as done!");
} catch (NumberFormatException | IndexOutOfBoundsException e) {
System.out.println("Invalid input.");
}
}
private void handleDeleteTask() {
handleViewTasks();
if (taskList.isEmpty()) {
return;
}
System.out.print("Enter task number to delete: ");
try {
int index = Integer.parseInt(scanner.nextLine()) - 1;
taskList.remove(index);
System.out.println("Task deleted.");
} catch (NumberFormatException | IndexOutOfBoundsException e) {
System.out.println("Invalid input.");
}
}
private void handleExit() {
System.out.println("Goodbye!");
System.exit(0);
}
private void menuItem(String key, String label, Runnable action) {
System.out.println(key + ". " + label);
menuActions.put(key, action);
}
private void initMenu() {
System.out.println("\n--- TASK MANAGER ---\n");
menuItem("1", "[+] Add Task", this::handleAddTask);
menuItem("2", "[-] View Tasks", this::handleViewTasks);
menuItem("3", "[v] Complete", this::handleCompleteTask);
menuItem("4", "[x] Delete", this::handleDeleteTask);
menuItem("5", "[^] Exit", this::handleExit);
System.out.println();
}
}