-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManagerApp.java
More file actions
238 lines (202 loc) · 6.56 KB
/
TaskManagerApp.java
File metadata and controls
238 lines (202 loc) · 6.56 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class Task {
private String title;
private Priority priority;
private boolean isComplete;
public Task(String title, Priority priority) {
this.title = title;
this.priority = priority;
this.isComplete = false;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Priority getPriority() {
return priority;
}
public void setPriority(Priority priority) {
this.priority = priority;
}
public boolean isComplete() {
return isComplete;
}
public void setComplete(boolean complete) {
isComplete = complete;
}
public void markComplete() {
this.isComplete = true;
}
@Override
public String toString() {
String status = isComplete ? "[X]" : "[ ]";
return status + " " + priority + " - " + title;
}
}
enum Priority {
HIGH,
MEDIUM,
LOW
}
class TaskView {
public void displayTaskList(List<Task> tasks) {
System.out.println("Task List:");
if (tasks.isEmpty()) {
System.out.println("No tasks found.");
} else {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
}
class TaskController {
private List<Task> tasks;
private TaskView taskView;
public TaskController() {
this.tasks = new ArrayList<>();
this.taskView = new TaskView();
}
public void createTask(String title, Priority priority) {
Task task = new Task(title, priority);
tasks.add(task);
}
public void markTaskComplete(int taskIndex) {
if (isValidTaskIndex(taskIndex)) {
Task task = tasks.get(taskIndex - 1);
task.markComplete();
} else {
System.out.println("Invalid task index.");
}
}
public List<Task> getTasks() {
return tasks;
}
private boolean isValidTaskIndex(int taskIndex) {
return taskIndex >= 1 && taskIndex <= tasks.size();
}
// Additional method to remove a task by name
public void removeTaskByName(String taskName) {
Task taskToRemove = null;
for (Task task : tasks) {
if (task.getTitle().equals(taskName)) {
taskToRemove = task;
break;
}
}
if (taskToRemove != null) {
tasks.remove(taskToRemove);
}
}
}
public class TaskManagerApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TaskController taskController = new TaskController();
TaskManagerGUI gui = new TaskManagerGUI(taskController);
}
});
}
}
class TaskManagerGUI {
private JFrame frame;
private TaskController taskController;
private DefaultListModel<String> taskListModel;
private JList<String> taskList;
private JTextField titleField;
private JTextField killTaskField;
private JComboBox<String> priorityComboBox;
public TaskManagerGUI(TaskController taskController) {
this.taskController = taskController;
initialize();
}
private void initialize() {
frame = new JFrame("Task Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel titleLabel = new JLabel("Task Title:");
titleField = new JTextField(20);
JLabel priorityLabel = new JLabel("Priority:");
String[] priorities = {"HIGH", "MEDIUM", "LOW"};
priorityComboBox = new JComboBox<>(priorities);
JButton createButton = new JButton("Create Task");
JButton killButton = new JButton("Kill Task");
JLabel killTaskLabel = new JLabel("Task Name to Kill:");
killTaskField = new JTextField(20);
inputPanel.add(titleLabel);
inputPanel.add(titleField);
inputPanel.add(priorityLabel);
inputPanel.add(priorityComboBox);
inputPanel.add(createButton);
inputPanel.add(killTaskLabel);
inputPanel.add(killTaskField);
inputPanel.add(killButton);
taskListModel = new DefaultListModel<>();
taskList = new JList<>(taskListModel);
JScrollPane taskScrollPane = new JScrollPane(taskList);
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(taskScrollPane, BorderLayout.CENTER);
createButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createTask();
}
});
killButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
killTask();
}
});
frame.setVisible(true);
}
private void createTask() {
String title = titleField.getText();
String priority = (String) priorityComboBox.getSelectedItem();
if (!title.isEmpty()) {
Priority taskPriority = Priority.valueOf(priority.toUpperCase());
taskController.createTask(title, taskPriority);
updateTaskList();
titleField.setText("");
} else {
JOptionPane.showMessageDialog(frame, "Task title cannot be empty.", "Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
private void killTask() {
String taskName = killTaskField.getText();
if (!taskName.isEmpty()) {
taskController.removeTaskByName(taskName);
updateTaskList();
killTaskField.setText("");
} else {
JOptionPane.showMessageDialog(frame, "Task name cannot be empty.", "Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
private void updateTaskList() {
taskListModel.clear();
for (Task task : taskController.getTasks()) {
taskListModel.addElement(task.toString());
}
}
}