Skip to content

Commit

Permalink
Merge pull request #225 from jay9645/master
Browse files Browse the repository at this point in the history
Fix find command output and edit command prevent insert duplicate policies
  • Loading branch information
jay9645 authored Apr 12, 2021
2 parents e8cb8d8 + 8fee53e commit 8c8c589
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public class EditCommand extends Command implements BatchOperation {
public static final String MESSAGE_MODIFY_POLICY_CONSTRAINT = "When -modify flag is indicated for editing policy,"
+ " the format should be i/[TO_MODIFY];[TO_REPLACE]. ";
public static final String MESSAGE_MODIFY_POLICY_NOT_FOUND = "The policy %s to modify or delete is not found.";
public static final String MESSAGE_INSERT_DUPLICATE_POLICY = "The policy %s already exists in this "
+ "client's record.";


private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand Down Expand Up @@ -190,9 +193,15 @@ private static List<InsurancePolicy> removeInsurancePolicies(List<InsurancePolic
}

private static List<InsurancePolicy> addInsurancePolicies(List<InsurancePolicy> policiesToAddTo,
List<InsurancePolicy> policiesToAdd) {
List<InsurancePolicy> policiesToAdd) throws CommandException {
ArrayList<InsurancePolicy> policiesToAddToTemp = new ArrayList<>(policiesToAddTo);
policiesToAddToTemp.addAll(policiesToAdd);
for (InsurancePolicy insurancePolicy : policiesToAdd) {
if (policiesToAddTo.contains(insurancePolicy)) {
throw new CommandException(String.format(MESSAGE_INSERT_DUPLICATE_POLICY, insurancePolicy.policyId));
} else {
policiesToAddToTemp.add(insurancePolicy);
}
}
return policiesToAddToTemp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.model.Model;
import seedu.address.storage.Authentication;

//@@author swayongshen
public class LockCommand extends Command {

public static final String COMMAND_WORD = "lock";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import seedu.address.model.Model;
import seedu.address.storage.Authentication;

//@@author swayongshen
public class UnlockCommand extends Command {

public static final String COMMAND_WORD = "unlock";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BatchCommand<? extends BatchOperation> parse(String args) throws ParseExc
// Tokenizes and parses the user input
String inputIndicesAndArgs = " " + splitCommandAndIndicesAndArgs[1].trim();
ArgumentMultimap argMultimap = ArgumentTokenizer
.tokenize(inputIndicesAndArgs, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
.tokenize(inputIndicesAndArgs, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_TAG, PREFIX_INSURANCE_POLICY, PREFIX_MEETING,
PREFIX_SHORTCUT_COMMAND, PREFIX_SHORTCUT_NAME);
boolean doIndicesContainWords = ParserUtil.checkIndicesInputContainsWords(argMultimap.getPreamble());
Expand All @@ -76,7 +76,7 @@ public BatchCommand<? extends BatchOperation> parse(String args) throws ParseExc
EditCommand.MESSAGE_USAGE_BATCH));
} else if (doIndicesContainWords && inputCommand.equals(DeleteCommand.COMMAND_WORD)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeleteCommand.MESSAGE_USAGE_BATCH));
DeleteCommand.MESSAGE_USAGE_BATCH));
}

List<Index> listOfIndices = parseAndPrepareIndices(argMultimap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws Pars
* If {@code policies} contain only one element which is an empty string, it will be parsed into a
* {@code List<InsurancePolicy>} containing zero policies.
*/
private Optional<List<InsurancePolicy>> parsePoliciesForEdit(Collection<String> policyIds) {
private Optional<List<InsurancePolicy>> parsePoliciesForEdit(Collection<String> policyIds) throws ParseException {
assert policyIds != null;

if (policyIds.isEmpty()) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,15 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
* Leading and trailing whitespaces will be trimmed from the initial input.
* Leading and trailing whitespaces will also be trimmed from the input meant as URL.
*/
private static InsurancePolicy parsePolicy(String policy) {
private static InsurancePolicy parsePolicy(String policy) throws ParseException {
requireNonNull(policy);
String trimmedPolicy = policy.trim();
String[] idAndUrl = trimmedPolicy.split(">", 2);

if (!InsurancePolicy.isValidPolicyInput(trimmedPolicy)) {
throw new ParseException(InsurancePolicy.MESSAGE_CONSTRAINTS);
}

if (!InsurancePolicy.hasPolicyUrl(idAndUrl)) {
return new InsurancePolicy(idAndUrl[0]);
}
Expand All @@ -293,7 +297,7 @@ private static InsurancePolicy parsePolicy(String policy) {
/**
* Parses {@code Collection<String> policies} into a {@code Set<InsurancePolicy>}.
*/
public static Set<InsurancePolicy> parsePolicies(Collection<String> policies) {
public static Set<InsurancePolicy> parsePolicies(Collection<String> policies) throws ParseException {
requireNonNull(policies);
final Set<InsurancePolicy> policySet = new HashSet<>();
for (String policy : policies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class InsurancePolicy {
*/
public static final Pattern ANGULAR_BRACKET_REGEX = Pattern.compile("^[^<>]+$");

public static final String MESSAGE_CONSTRAINTS = "Policy id should not contain the ; character.";

public final String policyId;
private final String policyUrl;

Expand Down Expand Up @@ -109,6 +111,9 @@ public String toString() {
*/
public static boolean isValidPolicyInput(String input) {
String[] splitByAngularBracket = input.split(">", 2);
if (splitByAngularBracket[0].contains(";")) {
return false;
}
if (splitByAngularBracket.length == 1) {
// Return true if length is 1, since no '>' was used, meaning no URL and valid policy ID.
return true;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/storage/Authentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.FileUtil;

//@@author swayongshen
/**
* Handles the encryption and decryption of the data .json file.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.model.shortcut.ShortcutLibrary;
import seedu.address.storage.Authentication;

//@@author swayongshen
/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code LockCommand}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.address.model.shortcut.ShortcutLibrary;
import seedu.address.storage.Authentication;

//@@author swayongshen
/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code LockCommand}.
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@ public void parsePolicies_null_throwsNullPointerException() {
}

@Test
public void parsePolicies_emptyCollection_returnsEmptySet() {
public void parsePolicies_emptyCollection_returnsEmptySet() throws ParseException {
assertTrue(ParserUtil.parsePolicies(Collections.emptyList()).isEmpty());
}

@Test
public void parsePolicies_returnsPoliciesList() {
public void parsePolicies_returnsPoliciesList() throws ParseException {
Set<InsurancePolicy> actualPolicySet = ParserUtil.parsePolicies(Arrays.asList(VALID_POLICYID_NO_URL,
VALID_POLICYID_VALID_URL));
Set<InsurancePolicy> expectedPolicyList = new HashSet<>(Arrays.asList(new InsurancePolicy(VALID_POLICYID_1),
Expand Down

0 comments on commit 8c8c589

Please sign in to comment.