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

Replace loan value types with BigDecimals #123

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VALUE;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -93,7 +94,7 @@

private LinkLoanDescriptor generateEditedLoanDetails(Loan loanToEdit, EditLoanDescriptor editedDetails)
throws CommandException {
float newValue = editedDetails.getValue().orElse(loanToEdit.getValue());
BigDecimal newValue = editedDetails.getValue().orElse(loanToEdit.getValue());

Check warning on line 97 in src/main/java/seedu/address/logic/commands/EditLoanCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/EditLoanCommand.java#L97

Added line #L97 was not covered by tests
Date newStartDate = editedDetails.getStartDate().orElse(loanToEdit.getStartDate());
Date newReturnDate = editedDetails.getReturnDate().orElse(loanToEdit.getReturnDate());
requireAllNonNull(newValue, newStartDate, newReturnDate);
Expand Down Expand Up @@ -131,7 +132,7 @@
* Stores the details of the loan that is edited.
*/
public static class EditLoanDescriptor {
private Float value = null;
private BigDecimal value = null;

Check warning on line 135 in src/main/java/seedu/address/logic/commands/EditLoanCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/EditLoanCommand.java#L135

Added line #L135 was not covered by tests
private Date startDate = null;
private Date returnDate = null;

Expand All @@ -147,11 +148,11 @@
return CollectionUtil.isAnyNonNull(value, startDate, returnDate);
}

public void setValue(float value) {
public void setValue(BigDecimal value) {
this.value = value;
}

public Optional<Float> getValue() {
public Optional<BigDecimal> getValue() {
return Optional.ofNullable(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VALUE;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -102,7 +103,7 @@
* Stores the details of the loan to be linked.
*/
public static class LinkLoanDescriptor {
private float value;
private BigDecimal value;
private Date startDate;
private Date returnDate;

Expand All @@ -112,7 +113,7 @@
* @param startDate The start date of the loan
* @param returnDate The date which the loan must be returned by
*/
public LinkLoanDescriptor(float value, Date startDate, Date returnDate) {
public LinkLoanDescriptor(BigDecimal value, Date startDate, Date returnDate) {

Check warning on line 116 in src/main/java/seedu/address/logic/commands/LinkLoanCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/LinkLoanCommand.java#L116

Added line #L116 was not covered by tests
this.value = value;
this.startDate = startDate;
this.returnDate = returnDate;
Expand All @@ -127,11 +128,11 @@
setReturnDate(toCopy.returnDate);
}

public void setValue(float value) {
public void setValue(BigDecimal value) {
this.value = value;
}

public float getValue() {
public BigDecimal getValue() {
return value;
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -135,7 +136,7 @@
public static LinkLoanDescriptor parseLoan(String value, String startDate, String returnDate)
throws ParseException {
requireAllNonNull(value, startDate, returnDate);
float convertedValue = parseValue(value);
BigDecimal convertedValue = parseValue(value);

Check warning on line 139 in src/main/java/seedu/address/logic/parser/ParserUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ParserUtil.java#L139

Added line #L139 was not covered by tests
Date convertedStartDate = parseDate(startDate);
Date convertedReturnDate = parseDate(returnDate);
if (!Loan.isValidDates(convertedStartDate, convertedReturnDate)) {
Expand All @@ -145,14 +146,14 @@
}

/**
* Parses loan {@code String value} into a {@code float}.
* Parses loan {@code String value} into a {@code BigDecimal}.
*/
public static float parseValue(String value) throws ParseException {
public static BigDecimal parseValue(String value) throws ParseException {
requireNonNull(value);
String trimmedValue = value.trim();
float convertedValue;
BigDecimal convertedValue;
try {
convertedValue = Float.parseFloat(trimmedValue);
convertedValue = new BigDecimal(trimmedValue);

Check warning on line 156 in src/main/java/seedu/address/logic/parser/ParserUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ParserUtil.java#L156

Added line #L156 was not covered by tests
} catch (NumberFormatException n) {
// Ths is caught when the formatter is unable to parse the value correctly
throw new ParseException(Loan.VALUE_CONSTRAINTS);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -261,7 +262,7 @@

@Override
public void generateDashboardData(Analytics analytics) {
float impactBenchmark = this.addressBook.getUniqueLoanList().getMaxLoanValue();
BigDecimal impactBenchmark = this.addressBook.getUniqueLoanList().getMaxLoanValue();

Check warning on line 265 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L265

Added line #L265 was not covered by tests
Date urgencyBenchmark = this.addressBook.getUniqueLoanList().getEarliestReturnDate();
dashboardData.setValue(new DashboardData(analytics, impactBenchmark, urgencyBenchmark));
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/seedu/address/model/analytics/DashboardData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.analytics;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
Expand All @@ -15,7 +17,7 @@
*/
public class DashboardData {
private Analytics analytics;
private float maxLoanValue;
private BigDecimal maxLoanValue;
private Date earliestReturnDate;

/**
Expand All @@ -25,7 +27,7 @@
* @param maxLoanValue maximum loan value of all loans
* @param earliestReturnDate earliest return date of all loans (not returned and not overdue)
*/
public DashboardData(Analytics analytics, float maxLoanValue, Date earliestReturnDate) {
public DashboardData(Analytics analytics, BigDecimal maxLoanValue, Date earliestReturnDate) {

Check warning on line 30 in src/main/java/seedu/address/model/analytics/DashboardData.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/analytics/DashboardData.java#L30

Added line #L30 was not covered by tests
this.analytics = analytics;
this.maxLoanValue = maxLoanValue;
this.earliestReturnDate = earliestReturnDate;
Expand All @@ -35,18 +37,19 @@
return analytics;
}

public float getMaxLoanValue() {
public BigDecimal getMaxLoanValue() {
return maxLoanValue;
}

/**
* Calculates the impact index of the dashboard data
* Impact index is calculated as the ratio of the average loan value to the maximum loan value
* to 2 decimal places.
*
* @return impact index between 0 and 1
*/
public float getImpactIndex() {
return analytics.getAverageLoanValue() / maxLoanValue;
public BigDecimal getImpactIndex() {
return analytics.getAverageLoanValue().divide(maxLoanValue, 2, RoundingMode.HALF_UP);

Check warning on line 52 in src/main/java/seedu/address/model/analytics/DashboardData.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/analytics/DashboardData.java#L52

Added line #L52 was not covered by tests
}

/**
Expand Down
59 changes: 32 additions & 27 deletions src/main/java/seedu/address/model/person/Analytics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.person;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import javafx.collections.ObservableList;
Expand All @@ -16,13 +18,13 @@
private float propOverdueLoans; // proportion of loans that are overdue over active loans
private float propActiveLoans; // proportion of loans that are active over total loans

private float totalValueLoaned; // total value of all loans
private float totalValueOverdue; // total value of all overdue loans
private float totalValueActive; // total value of all active loans
private BigDecimal totalValueLoaned; // total value of all loans
private BigDecimal totalValueOverdue; // total value of all overdue loans
private BigDecimal totalValueActive; // total value of all active loans

private float averageLoanValue; // average loan value of all loans
private float averageOverdueValue; // average loan value of all overdue loans
private float averageActiveValue; // average loan value of all active loans
private BigDecimal averageLoanValue; // average loan value of all loans
private BigDecimal averageOverdueValue; // average loan value of all overdue loans
private BigDecimal averageActiveValue; // average loan value of all active loans

private Date earliestLoanDate; // earliest loan date of all loans
private Date earliestReturnDate; // earliest return date of active loans
Expand All @@ -37,13 +39,13 @@
this.propOverdueLoans = 0;
this.propActiveLoans = 0;

this.totalValueLoaned = 0;
this.totalValueOverdue = 0;
this.totalValueActive = 0;
this.totalValueLoaned = BigDecimal.ZERO;
this.totalValueOverdue = BigDecimal.ZERO;
this.totalValueActive = BigDecimal.ZERO;

Check warning on line 44 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L42-L44

Added lines #L42 - L44 were not covered by tests

this.averageLoanValue = 0;
this.averageOverdueValue = 0;
this.averageActiveValue = 0;
this.averageLoanValue = BigDecimal.ZERO;
this.averageOverdueValue = BigDecimal.ZERO;
this.averageActiveValue = BigDecimal.ZERO;

Check warning on line 48 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L46-L48

Added lines #L46 - L48 were not covered by tests

this.earliestLoanDate = null;
this.earliestReturnDate = null;
Expand Down Expand Up @@ -83,12 +85,12 @@
* @param loan The loan to update the fields with.
*/
private void updateValueFields(Loan loan) {
this.totalValueLoaned += loan.getValue();
totalValueLoaned = totalValueLoaned.add(loan.getValue());

Check warning on line 88 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L88

Added line #L88 was not covered by tests
if (loan.isOverdue()) {
this.totalValueOverdue += loan.getValue();
totalValueOverdue = totalValueOverdue.add(loan.getValue());

Check warning on line 90 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L90

Added line #L90 was not covered by tests
}
if (loan.isActive()) {
this.totalValueActive += loan.getValue();
totalValueActive = totalValueActive.add(loan.getValue());

Check warning on line 93 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L93

Added line #L93 was not covered by tests
}
}

Expand All @@ -97,14 +99,17 @@
* This method should be called after the fields that calculate the total value of various loans have been updated.
*/
private void updateAverageFields() {
if (this.numActiveLoans > 0) {
this.averageActiveValue = this.totalValueActive / this.numActiveLoans;
if (numActiveLoans > 0) {
averageActiveValue = totalValueActive.divide(BigDecimal.valueOf(numActiveLoans),

Check warning on line 103 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L103

Added line #L103 was not covered by tests
2, RoundingMode.HALF_UP);
}
if (this.numOverdueLoans > 0) {
this.averageOverdueValue = this.totalValueOverdue / this.numOverdueLoans;
if (numOverdueLoans > 0) {
averageOverdueValue = totalValueOverdue.divide(BigDecimal.valueOf(numOverdueLoans),

Check warning on line 107 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L107

Added line #L107 was not covered by tests
2, RoundingMode.HALF_UP);
}
if (this.numLoans > 0) {
this.averageLoanValue = this.totalValueLoaned / this.numLoans;
if (numLoans > 0) {
averageLoanValue = totalValueLoaned.divide(BigDecimal.valueOf(this.numLoans),

Check warning on line 111 in src/main/java/seedu/address/model/person/Analytics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Analytics.java#L111

Added line #L111 was not covered by tests
2, RoundingMode.HALF_UP);
}
}

Expand Down Expand Up @@ -171,27 +176,27 @@
return propActiveLoans;
}

public float getTotalValueLoaned() {
public BigDecimal getTotalValueLoaned() {
return totalValueLoaned;
}

public float getTotalValueOverdue() {
public BigDecimal getTotalValueOverdue() {
return totalValueOverdue;
}

public float getTotalValueActive() {
public BigDecimal getTotalValueActive() {
return totalValueActive;
}

public float getAverageLoanValue() {
public BigDecimal getAverageLoanValue() {
return averageLoanValue;
}

public float getAverageOverdueValue() {
public BigDecimal getAverageOverdueValue() {
return averageOverdueValue;
}

public float getAverageActiveValue() {
public BigDecimal getAverageActiveValue() {
return averageActiveValue;
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/seedu/address/model/person/Loan.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import java.math.BigDecimal;
import java.util.Date;

import seedu.address.commons.util.DateUtil;
Expand All @@ -18,7 +19,7 @@
public static final String VALUE_CONSTRAINTS = "Loan values must be a positive number.";

private final int id;
private final float value;
private final BigDecimal value;
private final Date startDate;
private final Date returnDate;
private boolean isReturned;
Expand All @@ -33,7 +34,7 @@
* @param returnDate A valid return date.
* @param assignee A valid assignee.
*/
public Loan(int id, float value, Date startDate, Date returnDate, Person assignee) {
public Loan(int id, BigDecimal value, Date startDate, Date returnDate, Person assignee) {

Check warning on line 37 in src/main/java/seedu/address/model/person/Loan.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Loan.java#L37

Added line #L37 was not covered by tests
requireAllNonNull(id, value, startDate, returnDate, assignee);
assert isValidValue(value);
assert id >= 0;
Expand All @@ -55,7 +56,7 @@
* @param isReturned A valid return status.
* @param assignee A valid assignee.
*/
public Loan(int id, float value, Date startDate, Date returnDate, boolean isReturned, Person assignee) {
public Loan(int id, BigDecimal value, Date startDate, Date returnDate, boolean isReturned, Person assignee) {

Check warning on line 59 in src/main/java/seedu/address/model/person/Loan.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Loan.java#L59

Added line #L59 was not covered by tests
requireAllNonNull(id, value, startDate, returnDate, isReturned, assignee);
assert isValidValue(value);
assert id >= 0;
Expand All @@ -68,10 +69,10 @@
}

/**
* Returns true if a given float is a valid value.
* Returns true if a given BigDecimal is a valid value.
*/
public static boolean isValidValue(float value) {
return value > 0;
public static boolean isValidValue(BigDecimal value) {
return value.compareTo(BigDecimal.ZERO) > 0;
}

/**
Expand All @@ -85,7 +86,7 @@
return id;
}

public float getValue() {
public BigDecimal getValue() {
return value;
}

Expand Down
Loading
Loading