Skip to content

Commit

Permalink
Add pin and unpin logic and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren159 committed Oct 13, 2023
1 parent e14df6e commit d6e5e80
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();

/** Returns an unmodifiable view of the pinned list of persons */
ObservableList<Person> getPinnedPersonList();

/**
* Returns the user prefs' address book file path.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public ObservableList<Person> getFilteredPersonList() {
return model.getFilteredPersonList();
}

@Override
public ObservableList<Person> getPinnedPersonList() {
return model.getPinnedPersonList();
}

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/seedu/address/logic/commands/PinCommand.java
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 src/main/java/seedu/address/logic/commands/UnpinCommand.java
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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.PinCommand;
import seedu.address.logic.commands.UnpinCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -62,6 +64,12 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);

case PinCommand.COMMAND_WORD:
return new PinCommandParser().parse(arguments);

case UnpinCommand.COMMAND_WORD:
return new UnpinCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/logic/parser/PinCommandParser.java
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 src/main/java/seedu/address/logic/parser/UnpinCommandParser.java
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);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/** Returns an unmodifiable view of the pinned person list */
ObservableList<Person> getPinnedPersonList();
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

@Override
public ObservableList<Person> getPinnedPersonList() {
FilteredList<Person> pinnedPersons = new FilteredList<>(this.addressBook.getPersonList());
pinnedPersons.setPredicate(person -> person.isPinned());
return pinnedPersons;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -145,4 +152,6 @@ public boolean equals(Object other) {
&& filteredPersons.equals(otherModelManager.filteredPersons);
}



}
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Person {
// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private boolean isPinned;

/**
* Every field must be present and not null.
Expand All @@ -35,6 +36,16 @@ public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tag
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.isPinned = false;
}

public Person(Person otherPerson) {
this.name = otherPerson.getName();
this.phone = otherPerson.getPhone();
this.email = otherPerson.getEmail();
this.address = otherPerson.getAddress();
this.isPinned = otherPerson.isPinned();
this.tags.addAll(otherPerson.getTags());
}

public Name getName() {
Expand All @@ -53,6 +64,14 @@ public Address getAddress() {
return address;
}

public boolean isPinned() {
return isPinned;
}

public void setPinned(boolean isPinned) {
this.isPinned = isPinned;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void fillInnerParts() {
personListPanel = new PersonListPanel(logic.getFilteredPersonList());
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

pinnedPersonListPanel = new PinnedPersonListPanel(logic.getFilteredPersonList());
pinnedPersonListPanel = new PinnedPersonListPanel(logic.getPinnedPersonList());
pinnedPersonListPanelPlaceholder.getChildren().add(pinnedPersonListPanel.getRoot());

resultDisplay = new ResultDisplay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public ObservableList<Person> getFilteredPersonList() {
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getPinnedPersonList() {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down

0 comments on commit d6e5e80

Please sign in to comment.