forked from CS2113-AY1819S1-F10-3/main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteCommand.java
46 lines (34 loc) · 1.54 KB
/
DeleteCommand.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
package seedu.addressbook.commands;
import seedu.addressbook.common.Messages;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;
/**
* Deletes a person identified using it's last displayed index from the address book.
*/
public class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";
public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Deletes the person identified by the index number used in the last person listing.\n\t"
+ "Parameters: INDEX\n\t"
+ "Example: " + COMMAND_WORD + " 1";
public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
private String nameToSearch = "";
public DeleteCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}
public DeleteCommand(String name) {
this.nameToSearch = name;
}
@Override
public CommandResult execute() {
try {
final ReadOnlyPerson target = (nameToSearch.isEmpty()) ? getTargetPerson() : getTargetPerson(nameToSearch);
addressBook.removePerson(target);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, target));
} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
} catch (PersonNotFoundException pnfe) {
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK);
}
}
}