Skip to content

Commit

Permalink
Merge pull request #137 from jiahui0309/fix-find-command-bugs
Browse files Browse the repository at this point in the history
Fix UI bugs for FindCommand
  • Loading branch information
drustanyjt authored Apr 3, 2024
2 parents 68a8cd2 + c0fd8c6 commit 839e802
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 35 deletions.
20 changes: 18 additions & 2 deletions src/main/java/seedu/address/logic/commands/AddNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

Expand Down Expand Up @@ -74,12 +73,29 @@ public CommandResult execute(Model model) throws CommandException {
personToEdit.getIdentityCardNumber(), personToEdit.getAge(), personToEdit.getSex(),
personToEdit.getAddress(), updatedNote, personToEdit.getTags());
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

if (model.isPersonDisplayed(personToEdit)) {
model.setDisplayNote(editedPerson);
}

return new CommandResult(generateSuccessMessage(editedPerson));
}

/**
* Checks if the note of the person is changed.
* @param person the person to be compared with
* @param newNote the new note to be compared with
* @return true if the note is changed, false otherwise
*/
public boolean isNoteChanged(Person person, Note newNote) {
if (person != null) {
return !person.getNote().equals(newNote);
}
return false;
}

/**
* Generates a command execution success message based on whether the remark is added to or removed from
* {@code personToEdit}.
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.IdentityCardNumberMatchesPredicate;
import seedu.address.model.person.Person;

/**
* Finds and lists all persons in address book whose IC matches the argument IC.
Expand All @@ -17,7 +18,7 @@ public class FindCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose profile matches "
+ "the specified IC (case-insensitive) and displays them.\n"
+ "Parameters: IC (must be a valid identity card number) \n"
+ "Parameters: IC\n"
+ "Example: " + COMMAND_WORD + " t1234567A";

private final IdentityCardNumberMatchesPredicate predicate;
Expand All @@ -30,7 +31,14 @@ public FindCommand(IdentityCardNumberMatchesPredicate predicate) {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.setDisplayNoteAsFirstFilteredPerson();

assert model.getFilteredPersonList().size() <= 1 : "There should be at most one person in the filtered list";

if (model.getFilteredPersonList().size() == 1) {
Person person = model.getFilteredPersonList().get(0);
model.setDisplayNote(person);
}

return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/seedu/address/logic/commands/ShowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

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.IdentityCardNumberMatchesPredicate;
import seedu.address.model.person.Person;



/**
* Shows the user a note of a person in the address book.
* If no IC is given, the displayed note panel will be cleared.
*/
public class ShowCommand extends Command {

public static final String COMMAND_WORD = "show";
public static final String MESSAGE_SHOW_NOTE_SUCCESS = "Displayed note of person: %1$s";

public static final String MESSAGE_CLEAR_NOTE_SUCCESS = "Note cleared.";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows the note of the person whose IC matches the "
+ "specified valid IC (case-insensitive) and displays it.\n"
+ "If no IC is given, the displayed note panel will be cleared.\n"
+ "Parameters: IC (optional)\n"
+ "Example (to display note): " + COMMAND_WORD + " t1234567A\n"
+ "Example (to clear display): " + COMMAND_WORD;
private final IdentityCardNumberMatchesPredicate icPredicate;

private final boolean isClear;

/**
* Creates a ShowCommand to show the note of the person whose profile matches the specified {@code icPredicate}.
* @param icPredicate of the person in the filtered person list to show the note
*/
private ShowCommand(IdentityCardNumberMatchesPredicate icPredicate, boolean isClear) {
this.icPredicate = icPredicate;
this.isClear = isClear;
}

/**
* Factory method to create a ShowCommand to show the note of the person whose profile matches
* the specified {@code icPredicate}.
* @param icPredicate of the person in the filtered person list to show the note
*/
public static ShowCommand createShowCommand(IdentityCardNumberMatchesPredicate icPredicate) {
return new ShowCommand(icPredicate, false);
}

/**
* Factory method to create a ShowCommand to clear the displayed note panel.
*/
public static ShowCommand createClearCommand() {
return new ShowCommand(null, true);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (isClear) {
model.clearDisplayNote();
return new CommandResult(MESSAGE_CLEAR_NOTE_SUCCESS);
}

List<Person> allPatients = model.getAddressBook().getPersonList();

Person person = allPatients.stream()
.filter(icPredicate::test)
.findFirst()
.orElseThrow(() -> new CommandException(Messages.MESSAGE_NO_MATCHING_IC));

model.setDisplayNote(person);

return new CommandResult(
String.format(MESSAGE_SHOW_NOTE_SUCCESS, person.getIdentityCardNumber())
);
}


@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

ShowCommand e = (ShowCommand) other;
// icPredicate is not important if isClear is true
if (isClear && e.isClear) {
return true;
}
if (isClear != e.isClear) {
return false;
}
if (icPredicate == null || e.icPredicate == null) {
return icPredicate == e.icPredicate;
}
return icPredicate.equals(e.icPredicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", icPredicate)
.add("isClear", isClear)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ShowCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -69,6 +70,9 @@ public Command parseCommand(String userInput) throws ParseException {
case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

case ShowCommand.COMMAND_WORD:
return new ShowCommandParser().parse(arguments);

case AddNoteCommand.COMMAND_WORD:
return new AddNoteCommandParser().parse(arguments);

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/seedu/address/logic/parser/ShowCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.logic.commands.ShowCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.IdentityCardNumber;
import seedu.address.model.person.IdentityCardNumberMatchesPredicate;

/**
* Parses input arguments and creates a new ShowCommand object
*/
public class ShowCommandParser implements Parser<ShowCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ShowCommand
* and returns a ShowCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ShowCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
return ShowCommand.createClearCommand();
}

try {
IdentityCardNumber ic = ParserUtil.parseIC(trimmedArgs);
return ShowCommand.createShowCommand(new IdentityCardNumberMatchesPredicate(ic));
} catch (IllegalArgumentException e) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ShowCommand.MESSAGE_USAGE), e);
}
}

}
22 changes: 14 additions & 8 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AddressBook implements ReadOnlyAddressBook {
persons = new UniquePersonList();
}

private Optional<Note> displayNote = Optional.empty();
private Optional<Person> displayPerson = Optional.empty();

public AddressBook() {}

Expand Down Expand Up @@ -102,22 +102,28 @@ public void removePerson(Person key) {
* Get the display note.
*/
public Note getDisplayNote() {
return displayNote.orElse(Note.DEFAULT);
return displayPerson.map(Person::getNote).orElse(Note.EMPTY);
}

/**
* Sets the display note to the given note.
* @param note The note to be displayed.
* Set the display person.
*/
public void setDisplayNote(Note note) {
this.displayNote = Optional.of(note);
public void setDisplayPerson(Person person) {
this.displayPerson = Optional.of(person);
}

/**
* Check if person is displayed.
*/
public boolean isPersonDisplayed(Person person) {
return displayPerson.isPresent() && displayPerson.get().isSamePerson(person);
}

/**
* Clears the display note.
*/
public void clearDisplayNote() {
this.displayNote = Optional.empty();
public void clearDisplayPerson() {
this.displayPerson = Optional.empty();
}


Expand Down
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,21 @@ public interface Model {
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Returns the display note.
*/
Note getDisplayNote();

/**
* Sets the displayed note to the note of the given person.
*/
void setDisplayNote(Person person);

/**
* Replaces the note that is displayed in the note panel.
* Returns true if the person is displayed in the note panel.
*/
void setDisplayNote(Note note);
boolean isPersonDisplayed(Person person);


/**
* Sets the display note to the first filtered person.
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,15 @@ public Note getDisplayNote() {
}

@Override
public void setDisplayNote(Note note) {
requireNonNull(note);
addressBook.setDisplayNote(note);
public void setDisplayNote(Person person) {
requireNonNull(person);
addressBook.setDisplayPerson(person);
}

@Override
public void setDisplayNote(Person person) {
public boolean isPersonDisplayed(Person person) {
requireNonNull(person);
Note displayNote = person.getNote();
setDisplayNote(displayNote);
return addressBook.isPersonDisplayed(person);
}

/**
Expand All @@ -168,7 +167,7 @@ public void setDisplayNoteAsFirstFilteredPerson() {
*/
@Override
public void clearDisplayNote() {
addressBook.clearDisplayNote();
addressBook.clearDisplayPerson();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/person/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class Note {

public static final Note DEFAULT = new Note("");
public static final Note EMPTY = new Note("");
public static final String MESSAGE_CONSTRAINTS = "Notes can take any values, and it should not be blank";

public final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void setDisplayNote(Person person) {
}

@Override
public void setDisplayNote(Note note) {
public boolean isPersonDisplayed(Person person) {
throw new AssertionError("This method should not be called.");
}

Expand Down
Loading

0 comments on commit 839e802

Please sign in to comment.