Skip to content

Commit

Permalink
Merge pull request #114 from iamdiluxedbutcooler/add-view-and-close-c…
Browse files Browse the repository at this point in the history
…ommand

Add view command
  • Loading branch information
iamdiluxedbutcooler authored Oct 22, 2024
2 parents 055ee03 + 6a3dcca commit 8ca6273
Show file tree
Hide file tree
Showing 24 changed files with 712 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CommandResult execute(Model model, Boolean confirmationReceived) {
if (confirmationReceived.equals(requiresConfirmation)) {
return this.execute(model);
}
return new CommandResult(MESSAGE_CLEAR_CONFIRMATION, false, false, true);
return new CommandResult(MESSAGE_CLEAR_CONFIRMATION, false, false, true, null, false);
}

}
27 changes: 22 additions & 5 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Objects;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Person;

/**
* Represents the result of a command execution.
Expand All @@ -22,13 +23,19 @@ public class CommandResult {
/** The application should show a confirmation button */
private final boolean showConfirmation;

private final boolean showPerson;
private final Person viewedPerson;

/**
* Constructs a {@code CommandResult} with the specified fields.
*/
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit, boolean showConfirmation) {
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit,
boolean showPerson, Person viewedPerson, boolean showConfirmation) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = showHelp;
this.exit = exit;
this.showPerson = showPerson;
this.viewedPerson = viewedPerson;
this.showConfirmation = showConfirmation;
}

Expand All @@ -37,7 +44,7 @@ public CommandResult(String feedbackToUser, boolean showHelp, boolean exit, bool
* and other fields set to their default value.
*/
public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false, false);
this(feedbackToUser, false, false, false, null, false);
}

public String getFeedbackToUser() {
Expand All @@ -52,6 +59,14 @@ public boolean isExit() {
return exit;
}

public boolean isShowPerson() {
return showPerson;
}

public Person getViewedPerson() {
return viewedPerson;
}

public boolean isShowConfirmation() {
return showConfirmation;
}
Expand All @@ -62,20 +77,22 @@ public boolean equals(Object other) {
return true;
}

// instanceof handles nulls
if (!(other instanceof CommandResult)) {
return false;
}

CommandResult otherCommandResult = (CommandResult) other;
return feedbackToUser.equals(otherCommandResult.feedbackToUser)
&& showHelp == otherCommandResult.showHelp
&& exit == otherCommandResult.exit;
&& exit == otherCommandResult.exit
&& showConfirmation == otherCommandResult.showConfirmation
&& showPerson == otherCommandResult.showPerson
&& Objects.equals(viewedPerson, otherCommandResult.viewedPerson);
}

@Override
public int hashCode() {
return Objects.hash(feedbackToUser, showHelp, exit);
return Objects.hash(feedbackToUser, showHelp, exit, showConfirmation, showPerson, viewedPerson);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CommandResult execute(Model model, Boolean confirmationReceived) throws C
if (confirmationReceived.equals(requiresConfirmation)) {
return this.execute(model);
}
return new CommandResult(MESSAGE_DELETE_CONFIRMATION, false, false, true);
return new CommandResult(MESSAGE_DELETE_CONFIRMATION, false, false, true, null, false);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class ExitCommand extends Command {
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";

@Override
protected CommandResult execute(Model model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true, false);
public CommandResult execute(Model model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true, false, null, false);
}

}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HelpCommand extends Command {
public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
protected CommandResult execute(Model model) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false, false);
public CommandResult execute(Model model) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false, false, null, false);
}
}
76 changes: 76 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -78,6 +79,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ViewCommand.COMMAND_WORD:
return new ViewCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/ViewCommandParser.java
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);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Path;
import java.util.function.Predicate;

import javafx.beans.property.ReadOnlyProperty;
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -84,4 +85,21 @@ public interface Model {
* @throws NullPointerException if {@code predicates} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Selected person in the filtered person list.
* null if no person is selected.
*/
ReadOnlyProperty<Person> selectedPersonProperty();

/**
* Returns the selected person in the filtered person list.
* null if no person is selected.
*/
Person getSelectedPerson();

/**
* Sets the selected person in the filtered person list.
*/
void setSelectedPerson(Person person);
}
45 changes: 45 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import java.util.function.Predicate;
import java.util.logging.Logger;

import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.Person;
import seedu.address.model.person.exceptions.PersonNotFoundException;

/**
* Represents the in-memory model of the address book data.
Expand All @@ -22,11 +26,13 @@ public class ModelManager implements Model {
private final AddressBook addressBook;
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;
private final SimpleObjectProperty<Person> selectedPerson = new SimpleObjectProperty<>();

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
*/
public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs) {
super();
requireAllNonNull(addressBook, userPrefs);

logger.fine("Initializing with address book: " + addressBook + " and user prefs " + userPrefs);
Expand All @@ -40,6 +46,7 @@ public ModelManager() {
this(new AddressBook(), new UserPrefs());
}


//=========== UserPrefs ==================================================================================

@Override
Expand Down Expand Up @@ -111,6 +118,44 @@ public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

//=========== Selected Person ===========================================================================

@Override
public ReadOnlyProperty<Person> selectedPersonProperty() {
return selectedPerson;
}

@Override
public Person getSelectedPerson() {
return selectedPerson.getValue();
}

@Override
public void setSelectedPerson(Person person) {
requireNonNull(person);
if (!filteredPersons.contains(person)) {
throw new PersonNotFoundException();
}
selectedPerson.setValue(person);
}

/**
* Ensures {@code selectedPerson} is a valid person in {@code filteredPersons}.
*/
private void ensureSelectedPersonIsValid(ListChangeListener.Change<? extends Person> change) {
while (change.next()) {
if (this.getSelectedPerson() == null) {
return;
}

boolean wasSelectedPersonRemoved = change.getRemoved().stream()
.anyMatch(removedPerson -> selectedPerson.getValue().isSamePerson(removedPerson));
if (wasSelectedPersonRemoved) {
this.setSelectedPerson(change.getFrom() > 0 ? filteredPersons.get(change.getFrom() - 1) : null);
}
}
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down
Loading

0 comments on commit 8ca6273

Please sign in to comment.