forked from nus-cs2103-AY2425S1/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 #114 from iamdiluxedbutcooler/add-view-and-close-c…
…ommand Add view command
- Loading branch information
Showing
24 changed files
with
712 additions
and
92 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
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
76 changes: 76 additions & 0 deletions
76
src/main/java/seedu/address/logic/commands/ViewCommand.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,76 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; | ||
|
||
import java.util.List; | ||
|
||
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; | ||
|
||
/** | ||
* Represents a command to view the details of a person identified by their index in the displayed list. | ||
* This command allows users to see detailed information about a specific person in the address book. | ||
*/ | ||
public class ViewCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "view"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Views 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_VIEW_PERSON_SUCCESS = "Viewed Person: %1$s"; | ||
|
||
private final Index targetIndex; | ||
|
||
/** | ||
* Creates a new ViewCommand to view the person at the specified {@code targetIndex}. | ||
* | ||
* @param targetIndex The index of the person to view in the filtered person list | ||
* @throws NullPointerException if {@code targetIndex} is null | ||
*/ | ||
public ViewCommand(Index targetIndex) { | ||
requireNonNull(targetIndex); | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
/** | ||
* Executes the view command to show the person at the specified index. | ||
* | ||
* @param model The model containing the person data | ||
* @return A CommandResult containing the viewed person's information | ||
* @throws CommandException if the index is invalid or out of bounds | ||
* @throws NullPointerException if {@code model} is null | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToView = lastShownList.get(targetIndex.getZeroBased()); | ||
return new CommandResult(String.format(MESSAGE_VIEW_PERSON_SUCCESS, personToView), | ||
false, false, true, personToView, false); | ||
} | ||
|
||
/** | ||
* Compares this ViewCommand to another object for equality. | ||
* | ||
* @param other The object to compare to | ||
* @return true if the other object is also a ViewCommand targeting the same index | ||
*/ | ||
@Override | ||
public boolean equals(Object other) { | ||
return other == this | ||
|| (other instanceof ViewCommand | ||
&& targetIndex.equals(((ViewCommand) other).targetIndex)); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/parser/ViewCommandParser.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,28 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.ViewCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new ViewCommand object | ||
*/ | ||
public class ViewCommandParser implements Parser<ViewCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ViewCommand | ||
* and returns a ViewCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public ViewCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new ViewCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.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.