forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectCommand.java
63 lines (46 loc) · 2 KB
/
SelectCommand.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
package seedu.address.logic.commands;
import static java.util.Objects.requireNonNull;
import java.util.List;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
/**
* Selects a person identified using it's displayed index from the address book.
*/
public class SelectCommand extends Command {
public static final String COMMAND_WORD = "select";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Selects 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 + " 1";
public static final String MESSAGE_SELECT_PERSON_SUCCESS = "Selected Person: %1$s";
private final Index targetIndex;
public SelectCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}
@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Person> filteredPersonList = model.getFilteredPersonList();
if (targetIndex.getZeroBased() >= filteredPersonList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
model.setSelectedPerson(filteredPersonList.get(targetIndex.getZeroBased()));
return new CommandResult(String.format(MESSAGE_SELECT_PERSON_SUCCESS, targetIndex.getOneBased()));
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof SelectCommand)) {
return false;
}
SelectCommand otherSelectCommand = (SelectCommand) other;
return targetIndex.equals(otherSelectCommand.targetIndex);
}
}