Skip to content

Commit

Permalink
Merge pull request #247 from MayFairMI6/bug-resolve
Browse files Browse the repository at this point in the history
Assertion for limit added
  • Loading branch information
MayFairMI6 authored Nov 12, 2024
2 parents 6907d1d + fea79bc commit 0bce696
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/main/java/seedu/spendswift/parser/InputParser.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@@author glenda-1506

package seedu.spendswift.parser;

import seedu.spendswift.ui.ErrorMessage;
Expand All @@ -25,9 +25,17 @@ private String parseComponent(String input, String prefix) {

startIndex += prefix.length();
int endIndex = input.length();
String[] prefixes = {"n/", "a/", "c/", "e/", "l/"};
String[] prefixes = {
"n/",
"a/",
"c/",
"e/",
"l/",
"hcur/",
"ocur/"
};

for (String otherPrefix : prefixes) {
for (String otherPrefix: prefixes) {
if (!otherPrefix.equals(prefix)) {
int prefixIndex = input.indexOf(otherPrefix, startIndex);
if (prefixIndex != -1 && prefixIndex < endIndex) {
Expand All @@ -39,6 +47,8 @@ private String parseComponent(String input, String prefix) {
return input.substring(startIndex, endIndex).trim();
}



/**
* Parses an indexed component from teh string input using "e/" prefix.
*
Expand All @@ -51,9 +61,18 @@ public int parseIndex(String input) {
return Integer.parseInt(indexStr) - 1;
} catch (NumberFormatException e) {
return -1;

}
}

public String parseName(String input) {
return parseComponent(input, "n/");
}

public String parseCategory(String input) {
return parseComponent(input, "c/");
}

/**
* Parses a limit component from teh string input using "l/" prefix.
*
Expand All @@ -63,19 +82,35 @@ public int parseIndex(String input) {
public double parseLimit(String input) {
String limitStr = parseComponent(input, "l/");
try {
return Double.parseDouble(limitStr);
// Parse the limit string into double
double limit = Double.parseDouble(limitStr);
final double quadrillion = 1000000000000000.0;

// Assertion to ensure the limit does not exceed 1 quadrillion
assert limit <= quadrillion: "Limit exceeds 1 quadrillion, capping to 1 quadrillion";

// Cap the limit at 1 quadrillion if it exceeds this value
if (limit > quadrillion) {
System.err.println("Limit exceeds 1 quadrillion, capping to 1 quadrillion.");
limit = quadrillion;
}

return limit;
} catch (NumberFormatException e) {
ErrorMessage.printInvalidLimit();
System.err.println("Invalid limit format: '" + limitStr + "'.");
return Double.NaN;
}
}



/**
* Parses an amount component from the input using "a/" prefix..
*
* @param input The input string containing the amount.
* @return The extracted amount as a double.
*/

public double parseAmount(String input) {
String amountStr = parseComponent(input, "a/");
try {
Expand All @@ -85,25 +120,10 @@ public double parseAmount(String input) {
return Double.NaN;
}
}
}

/**
* Parses a name component from the input using "n/" prefix..
*
* @param input The input string containing the name.
* @return The extracted name as a string.
*/
public String parseName(String input) {
return parseComponent(input, "n/");
}



/**
* Parses a category component from the input using "c/" prefix..
*
* @param input The input string containing the category.
* @return The extracted category as a string.
*/
public String parseCategory(String input) {
return parseComponent(input, "c/");
}
}


2 changes: 2 additions & 0 deletions src/test/java/seedu/spendswift/SpendSwiftTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@




import org.junit.jupiter.api.BeforeEach;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -547,3 +548,4 @@ void testFormatAmountNegativeValue() {
assertEquals("-$123.45", Format.formatAmount(-123.45), "Should handle negative values correctly.");
}
}

0 comments on commit 0bce696

Please sign in to comment.