Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ListCommand class functionality #39

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/main/java/seedu/address/logic/commands/ListCommand.java
CJ-Lee01 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CONTACTS;
import static seedu.address.model.Model.PREDICATE_SHOW_ONLY_ORGANIZATIONS;
import static seedu.address.model.Model.PREDICATE_SHOW_ONLY_RECRUITERS;

import java.util.function.Predicate;

import seedu.address.model.Model;
import seedu.address.model.person.Contact;

/**
* Lists all contacts in the address book to the user.
Expand All @@ -12,13 +16,30 @@ public class ListCommand extends Command {

public static final String COMMAND_WORD = "list";

public static final String MESSAGE_SUCCESS = "Listed all contacts";
public static final String MESSAGE_SUCCESS_ALL_CONTACTS = "Listed all contacts";
public static final String MESSAGE_SUCCESS_ORGANIZATIONS = "Listed all organizations";
public static final String MESSAGE_SUCCESS_RECRUITERS = "Listed all recruiters";

private final Predicate<Contact> predicate;

/**
* Creates a ListCommand listing the {@code Contact} entries of the specified type.
* @param predicate the predicate determining the type of {@code Contact} to be listed
*/
public ListCommand(Predicate<Contact> predicate) {
requireNonNull(predicate);
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredContactList(PREDICATE_SHOW_ALL_CONTACTS);
return new CommandResult(MESSAGE_SUCCESS);
model.updateFilteredContactList(predicate);
if (predicate.equals(PREDICATE_SHOW_ONLY_ORGANIZATIONS)) {
return new CommandResult(MESSAGE_SUCCESS_ORGANIZATIONS);
} else if (predicate.equals(PREDICATE_SHOW_ONLY_RECRUITERS)) {
return new CommandResult(MESSAGE_SUCCESS_RECRUITERS);
}
return new CommandResult(MESSAGE_SUCCESS_ALL_CONTACTS);
}
}
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
46 changes: 46 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,46 @@
package seedu.address.logic.parser;

import static seedu.address.logic.parser.CliSyntax.FLAG_ORGANIZATION;
import static seedu.address.logic.parser.CliSyntax.FLAG_RECRUITER;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CONTACTS;
import static seedu.address.model.Model.PREDICATE_SHOW_ONLY_ORGANIZATIONS;
import static seedu.address.model.Model.PREDICATE_SHOW_ONLY_RECRUITERS;

import java.util.stream.Stream;

import seedu.address.logic.commands.ListCommand;

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

/**
* Parses the given {@code String} of arguments in the context of the ListCommand
* and returns a ListCommand object for execution.
*/
public ListCommand parse(String args) {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, FLAG_ORGANIZATION, FLAG_RECRUITER);

if (areFlagsPresent(argMultimap, FLAG_ORGANIZATION, FLAG_RECRUITER)) {
return new ListCommand(PREDICATE_SHOW_ALL_CONTACTS);
}

if (areFlagsPresent(argMultimap, FLAG_ORGANIZATION)) {
return new ListCommand(PREDICATE_SHOW_ONLY_ORGANIZATIONS);
} else if (areFlagsPresent(argMultimap, FLAG_RECRUITER)) {
return new ListCommand(PREDICATE_SHOW_ONLY_RECRUITERS);
}

return new ListCommand(PREDICATE_SHOW_ALL_CONTACTS);
}

/**
* Returns true if none of the flags contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean areFlagsPresent(ArgumentMultimap argumentMultimap, Flag... flags) {
return Stream.of(flags).allMatch(flag -> argumentMultimap.getValue(flag).isPresent());
}

}
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/model/Model.java
CJ-Lee01 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.Contact;
import seedu.address.model.person.Type;

/**
* The API of the Model component.
*/
public interface Model {
/** {@code Predicate} that always evaluate to true */
Predicate<Contact> PREDICATE_SHOW_ALL_CONTACTS = unused -> true;
Predicate<Contact> PREDICATE_SHOW_ALL_CONTACTS = contact -> true;
Predicate<Contact> PREDICATE_SHOW_ONLY_ORGANIZATIONS = contact -> contact.getType() == Type.ORGANIZATION;
Predicate<Contact> PREDICATE_SHOW_ONLY_RECRUITERS = contact -> contact.getType() == Type.RECRUITER;


/**
* Replaces user prefs data with the data in {@code userPrefs}.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void execute_commandExecutionError_throwsCommandException() {
@Test
public void execute_validCommand_success() throws Exception {
String listCommand = ListCommand.COMMAND_WORD;
assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model);
assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS_ALL_CONTACTS, model);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showContactAtIndex;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CONTACTS;
import static seedu.address.testutil.TypicalContacts.getTypicalAddressBook;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_CONTACT;

Expand All @@ -28,12 +29,14 @@ public void setUp() {

@Test
public void execute_listIsNotFiltered_showsSameList() {
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);
assertCommandSuccess(new ListCommand(PREDICATE_SHOW_ALL_CONTACTS), model,
ListCommand.MESSAGE_SUCCESS_ALL_CONTACTS, expectedModel);
}

@Test
public void execute_listIsFiltered_showsEverything() {
showContactAtIndex(model, INDEX_FIRST_CONTACT);
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);
assertCommandSuccess(new ListCommand(PREDICATE_SHOW_ALL_CONTACTS),
model, ListCommand.MESSAGE_SUCCESS_ALL_CONTACTS, expectedModel);
}
}
Loading