diff --git a/.gitignore b/.gitignore index 19294a30cb..2203331e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,8 @@ bin/ /text-ui-test/ACTUAL.TXT text-ui-test/EXPECTED-UNIX.TXT +text-ui-test/data +text-ui-test/data/* # Data files /data/ diff --git a/src/main/java/seedu/binbash/Parser.java b/src/main/java/seedu/binbash/Parser.java index e9791f099f..60be50fca5 100644 --- a/src/main/java/seedu/binbash/Parser.java +++ b/src/main/java/seedu/binbash/Parser.java @@ -1,6 +1,7 @@ package seedu.binbash; import java.util.regex.Matcher; +import java.util.Objects; import seedu.binbash.command.Command; import seedu.binbash.command.AddCommand; @@ -60,8 +61,13 @@ private Command parseAddCommand(String userInput) throws InvalidFormatException } String itemName = matcher.group("itemName"); String itemDescription = matcher.group("itemDescription"); - int itemQuantity = Integer.parseInt(matcher.group("itemQuantity")); - String itemExpirationDate = matcher.group("itemExpirationDate"); + int itemQuantity = Integer.parseInt( + Objects.requireNonNullElse(matcher.group("itemQuantity"), "0").strip() + ); + String itemExpirationDate = Objects.requireNonNullElse( // If no expiration date provided, set as N.A. + matcher.group("itemExpirationDate"), + "N.A." + ).strip(); double itemSalePrice = Double.parseDouble(matcher.group("itemSalePrice")); double itemCostPrice = Double.parseDouble(matcher.group("itemCostPrice")); diff --git a/src/main/java/seedu/binbash/command/AddCommand.java b/src/main/java/seedu/binbash/command/AddCommand.java index b176508176..71e950e758 100644 --- a/src/main/java/seedu/binbash/command/AddCommand.java +++ b/src/main/java/seedu/binbash/command/AddCommand.java @@ -5,9 +5,10 @@ public class AddCommand extends Command { - public static final Pattern COMMAND_FORMAT = - Pattern.compile("add\\s+n/(?.+?)\\s+d/(?.+)\\s+q/(?.+)\\s" - + "+e/(?.+)+s/(?.+)+c/(?.+)"); + public static final Pattern COMMAND_FORMAT = Pattern.compile( + "add\\s" + "n/(?.+?)\\s" + "d/(?.+?)\\s" + "(q/(?.+?))?" + + "(e/(?.+?))?" + "s/(?.+?)\\s" + "c/(?.+)" + ); private final String itemName; private final String itemDescription; private final int itemQuantity; diff --git a/src/main/java/seedu/binbash/storage/Storage.java b/src/main/java/seedu/binbash/storage/Storage.java index 2411bacbe9..16262d750e 100644 --- a/src/main/java/seedu/binbash/storage/Storage.java +++ b/src/main/java/seedu/binbash/storage/Storage.java @@ -11,6 +11,7 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -122,8 +123,13 @@ private ArrayList parseAndAddToList(ArrayList stringRepresentation if (matcher.matches()) { String itemName = matcher.group("itemName"); String itemDescription = matcher.group("itemDescription"); - int itemQuantity = Integer.parseInt(matcher.group("itemQuantity")); - String itemExpirationDate = matcher.group("itemExpirationDate"); + int itemQuantity = Integer.parseInt( + Objects.requireNonNullElse(matcher.group("itemQuantity"), "0").strip() + ); + String itemExpirationDate = Objects.requireNonNullElse( + matcher.group("itemExpirationDate"), + "N.A." + ).strip(); double itemSalePrice = Double.parseDouble(matcher.group("itemSalePrice")); double itemCostPrice = Double.parseDouble(matcher.group("itemCostPrice")); diff --git a/src/test/java/seedu/binbash/ParserTest.java b/src/test/java/seedu/binbash/ParserTest.java index 31471e42d8..1d319caa5b 100644 --- a/src/test/java/seedu/binbash/ParserTest.java +++ b/src/test/java/seedu/binbash/ParserTest.java @@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assertions.fail; import java.util.ArrayList; + +import seedu.binbash.command.AddCommand; import seedu.binbash.command.Command; import seedu.binbash.command.DeleteCommand; import seedu.binbash.command.SearchCommand; @@ -41,6 +43,50 @@ public void testParseCommand_invalidCommand_throwsInvalidCommandException() { assertThrows(InvalidCommandException.class, () -> parser.parseCommand("invalid")); } + @Test + public void parseAddCommand_createItemWithNoQuantityAndExpirationDate_returnsAddCommand() { + try { + itemList.addItem("Test Item", "Test Description", 0, "N.A.", 0.00, 0.00); + Command command = parser.parseCommand("add n/Test Item d/Test Description s/0.00 c/0.00"); + assertTrue(command instanceof AddCommand); + } catch (InvalidCommandException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + @Test + public void parseAddCommand_createItemWithNoQuantity_returnsAddCommand() { + try { + itemList.addItem("Test Item", "Test Description", 0, "01-01-1999", 0.00, 0.00); + Command command = parser.parseCommand("add n/Test Item d/Test Description e/01-01-1999 s/0.00 c/0.00"); + assertTrue(command instanceof AddCommand); + } catch (InvalidCommandException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + @Test + public void parseAddCommand_createItemWithNoExpiration_returnsAddCommand() { + try { + itemList.addItem("Test Item", "Test Description", 10, "N.A.", 0.00, 0.00); + Command command = parser.parseCommand("add n/Test Item d/Test Description q/10 s/0.00 c/0.00"); + assertTrue(command instanceof AddCommand); + } catch (InvalidCommandException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + @Test + public void parseAddCommand_createItemWithAllArguments_returnsAddCommand() { + try { + itemList.addItem("Test Item", "Test Description", 10, "01-01-1999", 0.00, 0.00); + Command command = parser.parseCommand("add n/Test Item d/Test Description q/10 e/01-01-1999 s/0.00 c/0.00"); + assertTrue(command instanceof AddCommand); + } catch (InvalidCommandException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + @Test public void testParseCommand_validCommandDelete_returnsDeleteCommand() { try { diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 159e1f18b3..7416005a9d 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -9,6 +9,77 @@ Welcome to BinBash! ------------------------------------------------------------- ------------------------------------------------------------- +------------------------------------------------------------- +------------------------------------------------------------- +Noted! I have added the following item into your inventory: + +item with + description: no exp or quantity + quantity: 0 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 +------------------------------------------------------------- +------------------------------------------------------------- +Noted! I have added the following item into your inventory: + +item with + description: no exp + quantity: 10 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 +------------------------------------------------------------- +------------------------------------------------------------- +Noted! I have added the following item into your inventory: + +item with + description: no quantity + quantity: 0 + expiry date: 01-01-1999 + sale price: $0.00 + cost price: $100.00 +------------------------------------------------------------- +------------------------------------------------------------- +Noted! I have added the following item into your inventory: + +item with + description: everything + quantity: 100 + expiry date: 01-01-1999 + sale price: $0.00 + cost price: $100.00 +------------------------------------------------------------- +------------------------------------------------------------- +1. item with + description: no exp or quantity + quantity: 0 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 + +2. item with + description: no exp + quantity: 10 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 + +3. item with + description: no quantity + quantity: 0 + expiry date: 01-01-1999 + sale price: $0.00 + cost price: $100.00 + +4. item with + description: everything + quantity: 100 + expiry date: 01-01-1999 + sale price: $0.00 + cost price: $100.00 + + ------------------------------------------------------------- ------------------------------------------------------------- Bye! diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index c3d148eb89..3500282d2a 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,2 +1,7 @@ list +add n/item with d/no exp or quantity s/0.00 c/100.00 +add n/item with d/no exp q/10 s/0.00 c/100.00 +add n/item with d/no quantity e/01-01-1999 s/0.00 c/100.00 +add n/item with d/everything q/100 e/01-01-1999 s/0.00 c/100.00 +list bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 25ac7a2989..f2e5d745da 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -2,6 +2,9 @@ setlocal enableextensions pushd %~dp0 +if exist data del data\items.txt +if exist data rmdir data + cd .. call gradlew clean shadowJar diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index 1dcbd12021..77e134b8ca 100755 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -8,6 +8,10 @@ cd .. cd text-ui-test +# cleanup old data files +rm data/items.txt +rmdir data + java -jar $(find ../build/libs/ -mindepth 1 -print -quit) < input.txt > ACTUAL.TXT cp EXPECTED.TXT EXPECTED-UNIX.TXT