forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from Pluiexo/branch-SelectCommand
Branch select command
- Loading branch information
Showing
16 changed files
with
528 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/staffconnect/logic/commands/SelectCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package staffconnect.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import staffconnect.commons.core.index.Index; | ||
import staffconnect.commons.util.ToStringBuilder; | ||
import staffconnect.logic.Messages; | ||
import staffconnect.logic.commands.exceptions.CommandException; | ||
import staffconnect.model.Model; | ||
import staffconnect.model.person.Person; | ||
|
||
/** | ||
* Selects a person identified using it's displayed index from the staff 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 for display.\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) throws CommandException { | ||
requireNonNull(model); | ||
List<Person> lastShownList = model.getSortedFilteredPersonList(); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToSelect = lastShownList.get(targetIndex.getZeroBased()); | ||
|
||
return new CommandResult(String.format(MESSAGE_SELECT_PERSON_SUCCESS, Messages.format(personToSelect)), | ||
personToSelect, targetIndex.getZeroBased()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SelectCommand)) { | ||
return false; | ||
} | ||
|
||
SelectCommand selectCommand = (SelectCommand) other; | ||
return targetIndex.equals(selectCommand.targetIndex); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIndex", targetIndex) | ||
.toString(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/staffconnect/logic/parser/SelectCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package staffconnect.logic.parser; | ||
|
||
import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import staffconnect.commons.core.index.Index; | ||
import staffconnect.logic.commands.SelectCommand; | ||
import staffconnect.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SelectCommand object | ||
*/ | ||
public class SelectCommandParser implements Parser<SelectCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SelectCommand | ||
* and returns a SelectCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SelectCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new SelectCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.