Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement add command with optional arguments #75

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/binbash/Parser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/binbash/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

public class AddCommand extends Command {

public static final Pattern COMMAND_FORMAT =
Pattern.compile("add\\s+n/(?<itemName>.+?)\\s+d/(?<itemDescription>.+)\\s+q/(?<itemQuantity>.+)\\s"
+ "+e/(?<itemExpirationDate>.+)+s/(?<itemSalePrice>.+)+c/(?<itemCostPrice>.+)");
public static final Pattern COMMAND_FORMAT = Pattern.compile(
"add\\s" + "n/(?<itemName>.+?)\\s" + "d/(?<itemDescription>.+?)\\s" + "(q/(?<itemQuantity>.+?))?"
+ "(e/(?<itemExpirationDate>.+?))?" + "s/(?<itemSalePrice>.+?)\\s" + "c/(?<itemCostPrice>.+)"
);
private final String itemName;
private final String itemDescription;
private final int itemQuantity;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/binbash/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,8 +123,13 @@ private ArrayList<Item> parseAndAddToList(ArrayList<String> 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"));

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/seedu/binbash/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
71 changes: 71 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
5 changes: 5 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading