Skip to content

Commit

Permalink
Merge pull request #106 from NicholasChungJunJie/NicholasChungJunJie
Browse files Browse the repository at this point in the history
Refactor additem validation
  • Loading branch information
yuanners authored Mar 15, 2023
2 parents e27aaa9 + 31d4622 commit c34e740
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 156 deletions.
5 changes: 4 additions & 1 deletion src/main/java/utility/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public String[] formatInput(String input) {
public Map<String, String> formatArguments(String argString) {


String regex = "(?:^|\\s)(?:--|-)(\\w+)(?:\\s+(-?[\\d.]+\\w+)|\\s+'([^']*)'|\\s+\"([^\"]*)\"|\\s*([^\\s-][^\\s]*)|\\s*(?=--|-|$))?";
String regex = "(?:^|\\s)(?:--|-)(\\w+)(?:\\s+(-?[\\d.]+" +
"\\w+)|\\s+'([^']*)'|\\s+\"([^\"]*)\"|\\s*([^\\s-]" +
"[^\\s]*)|\\s*(?=--|-|$))?";


Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(argString);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/validation/item/AddItemValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public void validateCommand(Command c, Menu items) throws ItemException {
}

public void validateName(Command c, Menu items) throws ItemException {
if(c.getArgumentMap().get(LONG_NAME_FLAG) == null) {
throw new ItemException(ui.getItemNameMinLengthError());
}

if (c.getArgumentMap().get(LONG_NAME_FLAG).length() > 25) {
throw new ItemException(ui.getItemNameMaxLengthError());
}
Expand All @@ -67,6 +71,10 @@ public void validateName(Command c, Menu items) throws ItemException {
* @param c Given command
*/
public void validatePrice(Command c) throws ItemException {
if(c.getArgumentMap().get(LONG_PRICE_FLAG) == null) {
throw new ItemException(ui.getItemPriceMinLengthError());
}

String price = c.getArgumentMap().get(LONG_PRICE_FLAG);
price = price.trim();

Expand Down
17 changes: 6 additions & 11 deletions src/test/java/seedu/moneygowhere/ItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public void runTest(String input, MoneyGoWhere moneyGoWhere) {
@Test
public void itemTest() {

runTest("additem -p 20.1 -n chicken rice100", moneyGoWhere);
runTest("additem -p 20.1 -n \"chicken rice100\"", moneyGoWhere);
assertEquals("chicken rice100", moneyGoWhere.items.getItems().
get(moneyGoWhere.items.getItems().size() - 1).getName());

assert moneyGoWhere.items.getItems().
get(moneyGoWhere.items.getItems().size() - 1).getName()
== "chicken rice100": "Item name should be chicken rice100";
get(moneyGoWhere.items.getItems().size() - 1)
.getName().equals("chicken rice100"): "Item name should be chicken rice100";

assertEquals(20.10, moneyGoWhere.items.getItems().
get(moneyGoWhere.items.getItems().size() - 1).getPrice());
Expand All @@ -49,21 +50,15 @@ public void itemTest() {

@Test
public void itemTest2() {
runTest("additem -p 2kuku0.01 -n chicken rice3", moneyGoWhere);
runTest("additem -p 2kuku0.01 -n \"chicken rice3\"", moneyGoWhere);

assert moneyGoWhere.items.getItems().
get(moneyGoWhere.items.getItems().size() - 1).getName()
== "chicken rice100": "Item name should be chicken rice100";
}

@Test
public void itemTest3() {
// max 2dp error
runTest("additem -p 20.001 -n chicken rice4", moneyGoWhere);
runTest("additem -p 20.0001 -n \"chicken rice4\"", moneyGoWhere);

assert moneyGoWhere.items.getItems().
get(moneyGoWhere.items.getItems().size() - 1).getName()
== "chicken rice100": "Item name should be chicken rice100";
}

}
142 changes: 0 additions & 142 deletions src/test/java/seedu/moneygowhere/OrderTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/utility/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ void formatArguments_printMap(){

System.out.println(argMap);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/utility/StoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class StoreTest {
void save_order() {

}
}
}

0 comments on commit c34e740

Please sign in to comment.