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

ListClient by product #190

Merged
merged 4 commits into from
Oct 27, 2022
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
17 changes: 15 additions & 2 deletions src/main/java/seedu/address/logic/commands/ListClientCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CLIENTS;

import seedu.address.model.Model;
import seedu.address.model.product.Product;

/**
* Lists all persons in MyInsuRec to the user.
*/
public class ListClientCommand extends Command {

public static final String COMMAND_WORD = "listClient";

public static final String MESSAGE_SUCCESS = "Listed all clients";
private final Object object;

public ListClientCommand() {
this.object = null;
}

public ListClientCommand(Product product) {
this.object = product;
}


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredClientList(PREDICATE_SHOW_ALL_CLIENTS);
if (this.object == null) {
model.updateFilteredClientList(PREDICATE_SHOW_ALL_CLIENTS);
} else if (this.object instanceof Product) {
model.updateFilteredClientList(client -> client.hasProduct((Product) object));
}
return new CommandResult(MESSAGE_SUCCESS, CommandSpecific.CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ViewClientCommand extends Command {
private final Index targetIndex;

/**
* Creates an AddCommand to add the specified {@code Client}
* Creates an ViewClientCommand to view the specified {@code Client}
*/
public ViewClientCommand(Index targetIndex) {
this.targetIndex = targetIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public AddClientCommand parse(String args) throws ParseException {
? Optional.of(new Birthday(ParserUtil.parseDate(
argMultimap.getValue(PREFIX_BIRTHDAY).get(), "birthday")))
: Optional.empty();
Set<Product> product = ParserUtil.parseProducts(argMultimap.getAllValues(PREFIX_PRODUCT));
Client client = new Client(name, phone, email, address, birthday, product);
Set<Product> products = ParserUtil.parseProducts(argMultimap.getAllValues(PREFIX_PRODUCT));
Client client = new Client(name, phone, email, address, birthday, products);
return new AddClientCommand(client);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package seedu.address.logic.parser;


import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRODUCT;

import java.util.stream.Stream;

import seedu.address.logic.commands.ListClientCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.product.Product;

/**
* Parses input arguments and creates a new ListClientCommand object
*/
public class ListClientCommandParser {

/**
* Parses the given {@code String} of arguments in the context of the ViewMeetingCommand
* and returns a ViewMeetingCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ListClientCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_PRODUCT);

if (!arePrefixesPresent(argumentMultimap, PREFIX_PRODUCT)) {
return new ListClientCommand();
} else {
Product product = ParserUtil.parseProduct(argumentMultimap.getValue(PREFIX_PRODUCT).get());
return new ListClientCommand(product);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to pass the product straight to the ListClientCommand and then form the predicate inside the command itself. IMO, the parser classes should be concerned with parsing only (i.e. turning the argument strings into the command).

}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Command parseCommand(String userInput) throws ParseException {
return new FindCommandParser().parse(arguments);

case ListClientCommand.COMMAND_WORD:
return new ListClientCommand();
return new ListClientCommandParser().parse(arguments);

case ViewMeetingCommand.COMMAND_WORD:
return new ViewMeetingCommandParser().parse(arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public void setUp() {

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

@Test
public void execute_listIsFiltered_showsEverything() {
showClientAtIndex(model, INDEX_FIRST_ELEMENT);
assertCommandSuccess(new ListClientCommand(), model, ListClientCommand.MESSAGE_SUCCESS, expectedModel);
assertCommandSuccess(new ListClientCommand(),
model, ListClientCommand.MESSAGE_SUCCESS, expectedModel);
}
}