Skip to content

Commit

Permalink
Merge pull request #123 from kjw142857/convert-to-bigdecimal
Browse files Browse the repository at this point in the history
Replace loan value types with BigDecimals
  • Loading branch information
marcus-ny authored Apr 4, 2024
2 parents 8b0e1d6 + 91cb2c5 commit 8e5d841
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 70 deletions.
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 String generateSuccessMessage(Loan editedLoan) {

private LinkLoanDescriptor generateEditedLoanDetails(Loan loanToEdit, EditLoanDescriptor editedDetails)
throws CommandException {
float newValue = editedDetails.getValue().orElse(loanToEdit.getValue());
BigDecimal newValue = editedDetails.getValue().orElse(loanToEdit.getValue());
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 @@ public String toString() {
* Stores the details of the loan that is edited.
*/
public static class EditLoanDescriptor {
private Float value = null;
private BigDecimal value = null;
private Date startDate = null;
private Date returnDate = null;

Expand All @@ -147,11 +148,11 @@ public boolean isAnyFieldEdited() {
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 @@ public String toString() {
* 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 @@ public static class LinkLoanDescriptor {
* @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) {
this.value = value;
this.startDate = startDate;
this.returnDate = returnDate;
Expand All @@ -127,11 +128,11 @@ public LinkLoanDescriptor(LinkLoanCommand.LinkLoanDescriptor toCopy) {
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 Set<Tag> parseTags(Collection<String> tags) throws ParseException
public static LinkLoanDescriptor parseLoan(String value, String startDate, String returnDate)
throws ParseException {
requireAllNonNull(value, startDate, returnDate);
float convertedValue = parseValue(value);
BigDecimal convertedValue = parseValue(value);
Date convertedStartDate = parseDate(startDate);
Date convertedReturnDate = parseDate(returnDate);
if (!Loan.isValidDates(convertedStartDate, convertedReturnDate)) {
Expand All @@ -145,14 +146,14 @@ public static LinkLoanDescriptor parseLoan(String value, String startDate, Strin
}

/**
* 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);
} 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 @@ public ObjectProperty<DashboardData> getDashboardData() {

@Override
public void generateDashboardData(Analytics analytics) {
float impactBenchmark = this.addressBook.getUniqueLoanList().getMaxLoanValue();
BigDecimal impactBenchmark = this.addressBook.getUniqueLoanList().getMaxLoanValue();
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 @@ public class DashboardData {
* @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) {
this.analytics = analytics;
this.maxLoanValue = maxLoanValue;
this.earliestReturnDate = earliestReturnDate;
Expand All @@ -35,18 +37,19 @@ public Analytics getAnalytics() {
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);
}

/**
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 @@ public class Analytics {
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 @@ private Analytics() {
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;

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

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

Expand All @@ -97,14 +99,17 @@ private void updateValueFields(Loan loan) {
* 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),
2, RoundingMode.HALF_UP);
}
if (this.numOverdueLoans > 0) {
this.averageOverdueValue = this.totalValueOverdue / this.numOverdueLoans;
if (numOverdueLoans > 0) {
averageOverdueValue = totalValueOverdue.divide(BigDecimal.valueOf(numOverdueLoans),
2, RoundingMode.HALF_UP);
}
if (this.numLoans > 0) {
this.averageLoanValue = this.totalValueLoaned / this.numLoans;
if (numLoans > 0) {
averageLoanValue = totalValueLoaned.divide(BigDecimal.valueOf(this.numLoans),
2, RoundingMode.HALF_UP);
}
}

Expand Down Expand Up @@ -171,27 +176,27 @@ public float getPropActiveLoans() {
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 class Loan implements Comparable<Loan> {
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 @@ public class Loan implements Comparable<Loan> {
* @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) {
requireAllNonNull(id, value, startDate, returnDate, assignee);
assert isValidValue(value);
assert id >= 0;
Expand All @@ -55,7 +56,7 @@ public Loan(int id, float value, Date startDate, Date returnDate, Person assigne
* @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) {
requireAllNonNull(id, value, startDate, returnDate, isReturned, assignee);
assert isValidValue(value);
assert id >= 0;
Expand All @@ -68,10 +69,10 @@ public Loan(int id, float value, Date startDate, Date returnDate, boolean isRetu
}

/**
* 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 @@ public int getId() {
return id;
}

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

Expand Down
Loading

0 comments on commit 8e5d841

Please sign in to comment.