Skip to content

Commit

Permalink
Merge pull request #144 from AY2122S1-CS2103-F10-4/sherwin-optional-e…
Browse files Browse the repository at this point in the history
…xpiry

Sherwin make expiry date optional
  • Loading branch information
zhuoyang125 authored Nov 4, 2021
2 parents 86c8d99 + 67d875f commit 24644c6
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class AddPolicyCommand extends Command {
*/
public AddPolicyCommand(Title title, PaymentStructure paymentStructure, CoverageExpiryDate coverageExpiryDate,
Commission commission, Index index, Set<Tag> tagList) {
requireAllNonNull(title, paymentStructure, coverageExpiryDate, index, tagList);
requireAllNonNull(title, paymentStructure, index, tagList);
this.title = title;
this.paymentStructure = paymentStructure;
this.coverageExpiryDate = coverageExpiryDate;
Expand All @@ -88,7 +88,7 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(Messages.MESSAGE_INVALID_CONTACT_DISPLAYED_INDEX);
}

if (!CoverageExpiryDate.isFutureExpiryDate(coverageExpiryDate.value)) {
if (coverageExpiryDate != null && !CoverageExpiryDate.isFutureExpiryDate(coverageExpiryDate.value)) {
boolean response = Warning.warnUser(MESSAGE_PAST_EXPIRY_DATE);
if (!response) {
return new CommandResult(Messages.MESSAGE_CANCELLED_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static Policy createEditedPolicy(
PaymentStructure updatedPaymentStructure = editPolicyDescriptor.getPaymentStructure()
.orElse(policyToEdit.getPaymentStructure());
CoverageExpiryDate updatedCoverageExpiryDate = editPolicyDescriptor.getCoverageExpiryDate()
.orElse(policyToEdit.getCoverageExpiryDate());
.orElse(policyToEdit.getCoverageExpiryDate().orElse(null));
Commission updatedCommission = editPolicyDescriptor.getCommission().orElse(policyToEdit.getCommission());
Set<Tag> updatedTags = editPolicyDescriptor.getTags().orElse(policyToEdit.getTags());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ShowExpiringPolicyCommand extends Command {

public static final String COMMAND_WORD = "expiringpolicy";

private static final LocalDate CUT_OFF_DATE = LocalDate.now().plusMonths(1);

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand All @@ -25,7 +27,8 @@ public CommandResult execute(Model model) {

while (policyListIterator.hasNext()) {
Policy policy = policyListIterator.next();
if (policy.getCoverageExpiryDate().value.isBefore(LocalDate.now().plusMonths(1))) {

if (policy.isExpiringBefore(CUT_OFF_DATE)) {
listOfPolicies.append(policy);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public AddPolicyCommand parse(String args) throws ParseException {

if (!arePrefixesPresent(argMultimap,
PREFIX_TITLE,
PREFIX_EXPIRY,
PREFIX_PAYMENT,
PREFIX_COMMISSION,
PREFIX_CONTACT_INDEX)
Expand All @@ -60,8 +59,10 @@ public AddPolicyCommand parse(String args) throws ParseException {
ParserUtil.parsePaymentStructure(argMultimap.getValue(PREFIX_PAYMENT).get());
Commission commission =
ParserUtil.parseCommission(argMultimap.getValue(PREFIX_COMMISSION).get(), paymentStructure);
CoverageExpiryDate coverageExpiryDate =
ParserUtil.parseCoverageExpiryDate(argMultimap.getValue(PREFIX_EXPIRY).get());
CoverageExpiryDate coverageExpiryDate = null;
if (argMultimap.getValue(PREFIX_EXPIRY).isPresent()) {
coverageExpiryDate = ParserUtil.parseCoverageExpiryDate(argMultimap.getValue(PREFIX_EXPIRY).get());
}
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT_INDEX).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/seedu/siasa/model/policy/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static seedu.siasa.commons.util.CollectionUtil.requireAllNonNull;

import java.time.LocalDate;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.siasa.model.contact.Contact;
Expand All @@ -16,6 +18,8 @@
*/
public class Policy {

public static final String MESSAGE_NO_EXPIRY = "No coverage expiry defined.";

private final Title title;
private final PaymentStructure paymentStructure;
private final CoverageExpiryDate coverageExpiryDate;
Expand All @@ -26,11 +30,11 @@ public class Policy {
private final Contact owner;

/**
* Every field must be present and not null.
* coverageExpiryDate can be optional and hence null.
*/
public Policy(Title title, PaymentStructure paymentStructure, CoverageExpiryDate coverageExpiryDate,
Commission commission, Contact owner, Set<Tag> tags) {
requireAllNonNull(title, paymentStructure, coverageExpiryDate, commission, owner, tags);
requireAllNonNull(title, paymentStructure, commission, owner, tags);
this.title = title;
this.paymentStructure = paymentStructure;
this.coverageExpiryDate = coverageExpiryDate;
Expand All @@ -47,8 +51,8 @@ public PaymentStructure getPaymentStructure() {
return paymentStructure;
}

public CoverageExpiryDate getCoverageExpiryDate() {
return coverageExpiryDate;
public Optional<CoverageExpiryDate> getCoverageExpiryDate() {
return Optional.ofNullable(coverageExpiryDate);
}

public Commission getCommission() {
Expand Down Expand Up @@ -92,6 +96,10 @@ public boolean isSimilarPolicy(Policy otherPolicy) {
&& otherPolicy.getTitle().isSimilarTo(getTitle());
}

public boolean isExpiringBefore(LocalDate cutOff) {
return getCoverageExpiryDate().map(date -> date.value.isBefore(cutOff)).orElse(false);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down Expand Up @@ -125,10 +133,11 @@ public String toString() {
.append("; Payment Structure: ")
.append(getPaymentStructure())
.append("; Commission: ")
.append(getCommission())
.append("; Expiry Date: ")
.append(getCoverageExpiryDate());

.append(getCommission());
if (getCoverageExpiryDate().isPresent()) {
builder.append("; Expiry Date: ")
.append(getCoverageExpiryDate().get());
}
Set<Tag> tags = getTags();
if (!tags.isEmpty()) {
builder.append("; Tags: ");
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/siasa/model/policy/PolicyComparator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.siasa.model.policy;

import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.util.Comparator;

Expand All @@ -23,10 +24,12 @@ public class PolicyComparator {
a, b) -> -Integer.compare(a.getCommission().commissionPercentage, b.getCommission().commissionPercentage);

public static final Comparator<Policy> POLICY_SORT_BY_DATE_ASC = (
a, b) -> ChronoLocalDate.timeLineOrder().compare(a.getCoverageExpiryDate().value,
b.getCoverageExpiryDate().value);
a, b) -> ChronoLocalDate.timeLineOrder().compare(
a.getCoverageExpiryDate().orElse(new CoverageExpiryDate(LocalDate.MAX)).value,
b.getCoverageExpiryDate().orElse(new CoverageExpiryDate(LocalDate.MAX)).value);

public static final Comparator<Policy> POLICY_SORT_BY_DATE_DSC = (
a, b) -> -ChronoLocalDate.timeLineOrder().compare(a.getCoverageExpiryDate().value,
b.getCoverageExpiryDate().value);
a, b) -> -ChronoLocalDate.timeLineOrder().compare(
a.getCoverageExpiryDate().orElse(new CoverageExpiryDate(LocalDate.MAX)).value,
b.getCoverageExpiryDate().orElse(new CoverageExpiryDate(LocalDate.MAX)).value);
}
24 changes: 13 additions & 11 deletions src/main/java/seedu/siasa/storage/JsonAdaptedPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.siasa.commons.exceptions.IllegalValueException;
Expand All @@ -31,6 +32,7 @@ public class JsonAdaptedPolicy {

private final String title;
private final JsonAdaptedPaymentStructure paymentStructure;
@JsonInclude(JsonInclude.Include.NON_NULL)
private final String coverageExpiryDate;
private final JsonAdaptedCommission commission;
private final JsonAdaptedContact owner;
Expand Down Expand Up @@ -62,7 +64,7 @@ public JsonAdaptedPolicy(@JsonProperty("title") String title,
public JsonAdaptedPolicy(Policy source) {
title = source.getTitle().toString();
paymentStructure = new JsonAdaptedPaymentStructure(source.getPaymentStructure());
coverageExpiryDate = source.getCoverageExpiryDate().toString();
coverageExpiryDate = source.getCoverageExpiryDate().map(date -> date.toString()).orElse(null);
commission = new JsonAdaptedCommission(source.getCommission());
owner = new JsonAdaptedContact(source.getOwner());
tagged.addAll(source.getTags().stream()
Expand Down Expand Up @@ -102,20 +104,20 @@ public Policy toModelType(Contact policyOwner) throws IllegalValueException {


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
if (coverageExpiryDate == null) {
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, CoverageExpiryDate.class.getSimpleName()));
final CoverageExpiryDate modelCoverageExpiryDate;
if (coverageExpiryDate != null) {
try {
LocalDate.parse(coverageExpiryDate, formatter);
} catch (DateTimeParseException e) {
throw new IllegalValueException(e.getMessage());
}
LocalDate date = LocalDate.parse(coverageExpiryDate, formatter);

} catch (IllegalValueException | DateTimeParseException e) {
throw new IllegalValueException(e.getMessage());
LocalDate date = LocalDate.parse(coverageExpiryDate, formatter);
modelCoverageExpiryDate = new CoverageExpiryDate(date);
} else {
modelCoverageExpiryDate = null;
}

LocalDate date = LocalDate.parse(coverageExpiryDate, formatter);
final CoverageExpiryDate modelCoverageExpiryDate = new CoverageExpiryDate(date);

if (commission == null) {
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, Commission.class.getSimpleName()));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/siasa/ui/PolicyCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public PolicyCard(Policy policy, int displayedIndex) {
id.setText(displayedIndex + ". ");
title.setText(policy.getTitle().value);
price.setText(String.valueOf(policy.getPaymentStructure()));
expiryDate.setText(String.valueOf(policy.getCoverageExpiryDate()));
expiryDate.setText(policy.getCoverageExpiryDate().map(date -> "Coverage Expiry: " + date)
.orElse(Policy.MESSAGE_NO_EXPIRY));
commission.setText(String.valueOf(policy.getCommission()));
owner.setText(String.valueOf(policy.getOwner().getName()));
policy.getTags().stream()
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/seedu/siasa/storage/JsonAdaptedPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import seedu.siasa.commons.exceptions.IllegalValueException;
import seedu.siasa.model.policy.Commission;
import seedu.siasa.model.policy.CoverageExpiryDate;
import seedu.siasa.model.policy.PaymentStructure;
import seedu.siasa.model.policy.Policy;
import seedu.siasa.model.policy.Title;
import seedu.siasa.testutil.PolicyBuilder;

public class JsonAdaptedPolicyTest {
private static final String INVALID_TITLE = "Policy#";
Expand All @@ -25,7 +26,7 @@ public class JsonAdaptedPolicyTest {
private static final String INVALID_COMMISSION = "1000";

private static final String VALID_TITLE = FULL_LIFE.getTitle().toString();
private static final String VALID_EXPIRY_DATE = FULL_LIFE.getCoverageExpiryDate().toString();
private static final String VALID_EXPIRY_DATE = FULL_LIFE.getCoverageExpiryDate().get().toString();
private static final JsonAdaptedContact VALID_OWNER = new JsonAdaptedContact(FULL_LIFE.getOwner());
private static final JsonAdaptedCommission VALID_COMMISSION = new JsonAdaptedCommission(FULL_LIFE.getCommission());
private static final JsonAdaptedPaymentStructure VALID_PAYMENT_STRUCTURE =
Expand All @@ -36,7 +37,15 @@ public class JsonAdaptedPolicyTest {
@Test
public void toModelType_validPolicyDetails_returnsPolicy() throws Exception {
JsonAdaptedPolicy policy = new JsonAdaptedPolicy(FULL_LIFE);
assertEquals(FULL_LIFE, policy.toModelType(VALID_OWNER.toModelType()));
assertEquals(FULL_LIFE, policy.toModelType(FULL_LIFE.getOwner()));
}

@Test
public void toModelType_nullExpiryDate_returnsPolicy() throws Exception {
Policy noExpiryDatePolicy = new PolicyBuilder(FULL_LIFE).withNoExpiryDate().build();
JsonAdaptedPolicy policy =
new JsonAdaptedPolicy(noExpiryDatePolicy);
assertEquals(noExpiryDatePolicy, policy.toModelType(FULL_LIFE.getOwner()));
}

@Test
Expand Down Expand Up @@ -74,15 +83,6 @@ public void toModelType_nullPrice_throwsIllegalValueException() {
assertThrows(IllegalValueException.class, expectedMessage, () -> policy.toModelType(ALICE));
}

@Test
public void toModelType_nullExpiryDate_throwsIllegalValueException() {
JsonAdaptedPolicy policy =
new JsonAdaptedPolicy(VALID_TITLE, VALID_PAYMENT_STRUCTURE, null, VALID_COMMISSION, VALID_OWNER,
VALID_TAGS);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, CoverageExpiryDate.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, () -> policy.toModelType(ALICE));
}

/*
@Test
public void toModelType_invalidCommission_throwsIllegalValueException() {
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/seedu/siasa/testutil/PolicyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public PolicyBuilder(Policy policyToCopy) {
requireNonNull(policyToCopy);
this.title = policyToCopy.getTitle();
this.paymentStructure = policyToCopy.getPaymentStructure();
this.coverageExpiryDate = policyToCopy.getCoverageExpiryDate();
this.coverageExpiryDate = policyToCopy.getCoverageExpiryDate().orElse(null);
this.commission = policyToCopy.getCommission();
this.owner = policyToCopy.getOwner();
this.tags = new HashSet<>(policyToCopy.getTags());
Expand Down Expand Up @@ -108,6 +108,15 @@ public PolicyBuilder withExpiryDate(LocalDate date) {
return this;
}

/**
* Removes the {@code ExpiryDate} of the {@code Policy} that we are building.
* @return
*/
public PolicyBuilder withNoExpiryDate() {
this.coverageExpiryDate = null;
return this;
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code Policy} that we are building.
*/
Expand Down

0 comments on commit 24644c6

Please sign in to comment.