From 1c3c910c136151429113cf8f1d8da37caf01f915 Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 16:54:19 +0800 Subject: [PATCH 1/7] Update .gitignore to ignore generated data files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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/ From eb8326e473637e1c3d7712de5570698a9fe42cb5 Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 16:54:40 +0800 Subject: [PATCH 2/7] Update text tests --- text-ui-test/EXPECTED.TXT | 71 +++++++++++++++++++++++++++++++++++++++ text-ui-test/input.txt | 5 +++ 2 files changed, 76 insertions(+) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 159e1f18b3..2e70ddb449 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 +------------------------------------------------------------- +------------------------------------------------------------- +item with + description: no exp or quantity + quantity: 0 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 + +item with + description: no exp + quantity: 10 + expiry date: N.A. + sale price: $0.00 + cost price: $100.00 + +item with + description: no quantity + quantity: 0 + expiry date: 01-01-1999 + sale price: $0.00 + cost price: $100.00 + +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 From 4e4d739d79ded9ee54a258b69238d0ff4a18a57a Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 16:58:02 +0800 Subject: [PATCH 3/7] Update text-UI test scripts --- text-ui-test/runtest.bat | 3 +++ text-ui-test/runtest.sh | 4 ++++ 2 files changed, 7 insertions(+) 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 From 5d3e671da3483b422942748531c1678fd9584cb9 Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 16:58:58 +0800 Subject: [PATCH 4/7] Update add command regex format --- src/main/java/seedu/binbash/command/AddCommand.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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; From d95826d94b97fe16bee702490b6ef84e0c913e5c Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 17:00:03 +0800 Subject: [PATCH 5/7] Update add command parse logic --- src/main/java/seedu/binbash/Parser.java | 10 ++++++++-- src/main/java/seedu/binbash/storage/Storage.java | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) 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/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")); From 5a68d9bf6cdb041db4b85ad4a656b3fb14b27351 Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 17:00:28 +0800 Subject: [PATCH 6/7] Add unit tests for add command --- src/test/java/seedu/binbash/ParserTest.java | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) 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 { From 3faf12ab17db375288b088e5ee6e443e25a322a4 Mon Sep 17 00:00:00 2001 From: YHWong20 Date: Tue, 19 Mar 2024 17:04:48 +0800 Subject: [PATCH 7/7] Update text-UI tests --- text-ui-test/EXPECTED.TXT | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 2e70ddb449..7416005a9d 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -51,28 +51,28 @@ item with cost price: $100.00 ------------------------------------------------------------- ------------------------------------------------------------- -item with +1. item with description: no exp or quantity quantity: 0 expiry date: N.A. sale price: $0.00 cost price: $100.00 -item with +2. item with description: no exp quantity: 10 expiry date: N.A. sale price: $0.00 cost price: $100.00 -item with +3. item with description: no quantity quantity: 0 expiry date: 01-01-1999 sale price: $0.00 cost price: $100.00 -item with +4. item with description: everything quantity: 100 expiry date: 01-01-1999