Skip to content

Commit

Permalink
Merge pull request #41 from Joellimjr/v1.0
Browse files Browse the repository at this point in the history
parser update to handle blank and garbage inputs
  • Loading branch information
Joellimjr committed Mar 21, 2024
2 parents 2540828 + 1515788 commit a29af20
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Messages {
public static final String INVALID_COMMAND = "Invalid command detected. Type 'help' for list of valid commands";
public static final String HELP = "Commands: ....";
public static final String INVALID_ADD_FORMAT ="Invalid command format. Please use format: " +
"'add [ITEM_NAME] qty/[QUANTITY_OF_ITEM] /[UNIT_OF_MEASUREMENT]'";
"'add [ITEM_NAME] qty/[QUANTITY_OF_ITEM] /[UNIT_OF_MEASUREMENT] cat/[CATEGORY]'";
public static final String INVALID_DELETE_FORMAT ="Invalid command format. Please use format: 'del [ITEM_NAME]'";
public static final String INVALID_EDIT_FORMAT ="Invalid command format. Please use format: " +
"'edit [ITEM_NAME] qty/[NEW_QUANTITY]'";
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

public class Parser {
public static final Pattern ADD_COMMAND_FORMAT =
//Pattern.compile("add (?<itemName>[^/]+) qty/(?<quantity>\\d+) /(?<uom>[^/]+)
// (?: cat/(?<category>[^/]+))?");
Pattern.compile("add (?<itemName>[^/]+) qty/(?<quantity>\\d+) /(?<uom>[^/]+)");
Pattern.compile("add (?<itemName>[^/]+) qty/(?<quantity>\\d+) /(?<uom>[^/]+)(?: cat/(?<category>[^/]+))?");

public static final Pattern DELETE_COMMAND_FORMAT =
Pattern.compile("del (?<itemName>[^/]+)");
Expand All @@ -32,16 +30,21 @@ public class Parser {
Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)");


public Command parseInput(String userInput) {

public Command parseInput(String userInput){
final CommandType userCommand;
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
System.out.println(Messages.INVALID_COMMAND);
System.out.println(Messages.HELP);
return new IncorrectCommand();
}
String commandWord = matcher.group("commandWord").toUpperCase();
try {
userCommand = CommandType.valueOf(commandWord);
} catch (IllegalArgumentException e){
System.out.println(Messages.INVALID_COMMAND);
return new IncorrectCommand();
}

final CommandType userCommand = CommandType.valueOf(matcher.group("commandWord").toUpperCase());


switch (userCommand) {
case EXIT:
Expand Down Expand Up @@ -71,7 +74,7 @@ public Command parseInput(String userInput) {
}
default:
System.out.println(Messages.INVALID_COMMAND);
break;
return new IncorrectCommand();
}
return new IncorrectCommand();
}
Expand All @@ -84,13 +87,13 @@ private Command prepareAdd(String args) throws CommandFormatException{
if (!matcher.matches()) {
throw new CommandFormatException(CommandType.ADD);
}
//String category = matcher.group("category") != null ? matcher.group("category") : "NA";
String category = matcher.group("category") != null ? matcher.group("category") : "NA";
int quantity = Integer.parseInt(matcher.group("quantity"));
return new AddCommand(
matcher.group("itemName"),
quantity,
matcher.group("uom"), "test cat"
//category
matcher.group("uom"),
category
);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import command.Command;
import command.ExitCommand;
import exceptions.CommandFormatException;
import parser.Parser;
import ui.TextUi;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class Storage {
private static final String FILENAME = "./StockMasterData.txt";

private static File stockMaster;


Expand Down Expand Up @@ -78,5 +78,4 @@ public static void main (String[]args){
System.out.println("File does not exist.");
}
}
}

}

0 comments on commit a29af20

Please sign in to comment.