Skip to content

Commit

Permalink
Merge pull request #190 from AY2223S1-CS2103T-W16-4/feat-viewclient-b…
Browse files Browse the repository at this point in the history
…y-product

ListClient by product
  • Loading branch information
ThomasHoooo authored Oct 27, 2022
2 parents 9674512 + 8b75093 commit 382ca44
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
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);
}
}

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);
}
}

0 comments on commit 382ca44

Please sign in to comment.