Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-W09-1#45 from GERARDJM018/V1.2-List
Browse files Browse the repository at this point in the history
Update ListCommand.java
  • Loading branch information
LimZiJia authored Mar 18, 2024
2 parents 5740de0 + dd2edab commit 18f4ca9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.TypePredicate;

/**
* Lists all persons in the address book to the user.
Expand All @@ -14,11 +16,21 @@ public class ListCommand extends Command {

public static final String MESSAGE_SUCCESS = "Listed all persons";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Show list of the contacts with the given type.\n"
+ "Parameters: TYPE\n"
+ "Example: " + COMMAND_WORD + " housekeeper";
private final TypePredicate predicate;
public ListCommand() {
this.predicate = (TypePredicate) PREDICATE_SHOW_ALL_PERSONS;
}

public ListCommand(TypePredicate predicate) {
this.predicate = predicate;
}
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.updateFilteredPersonList(predicate);
return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Command parseCommand(String userInput) throws ParseException {
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();
return new ListCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/seedu/address/logic/parser/ListCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.address.logic.parser;

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

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.TypePredicate;

/**
* Parses input arguments and creates a new FindCommand object
*/
public class ListCommandParser implements Parser<ListCommand> {

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

String[] type = trimmedArgs.split("\\s+");

if (type.length != 1) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE));
}


return new ListCommand(new TypePredicate(trimmedArgs));
}

}
50 changes: 50 additions & 0 deletions src/main/java/seedu/address/model/person/TypePredicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.ClearCommand;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class TypePredicate implements Predicate<Person> {
private final String type;

public TypePredicate(String type) {
this.type = type;
}

@Override
public boolean test(Person person) {
if (type.equals("housekeeper")) {
return !person.isClient();
} else if (type.equals("client")) {
return person.isClient();
} else {
return false;
}
}

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

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

TypePredicate otherNameContainsKeywordsPredicate = (TypePredicate) other;
return type.equals(otherNameContainsKeywordsPredicate.type);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("type", type).toString();
}
}

0 comments on commit 18f4ca9

Please sign in to comment.