forked from AY2324S1-CS2103T-T12-4/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pin and unpin logic and commands
- Loading branch information
Showing
12 changed files
with
257 additions
and
1 deletion.
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
73 changes: 73 additions & 0 deletions
73
src/main/java/seedu/address/logic/commands/PinCommand.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,73 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
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; | ||
|
||
/** | ||
* Pins the person identified using it's displayed index from the address book. | ||
*/ | ||
public class PinCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "pin"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Pins 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_PIN_PERSON_SUCCESS = "Pinned Person: %1$s"; | ||
|
||
private final Index targetIndex; | ||
|
||
public PinCommand(Index targetIndex) { | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@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 personToPin = lastShownList.get(targetIndex.getZeroBased()); | ||
Person pinnedPerson = new Person(personToPin); | ||
pinnedPerson.setPinned(true); | ||
|
||
model.setPerson(personToPin, pinnedPerson); | ||
return new CommandResult(String.format(MESSAGE_PIN_PERSON_SUCCESS, Messages.format(personToPin))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof PinCommand)) { | ||
return false; | ||
} | ||
|
||
PinCommand otherPinCommand = (PinCommand) other; | ||
return targetIndex.equals(otherPinCommand.targetIndex); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIndex", targetIndex) | ||
.toString(); | ||
} | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
src/main/java/seedu/address/logic/commands/UnpinCommand.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,73 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
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; | ||
|
||
/** | ||
* Unpins the person identified using it's displayed index from the address book. | ||
*/ | ||
public class UnpinCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "unpin"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Unpins the person identified by the index number used in the displayed pinned list.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
|
||
public static final String MESSAGE_UNPIN_PERSON_SUCCESS = "Unpinned Person: %1$s"; | ||
|
||
private final Index targetIndex; | ||
|
||
public UnpinCommand(Index targetIndex) { | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Person> lastShownPinnedList = model.getPinnedPersonList(); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownPinnedList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToUnpin = lastShownPinnedList.get(targetIndex.getZeroBased()); | ||
Person unpinnedPerson = new Person(personToUnpin); | ||
unpinnedPerson.setPinned(false); | ||
|
||
model.setPerson(personToUnpin, unpinnedPerson); | ||
return new CommandResult(String.format(MESSAGE_UNPIN_PERSON_SUCCESS, Messages.format(personToUnpin))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof UnpinCommand)) { | ||
return false; | ||
} | ||
|
||
UnpinCommand otherUnpinCommand = (UnpinCommand) other; | ||
return targetIndex.equals(otherUnpinCommand.targetIndex); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIndex", targetIndex) | ||
.toString(); | ||
} | ||
} | ||
|
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
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/address/logic/parser/PinCommandParser.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 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.PinCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new PinCommand object | ||
*/ | ||
public class PinCommandParser implements Parser<PinCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the PinCommand | ||
* and returns a PinCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public PinCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new PinCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, PinCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/address/logic/parser/UnpinCommandParser.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 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.UnpinCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new UnpinCommand object | ||
*/ | ||
public class UnpinCommandParser implements Parser<UnpinCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the UnpinCommand | ||
* and returns a UnpinCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public UnpinCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new UnpinCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnpinCommand.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
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