forked from nus-cs2103-AY2324S1/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.
Add ViewCommand and ViewExitCommand for enhancements of editing functionality.
- Loading branch information
1 parent
54c428d
commit e4b07ff
Showing
10 changed files
with
321 additions
and
5 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
|
||
public class ViewCommand extends Command { | ||
public static final String COMMAND_WORD = "view"; | ||
|
||
private final Index targetIndex; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows the profile of the user.\n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
public static final String VIEWING_PROFILE_SUCCESS = "Viewing Person: %1$s"; | ||
public ViewCommand(Index index) { | ||
this.targetIndex = index; | ||
} | ||
@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 personToView = lastShownList.get(targetIndex.getZeroBased()); | ||
return new CommandResult( | ||
String.format(VIEWING_PROFILE_SUCCESS, Messages.format(personToView)), | ||
false, | ||
false, | ||
true, | ||
personToView, | ||
false); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ViewCommand)) { | ||
return false; | ||
} | ||
|
||
ViewCommand e = (ViewCommand) other; | ||
return targetIndex.equals(e.targetIndex); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIndex", targetIndex) | ||
.toString(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/seedu/address/logic/commands/ViewExitCommand.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,21 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import seedu.address.model.Model; | ||
|
||
public class ViewExitCommand extends Command { | ||
public static final String COMMAND_WORD = "exit"; | ||
|
||
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting view mode as requested ..."; | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
return new CommandResult( | ||
MESSAGE_EXIT_ACKNOWLEDGEMENT, | ||
false, | ||
false, | ||
false, | ||
null, | ||
true | ||
); | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
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; | ||
|
||
public class ViewCommandParser { | ||
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); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/seedu/address/logic/parser/ViewModeParser.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,56 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND; | ||
|
||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.EditCommand; | ||
import seedu.address.logic.commands.HelpCommand; | ||
import seedu.address.logic.commands.ViewExitCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
public class ViewModeParser { | ||
/** | ||
* Used for initial separation of command word and args. | ||
*/ | ||
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)"); | ||
private static final Logger logger = LogsCenter.getLogger(seedu.address.logic.parser.AddressBookParser.class); | ||
|
||
/** | ||
* Parses user input into command for execution. | ||
* | ||
* @param userInput full user input string | ||
* @return the command based on the user input | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public Command parseCommand(String userInput) throws ParseException { | ||
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); | ||
if (!matcher.matches()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
final String commandWord = matcher.group("commandWord"); | ||
final String arguments = matcher.group("arguments"); | ||
|
||
// Note to developers: Change the log level in config.json to enable lower level (i.e., FINE, FINER and lower) | ||
// log messages such as the one below. | ||
// Lower level log messages are used sparingly to minimize noise in the code. | ||
logger.fine("Command word: " + commandWord + "; Arguments: " + arguments); | ||
|
||
switch (commandWord) { | ||
|
||
case ViewExitCommand.COMMAND_WORD: | ||
return new ViewExitCommand(); | ||
|
||
default: | ||
logger.finer("This user input caused a ParseException: " + userInput); | ||
throw new ParseException(MESSAGE_UNKNOWN_COMMAND); | ||
} | ||
} | ||
|
||
} |
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.