From 159e231414131559c047b339a2d788a5d4b73f66 Mon Sep 17 00:00:00 2001 From: yooplo Date: Thu, 10 Oct 2024 15:38:20 +0800 Subject: [PATCH] Refactor the getting of tag code in FindCommandParser --- .../logic/parser/FindCommandParser.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index d4e0827940f..7ea044f6dfa 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -28,18 +28,10 @@ public class FindCommandParser implements Parser { */ 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()) { @@ -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()) { @@ -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; + } }