forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddCommand.java
67 lines (60 loc) · 1.8 KB
/
AddCommand.java
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
package duke.command;
import java.util.Objects;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.Todo;
import duke.utils.CortanaException;
import duke.utils.Storage;
import duke.utils.TaskList;
import duke.utils.Ui;
/**
* The type Add command.
*/
public class AddCommand extends Command {
private final Task task;
/**
* Instantiates a new Add command.
*
* @param task the task to be added
*/
public AddCommand(Task task) {
assert task instanceof Deadline || task instanceof Event || task instanceof Todo;
this.task = task;
}
/**
* Add the given task to the task list.
* If the task already exists, task will not be added and exception will be thrown.
*
* @param taskList the task list to operate on
* @param ui the ui to operate on
* @param storage the storage to operate on
*/
public String execute(TaskList taskList, Ui ui, Storage storage) throws CortanaException {
if (taskList.getTaskSet().contains(task)) {
throw new CortanaException("Task already exists!");
} else {
taskList.getTaskSet().add(task);
taskList.getTaskList().add(task);
try {
storage.writeFile(taskList);
} catch (Exception e) {
throw new CortanaException(e.getMessage());
}
return ui.addedTask(taskList, task);
}
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj.getClass() == getClass()) {
AddCommand addCommand = (AddCommand) obj;
return addCommand.task.equals(this.task);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(task);
}
}