Skip to content

Commit

Permalink
Refactor the getting of tag code in FindCommandParser
Browse files Browse the repository at this point in the history
  • Loading branch information
yooplo committed Oct 10, 2024
1 parent cb0a3f9 commit 159e231
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,10 @@ public class FindCommandParser implements Parser<AbstractFindCommand> {
*/
public AbstractFindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
int index = trimmedArgs.indexOf(' ');
String tag;
String searchTerms = "";
// Check if there is a whitespace character in the string
if (index != -1) {
tag = trimmedArgs.substring(0, index); // Part before the first whitespace
searchTerms = trimmedArgs.substring(index + 1); // Part after the first whitespace
} else {
tag = trimmedArgs;
}

Matcher m = KEYWORD_EXTRACTOR.matcher(tag); // find tag and search words
String[] tagAndSearchTerms = getTagAndSearchTerms(trimmedArgs);
String tag = tagAndSearchTerms[0];
String searchTerms = tagAndSearchTerms[1];
Matcher m = KEYWORD_EXTRACTOR.matcher(tag); // check if the correct tag exists

// will throw exception if no args/command format not correct
if (args.isEmpty() || !m.matches()) {
Expand All @@ -50,7 +42,7 @@ public AbstractFindCommand parse(String args) throws ParseException {
// extract tag and search argument
String[] searchTermArray = searchTerms.split("\\s+");

// return appropriate FindCommand class depending on tag
// return appropriate FindCommand class and specific warning message to user based on the tag
switch (tag) {
case "/n":
if (searchTerms.isEmpty()) {
Expand All @@ -71,4 +63,12 @@ public AbstractFindCommand parse(String args) throws ParseException {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AbstractFindCommand.MESSAGE_USAGE));
}
}
private String[] getTagAndSearchTerms(String input) {
int index = input.indexOf(' ');
String[] result = new String[2];
// Check if there is a whitespace character in the string
result[0] = index != -1 ? input.substring(0, index) : input; // Part before the first whitespace
result[1] = index != -1 ? input.substring(index + 1) : ""; // Part after the first whitespace
return result;
}
}

0 comments on commit 159e231

Please sign in to comment.