-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathCommand.java
More file actions
109 lines (99 loc) · 3.54 KB
/
Command.java
File metadata and controls
109 lines (99 loc) · 3.54 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
import duke.exception.DukeException;
import duke.exception.IrregularInputException;
import duke.exception.RepeatedCompletionException;
import duke.task.Task;
import duke.task.TaskBank;
import java.util.ArrayList;
public class Command {
public static void perform(String sentence, Action action, Ui ui, Storage storage, TaskBank tb) throws DukeException {
switch (action) {
case UNKNOWN_ACTION:
System.out.printf("☹ OOPS!!! I'm sorry, but I don't know what that means :-( %nPlease try again!%n");
break;
case CLEAR:
clear(tb, storage);
ui.showClearMessage();
storage.exportTasks(tb);
break;
case DELETE:
Task deletedTask = deleteTask(sentence, tb);
ui.showDeleteMessage(deletedTask, tb.getTaskSize());
storage.exportTasks(tb);
break;
case BYE:
bye();
ui.showByeMessage();
break;
case LIST:
ui.showAllTaskMessage();
listAllTask(tb);
break;
case CREATE_TODO:
case CREATE_DEADLINE:
case CREATE_EVENT:
Task createdTask = createTask(action, tb, sentence);
ui.showTaskAddedMessage(createdTask, tb.getTaskSize());
storage.exportTasks(tb);
break;
case MARK_AS_DONE:
Task completedTask = completeTask(tb, sentence);
ui.showCompleteMessage(completedTask);
storage.exportTasks(tb);
break;
case FIND:
TaskBank matchedTaskBank = findTask(sentence, tb);
ui.showFindMessage();
listAllTask(matchedTaskBank);
break;
default:
System.out.println("ERROR IN COMMMAND");
}
}
private static void clear(TaskBank tb, Storage storage) {
tb.clear();
}
private static Task deleteTask(String deleteStament, TaskBank tb) throws IrregularInputException {
int targetIndex = Parser.parseIndex(deleteStament);
Task deletedTask = tb.removeTask(targetIndex);
return deletedTask;
}
private static void bye() {
Duke.terminateDuke();
}
private static void listAllTask(TaskBank tb) {
tb.printList();
}
private static Task createTask(Action action, TaskBank tb, String sentence) {
Task newTask;
String description = Parser.parseDescription(sentence);
switch (action) {
case CREATE_TODO:
newTask = tb.addTodo(description);
break;
case CREATE_DEADLINE:
newTask = tb.addDeadline(description);
break;
case CREATE_EVENT:
newTask = tb.addEvent(description);
break;
default:
newTask = null;
}
return newTask;
}
private static TaskBank findTask(String input, TaskBank givenTaskBank) throws IrregularInputException {
String keywordInput = Parser.parseKeyWord(input);
ArrayList<Task> matchingTasks = TaskBank.findMatchingTask(givenTaskBank, keywordInput);
if (matchingTasks.isEmpty()) {
return null;
} else {
return new TaskBank(matchingTasks);
}
}
private static Task completeTask(TaskBank tb, String sentence) throws RepeatedCompletionException, NumberFormatException, IrregularInputException {
int targetIndex = Parser.parseIndex(sentence);
Task completedTask = tb.searchTask(targetIndex);
completedTask.markAsDone();
return completedTask;
}
}