Skip to content

Commit

Permalink
Enforce Money upper limit
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Sep 29, 2023
1 parent e13e3bd commit c11e93f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/model/person/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/
public class Money {

public static final String MESSAGE_CONSTRAINTS = "Dollar amount should be a positive integer";
public static final String MESSAGE_CONSTRAINTS = "Dollar amount should be a positive integer " +
"with max value of $1,000,000,000,000";
public static final Long MAX_VALUE = (long) 1e12;

public final String amount;

Expand All @@ -19,9 +21,10 @@ public class Money {
*/
public Money(String numStr) {
requireNonNull(numStr);
checkArgument(numStr.length() < 14, MESSAGE_CONSTRAINTS);
try {
Integer num = Integer.valueOf(numStr);
checkArgument(num >= 0, MESSAGE_CONSTRAINTS);
Long num = Long.valueOf(numStr);
checkArgument(num >= 0 && num <= MAX_VALUE, MESSAGE_CONSTRAINTS);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(MESSAGE_CONSTRAINTS);
}
Expand All @@ -33,12 +36,11 @@ public Money(String numStr) {
*/
public static boolean isValidMoney(String test) {
try {
Integer num = Integer.valueOf(test);
checkArgument(num >= 0, MESSAGE_CONSTRAINTS);
Long num = Long.valueOf(test);
return num >= 0 && num <= MAX_VALUE;
} catch (NumberFormatException e) {
return false;
}
return true;
}

@Override
Expand Down

0 comments on commit c11e93f

Please sign in to comment.