forked from nusCS2113-AY1819S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Command.java
115 lines (95 loc) · 3.94 KB
/
Command.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
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
110
111
112
113
114
115
package seedu.addressbook.commands;
import seedu.addressbook.Main;
import seedu.addressbook.common.Messages;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import seedu.addressbook.ui.Formatter;
import seedu.addressbook.ui.MainWindow;
import static seedu.addressbook.ui.Gui.DISPLAYED_INDEX_OFFSET;
/**
* Represents an executable command.
*/
public abstract class Command {
protected AddressBook addressBook;
protected List<? extends ReadOnlyPerson> relevantPersons;
private int targetIndex = -1;
/**
* @param targetIndex last visible listing index of the target person
*/
public Command(int targetIndex) {
this.setTargetIndex(targetIndex);
}
protected Command() {
}
/**
* Constructs a feedback message to summarise an operation that displayed a listing of persons.
*
* @param personsDisplayed used to generate summary
* @return summary message for persons displayed
*/
public static String getMessageForPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}
/**
* Constructs a feedback message to summarise an operation that displayed a listing of persons.
*
* @param timestampsDisplayed used to generate summary
* @return summary message for timestamps displayed
*/
public static String getMessageForScreeningHistoryShownSummary(List<String> timestampsDisplayed) {
//TODO: Try format or mainwindow methods
Formatter formatter = new Formatter();
String result = formatter.formatForTstamps(timestampsDisplayed);
String finalResult = result + String.format(Messages.MESSAGE_TIMESTAMPS_LISTED_OVERVIEW, timestampsDisplayed.size());
return finalResult;
}
/**
* Executes the command and returns the result.
*/
public CommandResult execute(){
throw new UnsupportedOperationException("This method should be implement in child classes");
}
//Note: it is better to make the execute() method abstract, by replacing the above method with the line below:
//public abstract CommandResult execute();
/**
* Supplies the data the command will operate on.
*/
public void setData(AddressBook addressBook, List<? extends ReadOnlyPerson> relevantPersons) {
this.addressBook = addressBook;
this.relevantPersons = (relevantPersons.isEmpty()) ? addressBook.getAllPersons().immutableListView() : relevantPersons;
}
/**
* Extracts the the target person in the last shown list from the given arguments.
*
* @throws IndexOutOfBoundsException if the target index is out of bounds of the last viewed listing
*/
protected ReadOnlyPerson getTargetPerson() throws IndexOutOfBoundsException {
return relevantPersons.get(getTargetIndex() - DISPLAYED_INDEX_OFFSET);
}
protected ReadOnlyPerson getTargetPerson(Name name) throws UniquePersonList.PersonNotFoundException {
for (ReadOnlyPerson person: relevantPersons) {
if (person.getName().toString().equalsIgnoreCase(name.toString())) {
return person;
}
}
throw new UniquePersonList.PersonNotFoundException();
}
protected ReadOnlyPerson getTargetPersonWithNric(NRIC nric) throws UniquePersonList.PersonNotFoundException {
for (ReadOnlyPerson person: relevantPersons) {
if (person.getNRIC().getIdentificationNumber().equals(nric.getIdentificationNumber())) {
return person;
}
}
throw new UniquePersonList.PersonNotFoundException();
}
public int getTargetIndex() {
return targetIndex;
}
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}
}