forked from nus-cs2103-AY1920S1/addressbook-level3
/
DeletePersonCommand.java
56 lines (46 loc) Β· 2.11 KB
/
DeletePersonCommand.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
package seedu.address.logic.commands;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.FLAG_PERSON;
import java.util.List;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
/**
* Deletes a person identified using it's displayed index from the address book.
*/
public class DeletePersonCommand extends DeleteCommand {
public static final String MESSAGE_USAGE = COMMAND_WORD + " " + FLAG_PERSON
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " " + FLAG_PERSON + " 1";
public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
private final Index targetIndex;
private Person personToDelete;
public DeletePersonCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
this.personToDelete = personToDelete;
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, personToDelete));
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeletePersonCommand // instanceof handles nulls
&& targetIndex.equals(((DeletePersonCommand) other).targetIndex)); // state check
}
@Override
public String toString() {
return "Delete '" + personToDelete + "' Command";
}
}