diff --git a/src/main/java/seedu/spendswift/parser/InputParser.java b/src/main/java/seedu/spendswift/parser/InputParser.java index 158d38a771..8e1c9988ce 100644 --- a/src/main/java/seedu/spendswift/parser/InputParser.java +++ b/src/main/java/seedu/spendswift/parser/InputParser.java @@ -1,4 +1,4 @@ -//@@author glenda-1506 + package seedu.spendswift.parser; import seedu.spendswift.ui.ErrorMessage; @@ -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) { @@ -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. * @@ -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. * @@ -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 { @@ -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/"); - } -} + + diff --git a/src/test/java/seedu/spendswift/SpendSwiftTest.java b/src/test/java/seedu/spendswift/SpendSwiftTest.java index a360ced505..e225e1b435 100644 --- a/src/test/java/seedu/spendswift/SpendSwiftTest.java +++ b/src/test/java/seedu/spendswift/SpendSwiftTest.java @@ -15,6 +15,7 @@ + import org.junit.jupiter.api.BeforeEach; import java.util.Arrays; import java.util.HashMap; @@ -547,3 +548,4 @@ void testFormatAmountNegativeValue() { assertEquals("-$123.45", Format.formatAmount(-123.45), "Should handle negative values correctly."); } } +